Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select a row in Jquery datatable

I am using datatables in my application. Whenever user click on any row I want to highlight it and pick some values from selected row.

"oTableTools": {
               "sRowSelect": "single",

               "fnRowSelected": function ( node ) {
                   var s=$(node).children();
                       alert("Selected Row : " + $s[0]);
                   }

I tried sRowSelect and fnRowSelected but no luck. The row is not highlighted and neither fnRowSelected is called. Even no error on console.

Here is my complete code

  var userTable = $('#users').dataTable({
            "bPaginate": true,
            "bScrollCollapse": true,
            "iDisplayLength": 10,
            "bFilter": false,
            "bJQueryUI": true,
            "sPaginationType": "full_numbers",
            "oLanguage": {
                "sLengthMenu": "Display _MENU_ records per page",
                "sZeroRecords": "Enter a string and click on search",
                "sInfo": "Showing _START_ to _END_ of _TOTAL_ results",
                "sInfoEmpty": "Showing 0 to 0 of 0 results",
                "sInfoFiltered": "(filtered from _MAX_ total results)"
            },
            "aaSorting": [[ 0, "asc" ]],
            "aoColumns": [/* Name */ null,
                          /*Institution*/null,
                          /*Email*/null],
           "oTableTools": {
               "sRowSelect": "single",

               "fnRowSelected": function ( node ) {
                 alert("Clicked");
                   }
           }       
        });    

Am I missing anything ?

EDIT:
Now able to highlight selected row.Added class="display" to HTML table. Still wondering why I didn't find this in datatable docs. Now looking how to collect selected values.

like image 673
Ajinkya Avatar asked Nov 23 '11 13:11

Ajinkya


People also ask

How to select all rows in DataTable jQuery?

The selectAll button will simply select all items in the table, based on the current item selection mode ( select. items() ) - e.g. if the item selection mode is rows , all rows in the table will be selected when this button is activated.

How do I select the first row of a DataTable?

use Datatable. rows(0) this will give you the first row in the datatable.


2 Answers

Here is how I do it

just add this function to your page (if users is your table id)

$("#users tbody").delegate("tr", "click", function() {
var iPos = userTable.fnGetPosition( this );
if(iPos!=null){
    //couple of example on what can be done with the clicked row...
    var aData = userTable.fnGetData( iPos );//get data of the clicked row
    var iId = aData[1];//get column data of the row
    userTable.fnDeleteRow(iPos);//delete row

}
like image 63
Daniel Avatar answered Sep 24 '22 13:09

Daniel


When you are using fnRowSelected (i.e. when creating new tabletool) you have to use

"sRowSelect": "multi", 

That will resolve the issue. Please increment my comment count if it helps. I need to have more points.

I used it in my code as follows

    pqrtbl = new TableTools(NameOfTbl, { "sRowSelect": "multi",
                                        "fnRowSelected": function ( node ) {
                                            var s= $(node).children();
                                            fnAddToSelLst(s[1].innerText); 
                                        },.......................

//column index depend upon your req.
like image 29
psdk Avatar answered Sep 25 '22 13:09

psdk