Wrong rows().length after adding rows

Wrong rows().length after adding rows

TeroaTeroa Posts: 9Questions: 3Answers: 0

Hi,

I've got a strange problem.
Via API I receive a tr-node. Now I check if the row is already present. If not I add the row to the table by row().add().
If not, I want to update some fields, for example the date column by cell().data().

But after adding a row, the rows length is still 1.
Thats a problem, because If I want to edit the data of a cell of an existing row, the first row will be updated and not the correct row.

Every row got a uniqe id "task_" + id

var table = $('#tasks_' + task.state).DataTable();
selector = '#row_' + task.id;
content = $(task.node);
       
row = table.row(selector);
if (!row.any()) {
  table.row.add(content).draw();
}else{
  datetime = moment(task.date)
  row.cell('.date').data(datetime.calendar()).draw()
}

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,624Questions: 26Answers: 5,009
    edited March 2018

    Does the row show in the table after row.add()?

    It looks like your code snippet should work. Maybe you can put together a test case so we can see everything you are doing:
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • TeroaTeroa Posts: 9Questions: 3Answers: 0

    Hello Kevin,

    yes the row will be added. I've created a test case.
    You will see, that the new age will be added to the row task_1 instead of task_2.
    I found the row. but why I cannot update the data?

    http://live.datatables.net/jotiyiqi/1/edit

  • kthorngrenkthorngren Posts: 21,624Questions: 26Answers: 5,009
    Answer ✓

    Great, thanks for the example. The problem is this line:
    row.cell('.date').data(datetime.calendar()).draw()

    The row() api doesn't have a method cell(). You will need to use cell() with the row-selector and column-selector. Something more like this:
    table.cell(row.index(), '.date').data(datetime.calendar()).draw()`

    Additionally you will need to move your class ".date" from the row to the thead. The column selector will look in the header for the class not the row.

    You can see this in the updated example:
    http://live.datatables.net/jotiyiqi/2/edit

    Kevin

  • TeroaTeroa Posts: 9Questions: 3Answers: 0

    Hey Kevin,

    now I really understand whats wrong.
    Thank you very much. :smile:

This discussion has been closed.