How to select a row in Jquery datatable

2019-03-26 00:50发布

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.

4条回答
贼婆χ
2楼-- · 2019-03-26 01:07

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

}
查看更多
手持菜刀,她持情操
3楼-- · 2019-03-26 01:24

If you wanna select multiple row, wanna get the data of selected row for ajax purpose check this

http://jsfiddle.net/ezospama/1/

DataTable code will be as follows

$(document).ready(function() {
    var table = $('#datatable').DataTable();

    $('#datatable tbody').on( 'click', 'tr', function (){
        $(this).toggleClass('selected');
    } );

    $('#btn').click( function () {
        console.log(table.rows('.selected').data());
        alert("Check the console for selected data");
    } );
})
查看更多
再贱就再见
4楼-- · 2019-03-26 01:34

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.
查看更多
可以哭但决不认输i
5楼-- · 2019-03-26 01:34

The selected class should be, Within your function you used $s and you define var s which is not the same var.

"oTableTools": {
           "sSelectedClass": "yourclassname",
           "sRowSelect": "single",
           "fnRowSelected": function ( node ) {
               var s=$(node).children();
                   alert("Selected Row : " + s[0]);
               }
       }   
查看更多
登录 后发表回答