Question

I need the option to choose rows on 2 different Datatables independently for a few pages. These tables have different buttons that manipulate the tables according to the selected row. Here is the code to select a row for one table: https://datatables.net/release-datatables/examples/api/select_single_row.html I cannot get this to work for more than one table, even if I duplicate all of the code and use different ids. http://jsfiddle.net/BWCBX/ Any ideas?

var oTable;

$(document).ready(function() {
    /* Add a click handler to the rows - this could be used as a callback */
    $("#example tbody tr").click( function( e ) {
        if ( $(this).hasClass('row_selected') ) {
            $(this).removeClass('row_selected');
        }
        else {
            oTable.$('tr.row_selected').removeClass('row_selected');
            $(this).addClass('row_selected');
        }
    });

    /* Add a click handler for the delete row */
    $('#delete').click( function() {
        var anSelected = fnGetSelected( oTable );
        if ( anSelected.length !== 0 ) {
            oTable.fnDeleteRow( anSelected[0] );
        }
    } );

    /* Init the table */
    oTable = $('#example').dataTable( );
} );


/* Get the rows which are currently selected */
function fnGetSelected( oTableLocal )
{
    return oTableLocal.$('tr.row_selected');
}
Was it helpful?

Solution

In the code of jsfiddle, in the version 2 of the click and delete functions, you forgot to change oTable for oTable2, this is why the duplicate code don't work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top