简体   繁体   English

Datatables jquery 插件 - 限制可选行数

[英]Datatables jquery plugin - limit number of selectable rows

When row selection is enabled on a datatable, is there way to limit the number of rows a user can select?在数据表上启用行选择时,是否可以限制用户可以选择的行数? I only want users to be able to select a maximum of two rows and a minimum of one in the datatable, but I don't see an option in the Datatables API that describes how to do this?我只希望用户能够在数据表中选择最多两行和最少一行,但我在数据表 API 中没有看到描述如何执行此操作的选项?

Will I need to perform this manually in some callback that's triggered whenever a user selects a row?我是否需要在用户选择行时触发的某些回调中手动执行此操作? I'd like to avoid this if possible.如果可能的话,我想避免这种情况。 Any help or insight is appreciated.任何帮助或见解表示赞赏。

Using datatables example:使用数据表示例:

$(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');
        }
        // the following line do what you want
        else if(oTable.$('tr.row_selected').length < 2) {
            $(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( );
} );

Reference: select single row参考: 选择单行

Ok got an implementation working, so what I ended up doing was instead of limiting the number rows a user can select, I added some logic to deselect any previously selected row whenever a user clicked on another row.好的,实现了一个工作,所以我最终做的不是限制用户可以选择的行数,而是添加了一些逻辑来在用户单击另一行时取消选择任何先前选择的行。 I treated the selection order as a queue, which I just implemented using an array, each time I push a new row to the queue, any previous element would get popped.我将选择顺序视为一个队列,我只是使用一个数组来实现它,每次我将一个新行推入队列时,任何先前的元素都会被弹出。

The size of the queue determines the number of rows a user can select, by setting the queue size to two, this allows up to two rows to be selected, when an additional row is selected, the first selected row is removed from the queue. The size of the queue determines the number of rows a user can select, by setting the queue size to two, this allows up to two rows to be selected, when an additional row is selected, the first selected row is removed from the queue. I used the fnGetInstance() and fnDeselect() functions below for deselecting rows from my data table instance我使用下面的 fnGetInstance() 和 fnDeselect() 函数从我的数据表实例中取消选择行

//oldRow = first selected row from queue

var oTT = TableTools.fnGetInstance( 'MyDataTable' );
oTT.fnDeselect( oldRow );

As already pointed out in another answer, there is a single row slection example from DataTables, but unfortunately it is not fully usable and convincing.正如另一个答案中已经指出的那样,DataTables 中有一个单行选择示例,但不幸的是,它并不完全可用且令人信服。 It's evident from the code that their solution is external to the package, but also from a testing experience you can see that the native selection count is not updated (and that's not nice).从代码中可以明显看出他们的解决方案是包外部的,而且从测试经验中您可以看到本机选择计数未更新(这并不好)。

Anyway the most important part of the above said example - if we want to partially re-use it - is the ability to catch the event in a situation that is not already natively managed by the table.on( 'deselect', feauture. Which situation is it? The Ctrl + click by which one can select multiple items. Therefore the new solution structure will be无论如何,上述示例中最重要的部分 - 如果我们想部分重用它 - 是能够在table.on( 'deselect', feauture) 尚未本地管理的情况下捕获事件。情况是这样吗? Ctrl + click可以选择多个项目。因此,新的解决方案结构将是

table.on( 'click', 'tr', function () {
    // https://stackoverflow.com/a/42266749/11323942
    if ( event.ctrlKey ) {
        //is ctrl + click
        console.log("ctrl + click intercepted but forcing single select")
        // NOW WHAT TO DO?
    }
} );

The best thing to do now is deliver the control back to their framework's standard events.现在最好的办法是将控制权交还给他们框架的标准事件。 So所以

// NOW WHAT TO DO?
table.rows({ selected: true }).deselect();

The above limits the selection to a single row, but it's easy to extend to a different number of rows and of course now you can perform whatever reset action you might want in the appropriate way以上将选择限制为单行,但很容易扩展到不同数量的行,当然现在您可以以适当的方式执行您可能想要的任何重置操作

table.on( 'deselect', function ( e, dt, type, indexes ) {
    if ( type === 'row' ) {
        var deselectedrow = indexes[0]
        console.log('deselected row', deselectedrow);
        // perform whatever reset action you might need
    }
} );  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM