How do I capture table td elements using mousedown.dragselect event?

后端 未结 1 1150
天命终不由人
天命终不由人 2021-02-09 06:13

I have a directive which renders a HTML table where each td element has an id

What I want to accomplish is to use the mousedown.dragselect/mouseup.dr

1条回答
  •  佛祖请我去吃肉
    2021-02-09 06:46

    I suspect using anything relating to dragging won't give you what you want. Dragging is actually used when moving elements about (e.g. dragging files in My Computer / Finder), when what you're after is multiple selection.

    So there a number of things the directive needs:

    • Listen to mousedown, mouseenter and mouseup, events.

      • mousedown should listen on the cells of the table, and set a "dragging" mode.
      • mouseenter should listen on the cells as well, and if the directive is in dragging mode, select the "appropriate cells"
      • mouseup should disable dragging mode, and actually be on the whole body, in case the mouse is lifted up while the cursor is not over the table.
    • jQuery delegation is useful here, as it can nicely delegate the above events to the table, so the code is much more friendly to cells that are added after this directive is initialised. (I wouldn't include or use jQuery in an Angular project unless you have a clear reason like this).

    • Although you've not mentioned it, the "appropriate cells" I suspect all the cells "between" where the mouse was clicked, and the current cell, chosen in a rectangle, and not just the cells that have been entered while the mouse was held down. To find these, cellIndex and rowIndex can be used, together with filtering all the cells from the table.

    • All the listeners should be wrapped $scope.$apply to make sure Angular runs a digest cycle after they fire.

    • For the directive to communicate the ids of the selected elements to the surrounding scope, the directive can use bi-directional binding using the scope property, and the = symbol, as explained in the Angular docs

    Putting all this together gives:

    app.directive('dragSelect', function($window, $document) {
      return {
        scope: {
          dragSelectIds: '='
        },
        controller: function($scope, $element) {
          var cls = 'eng-selected-item';
          var startCell = null;
          var dragging = false;
    
          function mouseUp(el) {
            dragging = false;
          }
    
          function mouseDown(el) {
            dragging = true;
            setStartCell(el);
            setEndCell(el);
          }
    
          function mouseEnter(el) {
            if (!dragging) return;
            setEndCell(el);
          }
    
          function setStartCell(el) {
            startCell = el;
          }
    
          function setEndCell(el) {
            $scope.dragSelectIds = [];
            $element.find('td').removeClass(cls);
            cellsBetween(startCell, el).each(function() {
              var el = angular.element(this);
              el.addClass(cls);
              $scope.dragSelectIds.push(el.attr('id'));
            });
          }
    
          function cellsBetween(start, end) {
            var coordsStart = getCoords(start);
            var coordsEnd = getCoords(end);
            var topLeft = {
              column: $window.Math.min(coordsStart.column, coordsEnd.column),
              row: $window.Math.min(coordsStart.row, coordsEnd.row),
            };
            var bottomRight = {
              column: $window.Math.max(coordsStart.column, coordsEnd.column),
              row: $window.Math.max(coordsStart.row, coordsEnd.row),
            };
            return $element.find('td').filter(function() {
              var el = angular.element(this);
              var coords = getCoords(el);
              return coords.column >= topLeft.column
                  && coords.column <= bottomRight.column
                  && coords.row >= topLeft.row
                  && coords.row <= bottomRight.row;
            });
          }
    
          function getCoords(cell) {
            var row = cell.parents('row');
            return {
              column: cell[0].cellIndex, 
              row: cell.parent()[0].rowIndex
            };
          }
    
          function wrap(fn) {
            return function() {
              var el = angular.element(this);
              $scope.$apply(function() {
                fn(el);
              });
            }
          }
    
          $element.delegate('td', 'mousedown', wrap(mouseDown));
          $element.delegate('td', 'mouseenter', wrap(mouseEnter));
          $document.delegate('body', 'mouseup', wrap(mouseUp));
        }
      }
    });
    

    Another thing that will make the experience a bit nicer, is to set the cursor to a pointer, and disable text selection

    [drag-select] {
      cursor: pointer;
      -webkit-touch-callout: none;
      -webkit-user-select: none;
      -khtml-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;
    }
    

    You can also see this in action in this working demo

    0 讨论(0)
提交回复
热议问题