0

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');
}
4

1 回答 1

1

在jsfiddle的代码中,在第2版的点击和删除功能中,你忘记将oTable更改为oTable2,这就是为什么重复代码不起作用的原因。

于 2013-09-24T17:43:15.103 回答