Select multiple HTML table rows with Ctrl+click and Shift+click

前端 未结 6 1386
再見小時候
再見小時候 2021-01-31 20:20

Demo

I want to select multiple rows using Windows Shift and Ctrl keys, like multiple folder selection in Windows.

From table of selected ro

6条回答
  •  忘掉有多难
    2021-01-31 21:23

    I know this question is already answered and it's pretty old, but I found the answer by andyb to be super helpful. Perhaps it was because andyb's answer might be outdated now, but I ended up having to change his solution a bit to work with my project, so I figured I'd share my updated version. Here is what I ended up with, using a sprinkling of jQuery.

    $(document).ready(function(){
        //put all the table rows in a variable after page load to pass in to RowClick
        var trs = $('#tableStudent tr')
        //bind the click handler to all the table rows
        $('tr').on('click', function(){
            //call the RowClick function on click event
            RowClick($(this),false,trs)
        })
    })
    
    //declare variable to store the most recently clicked row
    var lastSelectedRow;
    
    // disable text selection
    document.onselectstart = function() {
        return false;
    }
    
    function RowClick(currentrow, lock, rows) {
        //if control is held down, toggle the row
        if (window.event.ctrlKey) {
            toggleRow(currentrow);
        }
    
        //if there are no buttons held down...
        if (window.event.button === 0) {
    
            //if neither control or shift are held down...
            if (!window.event.ctrlKey && !window.event.shiftKey) {
                //clear selection
                clearAll(rows);
                //toggle clicked row
                toggleRow(currentrow);
            }
    
            //if shift is held down...
            if (window.event.shiftKey) {
                //pass the indexes of the last selected row and currently selected row along with all rows
                selectRowsBetweenIndexes([lastSelectedRow.index(), currentrow.index()], rows)
            }
        }
    }
    
    function toggleRow(row) {
        //if the row is not the header row...
        if (!row.hasClass('header-row')){
            //if the row is selected...
            if (row.hasClass('selected')){
                //deselect it
                row.removeClass('selected')
            }
            else{
                //otherwise, select it
                row.addClass('selected')
            }
            //reassign the most recently selected row
            lastSelectedRow = row;
        }
    }
    
    function selectRowsBetweenIndexes(indexes,rows) {
        //sort the indexes in ascending order
        indexes.sort(function(a, b) {
            return a - b;
        });
    
        //for every row starting at the first index, until the second index...
        for (var i = indexes[0]; i <= indexes[1]; i++) {
            //select the row
            $(rows[i+1]).addClass('selected');
        }
    }
    
    function clearAll(rows) {
        //for all rows...
        for (var i = 0; i < rows.length; i++) {
            //deselect each row
            $(rows[i]).removeClass("selected");
        }
    }
    

提交回复
热议问题