删除多行

时间:2011-06-28 11:07:48

标签: php mysql jquery-datatables

我正在使用数据表,我选择了多行,我想对多行执行删除操作。例如,我选择了第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行。

1 个答案:

答案 0 :(得分: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;
}