2

我正在使用数据表,并且我选择了多行,我想对多行执行删除操作。例如,我选择了第 2、3 和 4 行,所以我希望同时删除所有 3 行。

<script>
var oTable;
var giRedraw = false;

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


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

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

function fnGetSelected( oTableLocal )
{
    var aReturn = new Array();
    var aTrs = oTableLocal.fnGetNodes();

    for ( var i=0 ; i<aTrs.length ; i++ )
    {
        if ( $(aTrs[i]).hasClass('row_selected') )
        {
            aReturn.push( aTrs[i] );
        }
    }
    return aReturn;
}

</script>

通过此脚本一次仅删除 1 行。

4

1 回答 1

1

尝试这个。

$('#delete').click( function() {
    var anSelected = fnGetSelected( oTable );
    oTable.fnDeleteRow( anSelected ); 
    // previously it was anSelected[0] which select only frist element
} );

已编辑

我对 fnGetSelected() 函数进行了一些更改。可能有帮助

function fnGetSelected( oTableLocal )
{
    var aReturn = new Array();
    var aTrs = oTableLocal.fnGetNodes();

    for ( var i=0 ; i<aTrs.length ; i++ )
    {
        // removed $() because this is already DOM element.
        if ( aTrs[i].hasClass('row_selected') ) 
        {
            aReturn.push( aTrs[i] );
        }
    }
    return aReturn;
}
于 2011-06-28T11:14:54.337 回答