admin管理员组

文章数量:1297026

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?

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?

Share Improve this question asked Jul 24, 2016 at 9:46 manohar emanohar e 3273 gold badges6 silver badges17 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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();

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

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();

// 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/reference/api/row().index()

本文标签: javascriptHow to update selected row in jquery data tablesStack Overflow