Jquery function when selected the first row and if deselect all selections

Jquery function when selected the first row and if deselect all selections

typhontyphon Posts: 25Questions: 7Answers: 0

Hello,

is it possible to call a function to show a menu if the first row is selected and call a function to hide the menu if deselect all selections?

Thanks and BR

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,322Questions: 1Answers: 10,023 Site admin

    select and deselect are the two events you'd probably want to listen for here.

    Allan

  • typhontyphon Posts: 25Questions: 7Answers: 0

    Thanks for the fast response!

    DataTables displays @ bottom of the table how many rows are selected. Is it possible to get this value?

  • kthorngrenkthorngren Posts: 20,089Questions: 26Answers: 4,721

    I think page.info() is what you are looking for.

    Kevin

  • typhontyphon Posts: 25Questions: 7Answers: 0

    No, I'm not looking for the page. If I use multiselect the Datatables shows how many rows are selected.

    I insert a simple counter which increase on select event and decrease on deselect. It works fine.

    How I get the row ID from the selected rows? I tried the example but it dont work.

    var table = $('#example').DataTable();
     
    table.on( 'select', function ( e, dt, type, indexes ) {
        if ( type === 'row' ) {
            var data = table.rows( indexes ).data().pluck( 'id' );
     
            // do something with the ID of the selected items
             alert('I want to display the row ids from the selected rows');
        }
    } );
    
  • kthorngrenkthorngren Posts: 20,089Questions: 26Answers: 4,721
    Answer ✓

    No, I'm not looking for the page. If I use multiselect the Datatables shows how many rows are selected.

    Sorry misread your question. This example shows how to get the selected row count:
    https://datatables.net/extensions/select/examples/api/get.html

    How I get the row ID from the selected rows?
    Use the same selector as in the above example:

    var data = table.rows( { selected: true } ).data().pluck( 'id' );
    

    If you want just an array of data (without the API attached) you can use this:

    var data = table.rows( { selected: true } ).data().pluck( 'id' ).toArray();
    

    Kevin

  • typhontyphon Posts: 25Questions: 7Answers: 0

    Thanks for the fast response!

    But I don't get the row IDs with your example. Can you edit my code snipet to display the is?

  • kthorngrenkthorngren Posts: 20,089Questions: 26Answers: 4,721
    Answer ✓

    Have you defined a column id using columns.data?

    Doesn't look like you have in the above code. If not then you need to change pluck('id') to pluck( 0 ). Replace the 0 with your column number containing the 'id'.

    Kevin

  • typhontyphon Posts: 25Questions: 7Answers: 0

    Ok, so I get the column. Thanks, is there a way to get the 'id' from <tr id="XXX" ...
    ?

  • kthorngrenkthorngren Posts: 20,089Questions: 26Answers: 4,721

    Its still unclear to me how you have everything setup but you could try using rows().ids() to get the ID's. Might not work in your case.

    You can use rows().nodes() to get the tr nodes of the selected rows. This may help.

    Kevin

This discussion has been closed.