简体   繁体   English

如何使用shift键选择jQuery数据表中的多行

[英]How to select the multiple row in jQuery data table using the shift key

I need to select multiple rows in a jQuery data table using the shift-key, the same as the shift-key select function, and store the selected row items in a variable. 我需要使用shift键在jQuery数据表中选择多行,与shift-key select函数相同,并将选定的行项存储在变量中。 Also I need to select multiple rows using a click event. 另外,我需要使用click事件选择多行。

Using a single variable, I need to pass the live click rows and shift selection rows. 使用单个变量,我需要传递实时单击行和移动选择行。 How I do that? 我怎么做的?

This is not working properly and does not store a single variable for the selected rows. 这不能正常工作,并且不会为所选行存储单个变量。 Where am I wrong? 我哪里错了?

My Code: 我的代码:

var aSelected = [];
var lastSelected;

$('#table tr').live('click', function (event) {
    var tableRow = $(this).closest("tr").prevAll("tr").length + 1;

    var id = this.cells[0].innerHTML;
    var index = jQuery.inArray(id, aSelected);
    if (index === -1) {
        aSelected.push(id);
    } else {
        aSelected.splice(index, 1);
    }

    $(this).toggleClass('row_selected');

    if (event.shiftKey) {
        var table = $('#table ');
        var start = Math.min(tableRow, lastSelected);
        var end = Math.max(tableRow, lastSelected);

        for (var i = start; i < end; i++) {
            $('#table').each(function () {
                table.find('tr:gt(' + (start - 1) + '):lt(' + (end) + ')').addClass('row_selected');
            });
        }

    } else {

        lastSelected = $(this).closest("tr").prevAll("tr").length + 1;
    }
});

For example I have 20 rows. 例如,我有20行。 There are 3 cause I select the rows and passing the selected row id 1. using Ctrl key select the multiple rows and passing 2. using shift key select the next by next 10 rows and passing 3. using shift key select the next by next 10 rows and extra 2 rows using Ctrl key other 10 rows and passing the 12 rows 有3个原因我选择行并传递选定的行ID 1.使用Ctrl键选择多行并传递2.使用shift键选择下一个接下来的10行并传递3.使用shift键选择下一个接下来的10个行行和额外的2行使用Ctrl键其他10行并传递12行

use the following code 使用以下代码

     $('#tableId').dataTable( {

    "sDom": 'T<"clear">lfrtip',
    "oTableTools": {
        "sRowSelect": "multi",
        "aButtons": [ "select_all", "select_none" ]
    }
} );

by using this code you select multiple rows with out using shift key 通过使用此代码,您可以使用shift键选择多行

 window.onmousemove = function (e) { shiftKey=false; if (e.shiftKey) shiftKey=true; } dataTableID='yourDataTable'; $('#'+dataTableID).DataTable().on( 'select', function ( e, dt, type, indexes ) { thisID=e.currentTarget.id; selecting=$('#'+thisID).attr('selecting'); if (typeof(selecting)=="undefined" || selecting=="false"){ idx=indexes[0]; lastSelected=$('#'+thisID).attr('lastSelected'); if (shiftKey==true) { $('#'+thisID).attr('lastSelected',idx); if (typeof(lastSelected)!="undefined" && lastSelected !='') { min=idx+1; max=lastSelected; if (max<min) { tmp=min; min=max; max=tmp; } if (min<max) { $('#'+thisID).attr('selecting',"true"); for (x=min;x<max;x++) $('#'+thisID).DataTable().row( x ).select(); $('#'+thisID).attr('selecting',"false"); } } } else { $('#'+thisID).attr('lastSelected',''); } } }); $('#'+settings.sTableId).DataTable().on( 'deselect', function ( e, dt, type, indexes ) { thisID=e.currentTarget.id; $('#'+thisID).attr('lastSelected',''); }); 

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

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