-
Notifications
You must be signed in to change notification settings - Fork 441
Best way to order items #172
Comments
Just to clarify, here is how I would hope to write this code: <!DOCTYPE html>
<html ng-app="todos">
<head>
<meta charset="utf-8">
<title>Todos</title>
</head>
<body>
<h2>Todos</h2>
<div ng-controller="TodoCtrl">
<ul ng-model="todos" ui-sortable="sortableOptions">
<li ng-repeat="todo in todos | orderBy:'i'">
<span style="cursor:move;">{{todo.txt}}</span>
</li>
</ul>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script>
<script src="https://rawgithub.com/angular-ui/ui-sortable/master/src/sortable.js"></script>
<script>
angular.module('todos', ['ui.sortable'])
.controller('TodoCtrl', function($scope) {
$scope.todos = [{txt: 'two', i: 1}, {txt: 'one', i: 0}, {txt: 'three', i: 2}]
// uncomment the following code for example to work
// .sort(function (a, b) {
// return a.i > b.i;
// });
$scope.sortableOptions = {
stop: function(e, ui) {
for (var index in $scope.todos) {
$scope.todos[index].i = index;
}
},
axis: 'y'
};
});
</script>
</body>
</html> The problem is that on the first drag, the ordering goes back to the original order. Subsequent ordering appears to work properly. If we then uncomment the following lines: // .sort(function (a, b) {
// return a.i > b.i;
// }); everything appears to work properly, but when we then work with angular bindings that initially load this scope data behind the scenes, we probably won't have access to perform this initial sort. Is there a better way to handle this in ui-sortable? |
First of all, lets assume that we can build (more on this later) a directive that changes it's behavior based on a view-level filter ( Moreover, take a look at tips 2&3 of section Tuning guidelines in this article. I have created two pens, based on the code from your posts. Lets call it penA and penB. You can see that penB is somewhat broken. Moving element
As a result the scope variable provided to ng-model and the repeater should be the same (and not have any view-level filter applied to it). Let's create penB1, to be the result of adding Comparing the two working approaches, penA to penB2 (and it's intermediate steps) we can deduce that they are somewhat equivalent (in logic and behavior):
Possible implementation of inferring the ordering from the view will be discussed later today... Have to go now, sorry :) |
Not yet implemented but possible ways to work this out. Solution 1 $scope.sortableOptions = {
uiOrderBy: 'i',
axis: 'y'
}; Pros:
Cons:
Solution 2
Cons:
|
Here is a pen using a proof-of-concept fork of Summarizing:
This proof-of-concept works with at most one |
This is great! Just to clarify, @thgreasi does this mean that you'll be merging the code from proof-of-concept fork into the master? |
This was just a draft and a proof that its possible. The code currently has some duplications and its performance is by definition lower than the master branch. Moreover, people will start asking to be able to combine ANY number of filters and ANY kind of them (not just
|
Will you be adding this to you list of things to do? If not, how would you like to handle this? It would be a shame to see this feature die here. You are far more knowledgable about this technology and how to use it properly. I could try to finish the implementation, but I have a feeling that you could devise a much better implementation and in far less time than me. |
I want to start off by saying that I really appreciate the work that has been done on this project!
I've searched the issues and #113 and #70 are somewhat relevant, but don't answer my question exactly and the answer is something that I consider to be very important about this project.
I'm trying to contribute to the hoodie project which is basically an open source version of firebase. I'm using the hoodie angular binding.
As far as I know, to support sorting via ui-sortable we need to modify the hoodie angular binding, which I proposed via elmarburke/hoodie-plugin-angularjs#14, however as per the comments, I agree that sorting should really be handled via angular filters.
To make this specific to the context of ui-sortable only, perhaps someone can tell me if the following code presents the only way to currently support this type of sorting in ui-sortable.
If I am correct and this is the only way, then is it on the roadmap for ui-sortable to support angular filters? If not, should I consider adding it ui-sortable?
The text was updated successfully, but these errors were encountered: