Your search did not match any results.

Tree List - Node Drag & Drop

The TreeList allows users to drag and drop nodes. Use the icons in the leftmost column as the drag handles.

Backend API
<div id="treeListDemo"> @(Html.DevExtreme().TreeList() .ID("treeList") .DataSource(new JS("employees")) .KeyExpr("ID") .ParentIdExpr("Head_ID") .ExpandedRowKeys(new JS("[1]")) .ShowRowLines(true) .ShowBorders(true) .ColumnAutoWidth(true) .RowDragging(rd => rd .AllowDropInsideItem(true) .AllowReordering(true) .OnDragChange("onDragChange") .OnReorder("onReorder") .ShowDragIcons(true) ) .Columns(columns => { columns.Add().DataField("Title") .Caption("Position"); columns.Add().DataField("Full_Name"); columns.Add().DataField("City"); columns.Add().DataField("State"); columns.Add().DataField("Mobile_Phone"); columns.Add().DataField("Hire_Date") .DataType(GridColumnDataType.Date); }) ) <div class="options"> <div class="caption">Options</div> <div class="options-container"> <div class="option"> @(Html.DevExtreme().CheckBox() .ID("allowDropInside") .Text("Allow Drop Inside Item") .Value(true) .OnValueChanged(@<text> function(e) { getTreeList().option("rowDragging.allowDropInsideItem", e.value); } </text>) ) </div> <div class="option"> @(Html.DevExtreme().CheckBox() .ID("allowReordering") .Text("Allow Reordering") .Value(true) .OnValueChanged(@<text> function(e) { getTreeList().option("rowDragging.allowReordering", e.value); } </text>) ) </div> <div class="option"> @(Html.DevExtreme().CheckBox() .ID("showDragIcons") .Text("Show Drag Icons") .Value(true) .OnValueChanged(@<text> function(e) { getTreeList().option("rowDragging.showDragIcons", e.value); } </text>) ) </div> </div> </div> </div> <script> function onDragChange(e) { var treeList = e.component, visibleRows = treeList.getVisibleRows(), sourceNode = treeList.getNodeByKey(e.itemData.ID), targetNode = visibleRows[e.toIndex].node; while(targetNode && targetNode.data) { if(targetNode.data.ID === sourceNode.data.ID) { e.cancel = true; break; } targetNode = targetNode.parent; } } function onReorder(e) { var visibleRows = e.component.getVisibleRows(); if (e.dropInsideItem) { e.itemData.Head_ID = visibleRows[e.toIndex].key; } else { var sourceData = e.itemData, toIndex = e.fromIndex > e.toIndex ? e.toIndex - 1 : e.toIndex, targetData = toIndex >= 0 ? visibleRows[toIndex].node.data : null; if (targetData && e.component.isRowExpanded(targetData.ID)) { sourceData.Head_ID = targetData.ID; targetData = null; } else { sourceData.Head_ID = targetData ? targetData.Head_ID : e.component.option('rootValue'); } var sourceIndex = employees.indexOf(sourceData); employees.splice(sourceIndex, 1); var targetIndex = employees.indexOf(targetData) + 1; employees.splice(targetIndex, 0, sourceData); } e.component.refresh(); } function getTreeList() { return $("#treeList").dxTreeList("instance"); } </script> <script src="~/Scripts/data/employees.js"></script>
using System.Web.Mvc; using DevExtreme.MVC.Demos.Models.SampleData; namespace DevExtreme.MVC.Demos.Controllers { public class TreeListController : Controller { public ActionResult LocalReordering() { return View(); } } }
var employees = [{ "ID": 1, "Head_ID": 0, "Full_Name": "John Heart", "Prefix": "Mr.", "Title": "CEO", "City": "Los Angeles", "State": "California", "Email": "[email protected]", "Skype": "jheart_DX_skype", "Mobile_Phone": "(213) 555-9392", "Birth_Date": "1964-03-16", "Hire_Date": "1995-01-15" }, { "ID": 2, "Head_ID": 1, "Full_Name": "Samantha Bright", "Prefix": "Dr.", "Title": "COO", "City": "Los Angeles", "State": "California", "Email": "[email protected]", "Skype": "samanthab_DX_skype", "Mobile_Phone": "(213) 555-2858", "Birth_Date": "1966-05-02", "Hire_Date": "2004-05-24" }, { "ID": 3, "Head_ID": 1, "Full_Name": "Arthur Miller", "Prefix": "Mr.", "Title": "CTO", "City": "Denver", "State": "Colorado", "Email": "[email protected]", "Skype": "arthurm_DX_skype", "Mobile_Phone": "(310) 555-8583", "Birth_Date": "1972-07-11", "Hire_Date": "2007-12-18" }, { "ID": 4, "Head_ID": 1, "Full_Name": "Robert Reagan", "Prefix": "Mr.", "Title": "CMO", "City": "Bentonville", "State": "Arkansas", "Email": "[email protected]", "Skype": "robertr_DX_skype", "Mobile_Phone": "(818) 555-2387", "Birth_Date": "1974-09-07", "Hire_Date": "2002-11-08" }, { "ID": 5, "Head_ID": 1, "Full_Name": "Greta Sims", "Prefix": "Ms.", "Title": "HR Manager", "City": "Atlanta", "State": "Georgia", "Email": "[email protected]", "Skype": "gretas_DX_skype", "Mobile_Phone": "(818) 555-6546", "Birth_Date": "1977-11-22", "Hire_Date": "1998-04-23" }, { "ID": 6, "Head_ID": 3, "Full_Name": "Brett Wade", "Prefix": "Mr.", "Title": "IT Manager", "City": "Reno", "State": "Nevada", "Email": "[email protected]", "Skype": "brettw_DX_skype", "Mobile_Phone": "(626) 555-0358", "Birth_Date": "1968-12-01", "Hire_Date": "2009-03-06" }, { "ID": 7, "Head_ID": 5, "Full_Name": "Sandra Johnson", "Prefix": "Mrs.", "Title": "Controller", "City": "Beaver", "State": "Utah", "Email": "[email protected]", "Skype": "sandraj_DX_skype", "Mobile_Phone": "(562) 555-2082", "Birth_Date": "1974-11-15", "Hire_Date": "2005-05-11" }, { "ID": 8, "Head_ID": 4, "Full_Name": "Ed Holmes", "Prefix": "Dr.", "Title": "Sales Manager", "City": "Malibu", "State": "California", "Email": "[email protected]", "Skype": "edwardh_DX_skype", "Mobile_Phone": "(310) 555-1288", "Birth_Date": "1973-07-14", "Hire_Date": "2005-06-19" }, { "ID": 9, "Head_ID": 3, "Full_Name": "Barb Banks", "Prefix": "Mrs.", "Title": "Support Manager", "City": "Phoenix", "State": "Arizona", "Email": "[email protected]", "Skype": "barbarab_DX_skype", "Mobile_Phone": "(310) 555-3355", "Birth_Date": "1979-04-14", "Hire_Date": "2002-08-07" }, { "ID": 10, "Head_ID": 2, "Full_Name": "Kevin Carter", "Prefix": "Mr.", "Title": "Shipping Manager", "City": "San Diego", "State": "California", "Email": "[email protected]", "Skype": "kevinc_DX_skype", "Mobile_Phone": "(213) 555-2840", "Birth_Date": "1978-01-09", "Hire_Date": "2009-08-11" }, { "ID": 11, "Head_ID": 5, "Full_Name": "Cindy Stanwick", "Prefix": "Ms.", "Title": "HR Assistant", "City": "Little Rock", "State": "Arkansas", "Email": "[email protected]", "Skype": "cindys_DX_skype", "Mobile_Phone": "(818) 555-6655", "Birth_Date": "1985-06-05", "Hire_Date": "2008-03-24" }, { "ID": 12, "Head_ID": 8, "Full_Name": "Sammy Hill", "Prefix": "Mr.", "Title": "Sales Assistant", "City": "Pasadena", "State": "California", "Email": "[email protected]", "Skype": "sammyh_DX_skype", "Mobile_Phone": "(626) 555-7292", "Birth_Date": "1984-02-17", "Hire_Date": "2012-02-01" }, { "ID": 13, "Head_ID": 10, "Full_Name": "Davey Jones", "Prefix": "Mr.", "Title": "Shipping Assistant", "City": "Pasadena", "State": "California", "Email": "[email protected]", "Skype": "davidj_DX_skype", "Mobile_Phone": "(626) 555-0281", "Birth_Date": "1983-03-06", "Hire_Date": "2011-04-24" }, { "ID": 14, "Head_ID": 10, "Full_Name": "Victor Norris", "Prefix": "Mr.", "Title": "Shipping Assistant", "City": "Little Rock", "State": "Arkansas", "Email": "[email protected]", "Skype": "victorn_DX_skype", "Mobile_Phone": "(213) 555-9278", "Birth_Date": "1986-07-23", "Hire_Date": "2012-07-23" }, { "ID": 15, "Head_ID": 10, "Full_Name": "Mary Stern", "Prefix": "Ms.", "Title": "Shipping Assistant", "City": "Beaver", "State": "Utah", "Email": "[email protected]", "Skype": "marys_DX_skype", "Mobile_Phone": "(818) 555-7857", "Birth_Date": "1982-04-08", "Hire_Date": "2012-08-12" }, { "ID": 16, "Head_ID": 10, "Full_Name": "Robin Cosworth", "Prefix": "Mrs.", "Title": "Shipping Assistant", "City": "Los Angeles", "State": "California", "Email": "[email protected]", "Skype": "robinc_DX_skype", "Mobile_Phone": "(818) 555-0942", "Birth_Date": "1981-06-12", "Hire_Date": "2012-09-01" }, { "ID": 17, "Head_ID": 9, "Full_Name": "Kelly Rodriguez", "Prefix": "Ms.", "Title": "Support Assistant", "City": "Boise", "State": "Idaho", "Email": "[email protected]", "Skype": "kellyr_DX_skype", "Mobile_Phone": "(818) 555-9248", "Birth_Date": "1988-05-11", "Hire_Date": "2012-10-13" }, { "ID": 18, "Head_ID": 9, "Full_Name": "James Anderson", "Prefix": "Mr.", "Title": "Support Assistant", "City": "Atlanta", "State": "Georgia", "Email": "[email protected]", "Skype": "jamesa_DX_skype", "Mobile_Phone": "(323) 555-4702", "Birth_Date": "1987-01-29", "Hire_Date": "2012-10-18" }, { "ID": 19, "Head_ID": 9, "Full_Name": "Antony Remmen", "Prefix": "Mr.", "Title": "Support Assistant", "City": "Boise", "State": "Idaho", "Email": "[email protected]", "Skype": "anthonyr_DX_skype", "Mobile_Phone": "(310) 555-6625", "Birth_Date": "1986-02-19", "Hire_Date": "2013-01-19" }, { "ID": 20, "Head_ID": 8, "Full_Name": "Olivia Peyton", "Prefix": "Mrs.", "Title": "Sales Assistant", "City": "Atlanta", "State": "Georgia", "Email": "[email protected]", "Skype": "oliviap_DX_skype", "Mobile_Phone": "(310) 555-2728", "Birth_Date": "1981-06-03", "Hire_Date": "2012-05-14" }, { "ID": 21, "Head_ID": 6, "Full_Name": "Taylor Riley", "Prefix": "Mr.", "Title": "Network Admin", "City": "San Jose", "State": "California", "Email": "[email protected]", "Skype": "taylorr_DX_skype", "Mobile_Phone": "(310) 555-7276", "Birth_Date": "1982-08-14", "Hire_Date": "2012-04-14" }, { "ID": 22, "Head_ID": 6, "Full_Name": "Amelia Harper", "Prefix": "Mrs.", "Title": "Network Admin", "City": "Los Angeles", "State": "California", "Email": "[email protected]", "Skype": "ameliah_DX_skype", "Mobile_Phone": "(213) 555-4276", "Birth_Date": "1983-11-19", "Hire_Date": "2011-02-10" }, { "ID": 23, "Head_ID": 6, "Full_Name": "Wally Hobbs", "Prefix": "Mr.", "Title": "Programmer", "City": "Chatsworth", "State": "California", "Email": "[email protected]", "Skype": "wallyh_DX_skype", "Mobile_Phone": "(818) 555-8872", "Birth_Date": "1984-12-24", "Hire_Date": "2011-02-17" }, { "ID": 24, "Head_ID": 6, "Full_Name": "Brad Jameson", "Prefix": "Mr.", "Title": "Programmer", "City": "San Fernando", "State": "California", "Email": "[email protected]", "Skype": "bradleyj_DX_skype", "Mobile_Phone": "(818) 555-4646", "Birth_Date": "1988-10-12", "Hire_Date": "2011-03-02" }, { "ID": 25, "Head_ID": 6, "Full_Name": "Karen Goodson", "Prefix": "Miss", "Title": "Programmer", "City": "South Pasadena", "State": "California", "Email": "[email protected]", "Skype": "kareng_DX_skype", "Mobile_Phone": "(626) 555-0908", "Birth_Date": "1987-04-26", "Hire_Date": "2011-03-14" }, { "ID": 26, "Head_ID": 5, "Full_Name": "Marcus Orbison", "Prefix": "Mr.", "Title": "Travel Coordinator", "City": "Los Angeles", "State": "California", "Email": "[email protected]", "Skype": "marcuso_DX_skype", "Mobile_Phone": "(213) 555-7098", "Birth_Date": "1982-03-02", "Hire_Date": "2005-05-19" }, { "ID": 27, "Head_ID": 5, "Full_Name": "Sandy Bright", "Prefix": "Ms.", "Title": "Benefits Coordinator", "City": "Denver", "State": "Colorado", "Email": "[email protected]", "Skype": "sandrab_DX_skype", "Mobile_Phone": "(818) 555-0524", "Birth_Date": "1983-09-11", "Hire_Date": "2005-06-04" }, { "ID": 28, "Head_ID": 6, "Full_Name": "Morgan Kennedy", "Prefix": "Mrs.", "Title": "Graphic Designer", "City": "San Fernando Valley", "State": "California", "Email": "[email protected]", "Skype": "morgank_DX_skype", "Mobile_Phone": "(818) 555-8238", "Birth_Date": "1984-07-17", "Hire_Date": "2012-01-11" }, { "ID": 29, "Head_ID": 28, "Full_Name": "Violet Bailey", "Prefix": "Ms.", "Title": "Jr Graphic Designer", "City": "La Canada", "State": "California", "Email": "[email protected]", "Skype": "violetb_DX_skype", "Mobile_Phone": "(818) 555-2478", "Birth_Date": "1985-06-10", "Hire_Date": "2012-01-19" }, { "ID": 30, "Head_ID": 5, "Full_Name": "Ken Samuelson", "Prefix": "Dr.", "Title": "Ombudsman", "City": "St. Louis", "State": "Missouri", "Email": "[email protected]", "Skype": "kents_DX_skype", "Mobile_Phone": "(562) 555-9282", "Birth_Date": "1972-09-11", "Hire_Date": "2009-04-22" }];
#treeList { max-height: 440px; } .dx-sortable-dragging { opacity: 0.9 } .options { margin-top: 20px; padding: 20px; background-color: rgba(191, 191, 191, 0.15); position: relative; } .caption { font-size: 18px; font-weight: 500; } .option { margin-top: 10px; margin-right: 40px; display: inline-block; } .option:last-child { margin-right: 0; } .options-container { display: flex; align-items: center; }

To configure node drag and drop, follow these steps:

  1. Allow users to reorder nodes
    In the rowDragging object, set the allowReordering property to true. You can use the "Allow Reordering" checkbox under the TreeList to try this property.

  2. Allow users to change the node hierarchy
    Enable the allowDropInsideItem property to allow users to drop one node onto another, thus adding it as the target node's child. If the property is disabled, users can only drop nodes in between others. You can use the "Allow Drop Inside Item" checkbox under the TreeList to try this property.

  3. Reorder nodes in code
    In the onReorder function, change the parent ID of the reordered node and the node's index in the data array. If the node was dropped onto another node, changing the parent ID is sufficient. Refresh() the TreeList after these manipulations.

  4. Prevent a node from being moved inside its child node
    Moving a parent node inside its own child node breaks the hierarchy. Prevent this situation in the onDragChange function. Cycle through the target node's parents. If the parent's and source node's IDs are equal, cancel the ability to drop the node.

  5. Show or hide drag icons (optional)
    Users use the icons as drag handles. To hide them, set the showDragIcons property to false. In this case, the drag handles are the rows themselves. Use the "Show Drag Icons" checkbox under the TreeList to try this property.

OSZAR »