angularjs – 如何使用mousedown.dragselect事件捕获表td元素?

前端之家收集整理的这篇文章主要介绍了angularjs – 如何使用mousedown.dragselect事件捕获表td元素?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个指令,它呈现一个 HTML表,其中每个td元素都有一个id

我想要完成的是使用mousedown.dragselect / mouseup.dragselect来确定选择了哪些元素,然后突出显示那些选定的元素.到目前为止我所拥有的是这样的:

  1. var $ele = $(this);
  2. scope.bindMultipleSelection = function() {
  3. element.bind('mousedown.dragselect',function() {
  4. $document.bind('mousemove.dragselect',scope.mousemove);
  5. $document.bind('mouseup.dragselect',scope.mouseup);
  6. });
  7. };
  8.  
  9. scope.bindMultipleSelection();
  10.  
  11. scope.mousemove = function(e) {
  12. scope.selectElement($(this));
  13. };
  14.  
  15. scope.mouseup = function(e) {
  16. };
  17.  
  18. scope.selectElement = function($ele) {
  19. if (!$ele.hasClass('eng-selected-item'))
  20. $ele.addClass('eng-selected-item'); //apply selection or de-selection to current element
  21. };

如何获取mousedown.dragselect选择的每个td元素,并能够获取它们的ID然后突出显示它们?

我怀疑使用任何与拖动有关的东西都不会给你你想要的东西.拖动实际上是在移动元素时使用(例如,在“我的电脑/搜索器”中拖动文件),当你所追求的是多重选择时.

所以指令需要很多东西:

>听mousedown,mouseenter和mouseup,事件.

> mousedown应该监听表格的单元格,并设置“拖动”模式.
> mouseenter也应该监听单元格,如果指令处于拖动模式,请选择“适当的单元格”
> mouseup应该禁用拖动模式,实际上是在整个身体上,以防鼠标在光标不在桌面上时被抬起.

> jQuery delegation在这里很有用,因为它可以很好地将上述事件委托给表,因此代码对初始化此指令后添加的单元格更加友好. (除非你有明确的理由,否则我不会在Angular项目中包含或使用jQuery).
>虽然你没有提到它,但是“适当的细胞”我怀疑点击鼠标的所有单元格和在矩形中选择的当前单元格,而不仅仅是鼠标时输入的单元格被压制住了.为了找到这些,可以使用cellIndexrowIndex,以及filtering表中的所有单元格.
>所有的监听器都应该包含$scope.$apply以确保Angular在它们触发后运行一个摘要周期.
>对于将所选元素的id传递给周围范围的指令,该指令可以使用scope属性和=符号进行双向绑定,如Angular docs中所述.

把所有这些放在一起给出了:

  1. app.directive('dragSelect',function($window,$document) {
  2. return {
  3. scope: {
  4. dragSelectIds: '='
  5. },controller: function($scope,$element) {
  6. var cls = 'eng-selected-item';
  7. var startCell = null;
  8. var dragging = false;
  9.  
  10. function mouseUp(el) {
  11. dragging = false;
  12. }
  13.  
  14. function mouseDown(el) {
  15. dragging = true;
  16. setStartCell(el);
  17. setEndCell(el);
  18. }
  19.  
  20. function mouseEnter(el) {
  21. if (!dragging) return;
  22. setEndCell(el);
  23. }
  24.  
  25. function setStartCell(el) {
  26. startCell = el;
  27. }
  28.  
  29. function setEndCell(el) {
  30. $scope.dragSelectIds = [];
  31. $element.find('td').removeClass(cls);
  32. cellsBetween(startCell,el).each(function() {
  33. var el = angular.element(this);
  34. el.addClass(cls);
  35. $scope.dragSelectIds.push(el.attr('id'));
  36. });
  37. }
  38.  
  39. function cellsBetween(start,end) {
  40. var coordsStart = getCoords(start);
  41. var coordsEnd = getCoords(end);
  42. var topLeft = {
  43. column: $window.Math.min(coordsStart.column,coordsEnd.column),row: $window.Math.min(coordsStart.row,coordsEnd.row),};
  44. var bottomRight = {
  45. column: $window.Math.max(coordsStart.column,row: $window.Math.max(coordsStart.row,};
  46. return $element.find('td').filter(function() {
  47. var el = angular.element(this);
  48. var coords = getCoords(el);
  49. return coords.column >= topLeft.column
  50. && coords.column <= bottomRight.column
  51. && coords.row >= topLeft.row
  52. && coords.row <= bottomRight.row;
  53. });
  54. }
  55.  
  56. function getCoords(cell) {
  57. var row = cell.parents('row');
  58. return {
  59. column: cell[0].cellIndex,row: cell.parent()[0].rowIndex
  60. };
  61. }
  62.  
  63. function wrap(fn) {
  64. return function() {
  65. var el = angular.element(this);
  66. $scope.$apply(function() {
  67. fn(el);
  68. });
  69. }
  70. }
  71.  
  72. $element.delegate('td','mousedown',wrap(mouseDown));
  73. $element.delegate('td','mouseenter',wrap(mouseEnter));
  74. $document.delegate('body','mouseup',wrap(mouseUp));
  75. }
  76. }
  77. });

使体验更好的另一件事是将光标设置为指针,并且disable text selection

  1. [drag-select] {
  2. cursor: pointer;
  3. -webkit-touch-callout: none;
  4. -webkit-user-select: none;
  5. -khtml-user-select: none;
  6. -moz-user-select: none;
  7. -ms-user-select: none;
  8. user-select: none;
  9. }

您还可以在此working demo中看到此操作

猜你在找的Angularjs相关文章