2

Could you please tell me how to update the selected row in the jquery data tables?

In the below code I am trying to set a cell. Looks like it is set but not reflecting in the table.

var datatable = $('#table').DataTable();
datatable.row('.selected').cell(':eq(1)').data("1234");
alert(datatable.row('.selected').cell(':eq(1)').data());

Here able to alert the changed value but the same is not reflecting in the data table UI. Can you help me?

4 Answers 4

4

Official documentation

draw() Since: DataTables 1.10 Redraw the table.

Description When you perform an action such as adding or deleting a row, changing the sorting, filtering or paging characteristics of the table you'll want DataTables to update the display to reflect these changes. This function is provided for that purpose.

your code is correct but datatables won't apply changes automatically, just call the draw() function.

datatable.row('.selected').cell(':eq(1)').data('123').draw();
Sign up to request clarification or add additional context in comments.

1 Comment

it works on change event for specific row with column but when change event occurs other row it still changed on old cell and row.
3

You have to redraw the table using .draw() method as mentioned in the documentation of cell().data() method:

Note that when used as a setter, this method sets the data to apply to the table, storing it in the data source array or object for the row, but does not update the table's internal caches of the data (i.e. the search and order cache) until the draw() method is called.

datatable.row('.selected').cell(':eq(1)').data("1234").draw();

Check the documentation

1 Comment

I did that also but still it is not reflecting in the UI.
1

For me, this was the solution:

var t = $('#myTable').DataTable();
t.cell('.selectedRow', ':eq(0)').data('something data');
t.cell('.selectedRow', ':eq(1)').data('something data');
t.cell('.selectedRow', ':eq(2)').data('something data');
t.draw();

Comments

0

// selectedRowIndex should be global var selectedRowIndex = oTable.row(this).index();

// At the time of updating data in the selected row, do this oTable.cell(selectedRowIndex,2).data("xyz"); // where 2 is the cell index

// it will reflect the data in the grid. :)

https://datatables.net/reference/api/row().index()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.