Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery DataTable add ID to TR element after row is dynamically added

I am using this code to init DataTable:

    $('#table-groups').dataTable( {
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "bFilter": false,
    "bInfo": false,
    "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
    "aoColumns": [
     { "sClass": "name" },
     { "sClass": "tools", "bSortable": false},
     ],     
    });

Now I am adding rows via server-side script like this:

$('#table-groups').dataTable().fnAddData( ["<strong>"+$_returnvalue.name+"</strong>","<div class=\"cell edit\"> Group ID is: "+$_returnvalue.entryid+" </div>"]);   

And my question: is there a way to insert value of $_returnvalue.entryid to be ID of the <tr> ?

like image 622
Peter Avatar asked Jan 30 '13 03:01

Peter


People also ask

How to add or delete a row in a table using jQuery?

To add a row, define a variable that keeps the count of the total number of that now exists in the table. Then we will use the jQuery “click” event to detect a click on the add row button and then use the .append () method of jQuery to add a row in the table. Each row element has been assigned an id Ri that we will later use to delete a row.

How to modify row index and row ID using jQuery?

In order to tackle the second problem, we will get all the rows next to the row where the remove button is clicked using the .nextAll () method of jQuery and then iterate across each of these elements to modify the row index and row id. The code is as follows: This code can be modified in several ways according to the needs.

How to send unique ID from serverside DataTable?

You are not using serverside datatable here. This answer may help those who are using serverside datatable and trying to put unique ID (from serverside, based on db data). You just have to send the ID by this key: DT_RowId (from serverside, as response).

Is the DataTables API backwards compatible?

Legacy interface notice: This discussion was created before the release of DataTables 1.10, which introduced a more modern API. The documentation for the old DataTables API is still available and newer versions are backwards compatible, but the primary documentation on this site refers to DataTables 1.10 and newer.


1 Answers

You are not using serverside datatable here. This answer may help those who are using serverside datatable and trying to put unique ID (from serverside, based on db data).

You just have to send the ID by this key: DT_RowId (from serverside, as response). It doesn't require any change in the frontend. More details of this can be found in the following link: https://datatables.net/examples/server_side/ids.html

like image 182
Farid Chowdhury Avatar answered Oct 04 '22 00:10

Farid Chowdhury