Swap a row and set a value update the whole row values

Swap a row and set a value update the whole row values

mc2devmc2dev Posts: 33Questions: 6Answers: 0
edited January 2022 in DataTables

Hi,
Here is the method I'm using to move a row up or down in datatable :

// Move a row up or down
  moveRow(currentRow, direction) {
    var currentRowIndex = currentRow.index()
    if (direction === 'up') {
      if (currentRowIndex < 1) {
        return
      }
    } else {
      var rowCount = $(this.tableId).dataTable().fnGetData().length
      if (currentRowIndex + 1 === rowCount) {
        return
      }
    }
    const nextRow =
      direction === 'up'
        ? this.table.row(currentRowIndex - 1)
        : this.table.row(currentRowIndex + 1)
    const currentRowData = currentRow.data()
    const nextRowData = nextRow.data()
    currentRow.data(nextRowData).draw()
    nextRow.data(currentRowData).draw()
  }

But when I move a row up or down then update value to one of the moved row, the other take the same values on the whole row... I tried to comment my instruction on submit and edit but the problem persists so I think my problem comes from this moveRow() method.

Any ideas what I'm badly writing here?

Thank you for your support!

Answers

  • kthorngrenkthorngren Posts: 21,261Questions: 26Answers: 4,934
    edited January 2022

    Its hard to see specifically without being able to debug the code.

    I believe the row().index() is the order that the table data is loaded into the table not the sorted order of the table. This may or may not cause an unexpected row to be fetched with this.table.row(currentRowIndex - 1) or this.table.row(currentRowIndex + 1).

    One option might be to use this example to dynamically create row indexes. Then just swap the row indexes between the two rows you want to swap. This may or may not fit into your solution.

    Please provide a test case showing an example what you are doing so we can help debug or offer suggestions.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • mc2devmc2dev Posts: 33Questions: 6Answers: 0
  • kthorngrenkthorngren Posts: 21,261Questions: 26Answers: 4,934
    edited January 2022

    You code doesn't look like the final working solution. Take a look at the last post in the thread. The OP realized the issue with using row().index() I mentioned above This solution may work in the OP's environment but not in yours without making adjustments. In order to fully help we will need a test case. Doesn't need be your full solution but something that haas an example of your data and the mechanism you are using to move the rows.

    Kevin

  • mc2devmc2dev Posts: 33Questions: 6Answers: 0
    edited January 2022

    Thank you for your time.
    I can show you this basic example but it doesn't the editor logic. I don't know if I can handle it with this tool => http://live.datatables.net/dutaboki/1/

    As you can see, the moveRow() function works but if I update a field of a moved row, it update the the previous row too. I can't share it on live.datatables.net but I can show you the view of my solution which show the problem :

  • kthorngrenkthorngren Posts: 21,261Questions: 26Answers: 4,934

    Thanks for additional information. I thought the problem was with actually swapping the rows. Didn't realize the problem was with the Editor. I added your test case to a base Editor example:
    http://live.datatables.net/guwafemu/242/edit

    You can see the problem as you described.

    Do you have a column with unique data? If so the fix is to assign the idSrc to that column. Uncomment the Editor config option //idSrc: 'name', ad you will see the updates affect the correct row.

    Kevin

Sign In or Register to comment.