Adding functionality for using the up and down arrow keys to select a table row

后端 未结 4 2204
甜味超标
甜味超标 2021-02-10 15:17

I need the help of an expert on my question below as it exceed and goes well beyond the level of knowledge that I have for programming in JavaScript.

Given the existing

4条回答
  •  野的像风
    2021-02-10 16:03

    Here is the complete solution which selects rows in table just like a windows file selection works.

    add class multiSelect to you table and then place this code in a JS file

    $(document).ready(function() {  
    var selectionPivot; 
    // code for selected rows. 
    $('.multiSelect tbody').on( 'click', 'tr', function (e) {
    var tbodyElement = $(this).parents('tbody');
    var trElements = tbodyElement.find('tr');
    if(!e.ctrlKey && (!e.shiftKey) ){   
      trElements.removeClass("row_selected");
      selectionPivot=$(this);
     } 
    
    if(e.shiftKey){ 
    var bot = Math.min(selectionPivot[0].rowIndex, $(this)[0].rowIndex);
    var top = Math.max(selectionPivot[0].rowIndex, $(this)[0].rowIndex);
    trElements.removeClass("row_selected");
    for(var i=bot; i<=top; i++){    
    trElements[i-1].className+=" row_selected";
    }     
    }  
    else  { 
       selectionPivot=$(this);
       trElements.removeClass("focus"); 
       $(this).addClass('focus');
        if ( $(this).hasClass('row_selected') ) {
        $(this).removeClass('row_selected');
        }
        else {      
            $(this).addClass('row_selected');
        }
    }
    });
    
    $(document).keypress(function(evt){
    if(evt.shiftKey){
          var highlightedRow = $(".multiSelect .focus");        
          if (highlightedRow.length > 0) // table cell is selected
          {
            var tbodyElement = highlightedRow.parents('tbody');
            var trElements = tbodyElement.find('tr');
            var nextElement =  highlightedRow.next('tr');
            var prevElement = highlightedRow.prev('tr');
            trElements.removeClass("focus");
            switch(evt.keyCode)
            {
              case 40:
                if(nextElement.length)
                {
                  nextElement.addClass('row_selected');
                  nextElement.addClass('focus');
                }
                else if (trElements.length)
                {
                  $(trElements[0]).addClass('row_selected'); 
                   $(trElements[0]).addClass('focus');
                }
                break;
              case 38:
                if(prevElement.length)
                {
                  prevElement.addClass('row_selected');
                  prevElement.addClass('focus');
                }
                else if (trElements.length)
                {
                  $(trElements[trElements.length - 1]).addClass('row_selected'); 
                    $(trElements[trElements.length - 1]).addClass('focus');
                }
                break;
            }
          }
          }
        });
        });
    

提交回复
热议问题