11

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 Answers 4

7

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for giving simple solution. I used "$(. row_selected)" to find selected row and then filtered all data by again using jquery selector.
3

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.

Comments

0

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]);
               }
       }   

3 Comments

Do we need to apply our own class to sSelectedClass ?
I do not know, but since there was an error inside your function, the highlight might have been interrupted.
i found your suggestion even on datatables website but dont know why its not working.
0

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");
    } );
})

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.