5

I have this json structure:

{  
   "items":[  
      {  
         "category":"Category 1",
         "name":"01",
         "id":"46701a72410c"
      },
      {  
         "category":"Category 2",
         "name":"02",
         "id":"9a18ffe9f148"
      },
      {  
         "category":"Category 2",
         "name":"03",
         "id":"de3a49a458cc"
      },
      {  
         "category":"Category 3",
         "name":"04",
         "id":"bb06b1eec9c8"
      },
      {  
         "category":"Category 3",
         "name":"05",
         "id":"92973f4cfbfc"
      },
      {  
         "category":"Category 3",
         "name":"06",
         "id":"b06bbb86d278"
      }
   ],
   "categories":[  
      {  
         "isCollapsed":false,
         "name":"Category 1"
      },
      {  
         "isCollapsed":false,
         "name":"Category 2"
      },
      {  
         "isCollapsed":false,
         "name":"Category 3"
      }
   ]
}

and I'm trying to add sorting behavior using angularjs ui.sortable. I want both categories and items to be sortable.

I created two nested ordered lists based on this json, but I have no idea how to solve sortable settings. Is it possible for this json structure?

With these settings I solved only categories sorting to work. The problem is when items are moved (wrong positions or undefined are taken).

$scope.sortableOptionsCategories = {
    stop: function(ev, ui) {
      console.log("Category moved.");
    }
  };

$scope.sortableOptionsItems = {
  connectWith: '.items-container',
  start: function(e, ui) {
    $(this).attr('data-previndex', ui.item.index());
    console.log("Start: from position " + ui.item.index());
  },

  update: function(e, ui) {
    var newIndex = ui.item.index();
    var oldIndex = $(this).attr('data-previndex');
    $(this).removeAttr('data-previndex');
    console.log("Update: " + oldIndex + " -> " + newIndex);
  },

  stop: function(ev, ui) {
    console.log("Item moved.");
  }
};

UPDATE: Here is my code: http://codepen.io/anon/pen/LpGmeY A solution that keeps the json as it is would perfect for me, but if not possible I will accept any other solution.

3
  • 1
    Can you post this in a codepen or Plunkr including the HTML? Commented Sep 11, 2015 at 12:04
  • 1
    Can you extend and post any ui-sortable codepen samples with your code? Commented Sep 11, 2015 at 12:31
  • I updated with my code. Commented Sep 15, 2015 at 13:09

2 Answers 2

1
+50

Have you tried using this library - https://github.com/angular-ui-tree/angular-ui-tree

I created a jsfiddle - http://jsfiddle.net/450fk7wp/5/ - that handles the nested, sortable, draggable items.

You will have to transform your JSON so that it looks like this:

$scope.rows = [{
  "name":"Category 1",
  "columns": [
    {
     "category":"Category 1",
     "name":"01",
     "id":"46701a72410c"
    }
  ],
}, {
  "name":"Category 2",
  "columns": [
    {  
       "category":"Category 2",
       "name":"02",
       "id":"9a18ffe9f148"
    },
    {  
       "category":"Category 2",
       "name":"03",
       "id":"de3a49a458cc"
    }
  ],
}, {
  "name":"Category 3",
  "columns": [
  {  
     "category":"Category 3",
     "name":"04",
     "id":"bb06b1eec9c8"
  },
  {  
     "category":"Category 3",
     "name":"05",
     "id":"92973f4cfbfc"
  },
  {  
     "category":"Category 3",
     "name":"06",
     "id":"b06bbb86d278"
  }
  ]
}];

Just not exactly sure what type of sorting functionality you are looking for.

Hope this helps!

Sign up to request clarification or add additional context in comments.

4 Comments

I will try. If someone gives me an example using my json structure I will accept his answer.
Posted example above.
Perfect. :) I will use it without dropped function. I want to have items sortable, too.
To be fixed: prevent drop categories in items lists and items in category list. I think this must be simple. I will take a look to available options.
1

Provided I've understood this correctly you have nested lists, are you using nested ng-repeats?

If so it seems you can't do this with sortable (from https://github.com/angular-ui/ui-sortable)

  • ng-model is required, so that the directive knows which model to update.
  • ui-sortable element should only contain one ng-repeat and not any other elements (above or below).

Can you paste your HTML?

1 Comment

Code added. Please take a look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.