|
| 1 | +--- |
| 2 | +title: Restrict drag zone for the TreeView |
| 3 | +description: An example on how to restrict the drag zone for the Kendo UI TreeView widget. |
| 4 | +type: how-to |
| 5 | +page_title: Restrict drag zone for the TreeView | Kendo UI TreeView |
| 6 | +slug: treeview-restrict-drag-zone |
| 7 | +tags: treeview, restrict, drag, zone |
| 8 | +res_type: kb |
| 9 | +component: treeview |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tr> |
| 16 | + <td>Product</td> |
| 17 | + <td>Kendo UI TreeView</td> |
| 18 | + </tr> |
| 19 | + <tr> |
| 20 | + <td>Kendo UI version</td> |
| 21 | + <td>Created with the 2019.1.115 version</td> |
| 22 | + </tr> |
| 23 | +</table> |
| 24 | + |
| 25 | +## Description |
| 26 | + |
| 27 | +Presently, the drag zone of the TreeView is on the entire node. It would be nice to have the possibility to specify a dragging element in the node instead. |
| 28 | + |
| 29 | +## Solution |
| 30 | + |
| 31 | +Add a dragFilter option to the TreeView by overriding the implementation of the `_draggable` method. |
| 32 | + |
| 33 | +```dojo |
| 34 | + <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/solid.css"> |
| 35 | + <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/fontawesome.css"> |
| 36 | +
|
| 37 | + <div id="treeview"></div> |
| 38 | +
|
| 39 | + <script> |
| 40 | + kendo.ui.TreeView.fn.options.dragFilter = null; |
| 41 | +
|
| 42 | + kendo.ui.TreeView.fn._dragging = function() { |
| 43 | + var enabled = this.options.dragAndDrop; |
| 44 | + var dragging = this.dragging; |
| 45 | + if (enabled && !dragging) { |
| 46 | + var widget = this; |
| 47 | + var dragFilter = 'div:not(.k-state-disabled) .k-in'; |
| 48 | +
|
| 49 | + if (this.options.dragFilter && typeof this.options.dragFilter == 'string') |
| 50 | + { |
| 51 | + dragFilter = dragFilter + ' ' + this.options.dragFilter; |
| 52 | + } |
| 53 | +
|
| 54 | + this.dragging = new kendo.ui.HierarchicalDragAndDrop(this.element, { |
| 55 | + reorderable: true, |
| 56 | + $angular: this.options.$angular, |
| 57 | + autoScroll: this.options.autoScroll, |
| 58 | + filter: dragFilter, |
| 59 | + allowedContainers: '.k-treeview', |
| 60 | + itemSelector: '.k-treeview .k-item', |
| 61 | + hintText: $.proxy(this._hintText, this), |
| 62 | + contains: function (source, destination) { |
| 63 | + return $.contains(source, destination); |
| 64 | + }, |
| 65 | + dropHintContainer: function (item) { |
| 66 | + return item; |
| 67 | + }, |
| 68 | + itemFromTarget: function (target) { |
| 69 | + var item = target.closest('.k-top,.k-mid,.k-bot'); |
| 70 | + return { |
| 71 | + item: item, |
| 72 | + content: target.closest('.k-in'), |
| 73 | + first: item.hasClass('k-top'), |
| 74 | + last: item.hasClass('k-bot') |
| 75 | + }; |
| 76 | + }, |
| 77 | + dropPositionFrom: function (dropHint) { |
| 78 | + return dropHint.prevAll('.k-in').length > 0 ? 'after' : 'before'; |
| 79 | + }, |
| 80 | + dragstart: function (source) { |
| 81 | + return widget.trigger('dragstart', { sourceNode: source[0] }); |
| 82 | + }, |
| 83 | + drag: function (options) { |
| 84 | + widget.trigger('drag', { |
| 85 | + originalEvent: options.originalEvent, |
| 86 | + sourceNode: options.source[0], |
| 87 | + dropTarget: options.target[0], |
| 88 | + pageY: options.pageY, |
| 89 | + pageX: options.pageX, |
| 90 | + statusClass: options.status, |
| 91 | + setStatusClass: options.setStatus |
| 92 | + }); |
| 93 | + }, |
| 94 | + drop: function (options) { |
| 95 | + var dropTarget = $(options.dropTarget); |
| 96 | + var navigationTarget = dropTarget.closest('a'); |
| 97 | + if (navigationTarget && navigationTarget.attr('href')) { |
| 98 | + widget._tempPreventNavigation(navigationTarget); |
| 99 | + } |
| 100 | + return widget.trigger('drop', { |
| 101 | + originalEvent: options.originalEvent, |
| 102 | + sourceNode: options.source, |
| 103 | + destinationNode: options.destination, |
| 104 | + valid: options.valid, |
| 105 | + setValid: function (state) { |
| 106 | + this.valid = state; |
| 107 | + options.setValid(state); |
| 108 | + }, |
| 109 | + dropTarget: options.dropTarget, |
| 110 | + dropPosition: options.position |
| 111 | + }); |
| 112 | + }, |
| 113 | + dragend: function (options) { |
| 114 | + var source = options.source; |
| 115 | + var destination = options.destination; |
| 116 | + var position = options.position; |
| 117 | + function triggerDragEnd(source) { |
| 118 | + if (widget.options.checkboxes && widget.options.checkboxes.checkChildren) { |
| 119 | + widget.updateIndeterminate(); |
| 120 | + } |
| 121 | + widget.trigger('dragend', { |
| 122 | + originalEvent: options.originalEvent, |
| 123 | + sourceNode: source && source[0], |
| 124 | + destinationNode: destination[0], |
| 125 | + dropPosition: position |
| 126 | + }); |
| 127 | + } |
| 128 | + if (position == 'over') { |
| 129 | + widget.append(source, destination, triggerDragEnd); |
| 130 | + } else { |
| 131 | + if (position == 'before') { |
| 132 | + source = widget.insertBefore(source, destination); |
| 133 | + } else if (position == 'after') { |
| 134 | + source = widget.insertAfter(source, destination); |
| 135 | + } |
| 136 | + triggerDragEnd(source); |
| 137 | + } |
| 138 | + } |
| 139 | + }); |
| 140 | + } else if (!enabled && dragging) { |
| 141 | + dragging.destroy(); |
| 142 | + this.dragging = null; |
| 143 | + } |
| 144 | + }; |
| 145 | + </script> |
| 146 | +
|
| 147 | + <script> |
| 148 | + $(function () { |
| 149 | + var dataSource = new kendo.data.HierarchicalDataSource({ |
| 150 | + data: [ |
| 151 | + { text: "Furniture", items: [ |
| 152 | + { text: "Tables & Chairs" }, |
| 153 | + { text: "Sofas" }, |
| 154 | + { text: "Occasional Furniture" } |
| 155 | + ] }, |
| 156 | + { text: "Decor", items: [ |
| 157 | + { text: "Bed Linen" }, |
| 158 | + { text: "Curtains & Blinds" }, |
| 159 | + { text: "Carpets" } |
| 160 | + ] } |
| 161 | + ] |
| 162 | + }); |
| 163 | +
|
| 164 | + $("#treeview").kendoTreeView({ |
| 165 | + dataSource: dataSource, |
| 166 | + dragAndDrop: true, |
| 167 | + dragFilter: ".drag-handler", |
| 168 | + template: $('#treeview-item-template').html() |
| 169 | + }).data("kendoTreeView") |
| 170 | + .templates |
| 171 | + .dragClue = kendo.template($('#treeview-drag-clue-template').html()); |
| 172 | + }); |
| 173 | + </script> |
| 174 | +
|
| 175 | + <script id="treeview-item-template" type="text/x-kendo-template"> |
| 176 | + <div class="drag-handler"><i class="fa fa-grip-vertical fa-fw"></i></div> |
| 177 | + #: item.text # |
| 178 | + </script> |
| 179 | +
|
| 180 | + <script id="treeview-drag-clue-template" type="text/x-kendo-template"> |
| 181 | + #: item.text # |
| 182 | + </script> |
| 183 | +
|
| 184 | + <style> |
| 185 | + .k-treeview .k-i-drag-and-drop { |
| 186 | + margin-left: 7px; |
| 187 | + margin-top: -7px; |
| 188 | + width: auto; |
| 189 | + height: auto; |
| 190 | + } |
| 191 | +
|
| 192 | + .drag-handler { |
| 193 | + display: inline-block; |
| 194 | + color:rgba(0,0,0,.44); |
| 195 | + cursor:grab; |
| 196 | + } |
| 197 | + </style> |
| 198 | +``` |
0 commit comments