diff --git a/dragon-drop.js b/dragon-drop.js index 3222953..7d6217d 100644 --- a/dragon-drop.js +++ b/dragon-drop.js @@ -62,9 +62,18 @@ angular.module('btford.dragon-drop', []). } }; - var add = function (collection, item, key) { + var add = function (collection, item, key, orderBy) { if (collection instanceof Array) { collection.push(item); + /*sorting support*/ + if(orderBy){ + + collection = collection.sort(function(a, b){ + a = parseInt(a[orderBy]); + b = parseInt(b[orderBy]); + return a - b; + }) + } } else { collection[key] = item; } @@ -146,17 +155,23 @@ angular.module('btford.dragon-drop', []). if (dropArea.length > 0) { var expression = dropArea.attr('btf-dragon'); var targetScope = dropArea.scope(); - var match = expression.match(/^\s*(.+)\s+in\s+(.*?)\s*$/); + //added "(?:\s+\|\s+(.*))?" for support orderBy + var match = expression.match(/^\s*(.+)\s+in\s+(.*?)\s*(?:\s+\|\s+(.*))?$/); + + var targetList = targetScope.$eval(match[2]); + var orderBy; + if(match[3]){orderBy = match[3].split(':')[1];} + targetScope.$apply(function () { - add(targetList, dragValue, dragKey); + add(targetList, dragValue, dragKey, orderBy); }); } else if (!dragDuplicate) { // no dropArea here // put item back to origin $rootScope.$apply(function () { - add(dragOrigin, dragValue, dragKey); + add(dragOrigin, dragValue, dragKey, orderBy); }); } @@ -278,4 +293,4 @@ angular.module('btford.dragon-drop', []). }; } }; - }); + }); \ No newline at end of file diff --git a/example.html b/example.html index b8bbad0..72d3a3e 100644 --- a/example.html +++ b/example.html @@ -8,6 +8,9 @@ padding: 20px; border: 1px solid red; } + [btf-dragon].sortable span{ + display:block; + } @@ -44,6 +47,35 @@

Other Things

+

Sorting Support:

+
+ + + + +
+
+

sortableThings

+
{{thing.name}}
+
+
+

Other Things

+
{{thing.name}}
+
+
+ +
+ +
+
+

sortableThings

+
{{sortablethings | json}}
+
+
+

Other Things

+
{{sortableotherThings | json}}
+
+
@@ -53,6 +85,9 @@

Other Things

controller('MainCtrl', function ($scope) { $scope.things = ['one', 'two', 'three']; $scope.otherThings = []; + + $scope.sortablethings = [{name:'one',id:3}, {name:'two',id:2}, {name:'three',id:1}]; + $scope.sortableotherThings = []; });