Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solving the issue of sorting (issue #18) #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ $scope.shinyThings = function (item) {
```

## Example
See [`example.html`](http://htmlpreview.github.io/?https://github.com/btford/angular-dragon-drop/blob/master/example.html).
See [`example.html`](http://htmlpreview.github.io/?https://github.com/shootermv/angular-dragon-drop/blob/master/example.html).

## License
MIT
27 changes: 21 additions & 6 deletions dragon-drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
});
}

Expand All @@ -171,7 +186,7 @@ angular.module('btford.dragon-drop', []).

// get the `thing in things` expression
var expression = attr.btfDragon;
var match = expression.match(/^\s*(.+)\s+in\s+(.*?)\s*$/);
var match = expression.match(/^\s*(.+)\s+in\s+(.*?)\s*(?:\s+\|\s+(.*))?$/);
if (!match) {
throw Error("Expected btfDragon in form of '_item_ in _collection_' but got '" +
expression + "'.");
Expand Down Expand Up @@ -278,4 +293,4 @@ angular.module('btford.dragon-drop', []).
};
}
};
});
});
39 changes: 37 additions & 2 deletions example.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
<html>
<head>
<title>Dragon Drop for AngularJS</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootswatch/2.3.1/spruce/bootstrap.min.css">
<link rel="stylesheet" href="lib/bootstrap.min.css">
<style>
[btf-dragon] {
padding: 20px;
border: 1px solid red;
}
[btf-dragon].sortable span{
display:block;
}
</style>
</head>
<body ng-app="ExampleApp">
Expand Down Expand Up @@ -44,15 +47,47 @@ <h3>Other Things</h3>
</div>
</div>

<h3>Sorting Support:</h3>
<div style="border:2px solid blue"></div>




<div class="row">
<div class="span6">
<h3>sortableThings</h3>
<div class="sortable" btf-dragon="thing in sortablethings | orderBy :'id'">{{thing.name}}</div>
</div>
<div class="span6">
<h3>Other Things</h3>
<div class="sortable" btf-dragon="thing in sortableotherThings | orderBy :'id':false">{{thing.name}}</div>
</div>
</div>

<hr>

<div class="row">
<div class="span6">
<h3>sortableThings</h3>
<pre>{{sortablethings | json}}</pre>
</div>
<div class="span6">
<h3>Other Things</h3>
<pre>{{sortableotherThings | json}}</pre>
</div>
</div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script>
<script src="lib/angular.min.js"></script>
<script src="dragon-drop.js"></script>
<script>
angular.module('ExampleApp', ['btford.dragon-drop']).
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 = [];
});
</script>
</body>
Expand Down
Loading