Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9159287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:27:19+00:00 2026-06-17T13:27:19+00:00

I want to create a tree like structure where the user can drag and

  • 0

I want to create a tree like structure where the user can drag and drop leaves. I have a starting point as follows:

HTML

<div ng:controller="controller">
  <ul ui-sortable ng-model="items" ui-options="{connectWith: '.item'}" class="item">
    <li ng-repeat="item in items" class="item">
      {{ item.name }}
      <ul ui-sortable ng-model="item.children" ui-options="{connectWith: '.item'}" class="item">
        <li ng-repeat="item in item.children" class="item">{{ item.name }}</li>
      </ul>
    </li>
  </ul>

  <pre>{{ items | json }}</pre>
</div>

<script src="http://code.angularjs.org/1.0.2/angular.min.js"></script>
<script src="https://raw.github.com/angular-ui/angular-ui/master/build/angular-ui.min.js"></script>

CoffeeScript

myapp = angular.module 'myapp', ['ui']

myapp.controller 'controller', ($scope) ->

    $scope.items = [
      {id: 1, name: 'Item 1', children: [
        {id: 5, name: 'SubItem 1.1', children: [
          {id: 11, name: 'SubItem 1.1.1', children: []},
          {id: 12, name: 'SubItem 1.1.2', children: []}
        ]},
        {id: 6, name: 'SubItem 1.2', children: []}
      ]},
      {id: 2, name: 'Item 2', children: [
        {id: 7, name: 'SubItem 2.1', children: []},
        {id: 8, name: 'SubItem 2.2', children: []}
        {id: 9, name: 'SubItem 2.3', children: []}
      ]},
      {id: 3, name: 'Item 3', children: [
        {id: 10, name: 'SubItem 3.1', children: []}
      ]}
    ]

angular.bootstrap document, ['myapp']

The code is in this JSFiddle as well: http://jsfiddle.net/bESrf/1/

On my “real” code, instead of only having one level for children, I extracted the second <ul> into a template and rendered it recursively, which works fine, but I couldn’t find a way to do it in JSFiddle.

What would be the best way to render it recursively and still allow dragging and dropping that would change the array of objects and sub-objects represented by ng-model?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:27:21+00:00Added an answer on June 17, 2026 at 1:27 pm

    Take a look at this example: http://jsfiddle.net/furf/EJGHX/

    I just completed this solution so it is not yet properly documented, but you should be able to mine it for your solution.

    You will need to use a few things:

    1. the ezTree directive – to render the tree
    2. Manuele J Sarfatti’s nestedSortable plugin for jQuery UI
    3. (optional) the uiNestedSortable directive – to enable nestedSortable from your template.
    4. controller code for updating your model – refer to $scope.update

    Using the ezTree directive

    Given a recursive data structure:

    $scope.data = {
      children: [{
        text: 'I want to create a tree like structure...',
        children: [{
          text: 'Take a look at this example...',
          children: []
        }]
      }]
    };
    

    This template will build the tree:

    <ol>
      <li ez-tree="child in data.children at ol">
        <div>{{item.text}}</div>
        <ol></ol>
      </li>
    </ol>
    

    The ez-tree expression should be written as item in collection at selector where item is the iterated child (ala ng-repeat), collection is the root-level collection, and selector is the CSS selector for the node inside the template where the directive should recurse. The name of the terminal property of the collection, in this case children will be used to recurse the tree, in this case child.children. This could be rewritten to be configurable but I’ll leave that as an exercise for the reader.

    Using uiNestedSortable directive

    <ol ui-nested-sortable="{ listType: 'ol', items: 'li', doNotClear: true }"
      ui-nested-sortable-stop="update($event, $ui)">
    </ol>
    

    The ui-nested-sortable attribute should contain a JSON configuration for the nestedSortable plugin. The plugin requires that you specify listType and items. My solution requires that doNotClear be true. Assign callbacks to events using ui-nested-sortable-*eventName*. My directive supplies optional $event and $ui arguments to callbacks. Refer to nestedSortable’s documentation for other options.

    Updating your model

    There is more than one way to skin this cat. Here’s mine. On the stop event, it extracts the child property of the element’s scope to determine which object was moved, the child property of the element’s parent’s scope to determine the destination of the object, and the position of the element to determine the position of the object at its destination. It then walks the data structure and removes the object from its original position and inserts it into its new position.

    $scope.update = function (event, ui) {
    
      var root = event.target,
        item = ui.item,
        parent = item.parent(),
        target = (parent[0] === root) ? $scope.data : parent.scope().child,
        child = item.scope().child,
        index = item.index();
    
      target.children || (target.children = []);
    
      function walk(target, child) {
        var children = target.children,
          i;
        if (children) {
          i = children.length;
          while (i--) {
            if (children[i] === child) {
              return children.splice(i, 1);
            } else {
              walk(children[i], child)
            }
          }
        }
      }
      walk($scope.data, child);
    
      target.children.splice(index, 0, child);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create tree structure using web service. I have used bottom up
I have a tree structure in an sql table like so: CREATE TABLE containers
Can We create tree structure in UITableView like below. -Sub1 //First row -Sub1sub1 //First
I want to create a completely generic treeview like structure. some thing like this:
I have a tree-like object structure that consists of two types of objects: object
I have a tree like structure created out of models using ForeignKey linkages. As
I want to have a map that permits a tree-like behaviour. I want to
I'm using Core Data and I have a file-folder tree-like structure. So I've created
I want to create tree structure with annotation @Retention(RetentionPolicy.RUNTIME) public @interface MyNode { String
Using a PostgreSQL 8.4.14 database, I have a table representing a tree structure like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.