Disabling orderBy in AngularJS while editing the list

前端 未结 8 653
情歌与酒
情歌与酒 2021-01-07 23:06

After applying orderBy in my list of input boxes if i edit any field it starts sorting immediately and i loose focus. I tried to dug in the angular code and fou

相关标签:
8条回答
  • 2021-01-08 00:03

    You can freeze the current ordering while you are editing. Say your html looks like this:

    <tbody ng-repeat="item in items | orderBy:orderBy:reverse">
        <tr ng-click="startEdit()">
          <td>{{item.name}}</td>
        </tr>
    </tbody>
    

    In your controller you write:

    var savedOrderBy, savedReverse;
    $scope.startEdit() = function() {
        $scope.items = $filter('orderBy')($scope.items, $scope.orderby, $scope.reverse);
    
        for (var i = 0; i < $scope.items.length; ++i) {
            if (i < 9999) { 
                $scope.items[i]['pos'] = ("000" + i).slice(-4); 
            }
        }
    
        savedOrderBy = $scope.orderBy;
        savedReverse = $scope.reverse;
        $scope.orderBy = 'pos';
        $scope.reverse = false;
    };
    

    Before the user starts editing, you first sort the current items in exactly the same order that they currently appear in the page. You do that by calling the orderBy $filter() with the current sorting parameters.

    Then you go over your - now sorted - items, and add an arbitrary property (here "pos") and set it to the current position. I zero-pad it so that position 0002 collates before 0011. Maybe that is not necessary, no idea.

    You normally want to remember the current ordering, here in the scope variables "savedOrder" and "savedReverse".

    And finally you tell angular to sort by that new property "pos" and voilà the table order is frozen, because that property simply does not change while editing.

    When you are done editing, you have to do the opposite. You restore the old ordering from the scope variables "savedOrder" and "savedReverse":

    $scope.endEdit = function() {
        $scope.orderBy = savedOrderBy;
        $scope.reverse = reverse;
    };
    

    If the order of the $scope.items array matters for you, you would also have to sort it again to its original ordering.

    0 讨论(0)
  • 2021-01-08 00:07

    Try using a scope variable to change the order. In this case, when you are going to order, you call a function in your controller that changes the variable order value to the field you want to order by, and when you are editing, you reset it.

    Example:

    <li ng-repeat="item in filtered = (items | filter:search) |  orderBy:order:rever" >
    

    So here we have the variable "order" to tell the ng-repeat by which field the list must be ordered. A button calls a function in the controller that changes the "order" value.

    <button type="button" id="orderName" click="changeOrder('item.name')" >Name order </button>
    
    <button type="button" id="orderDate" click="changeOrder('item.date')" >Date order </button>`
    

    And then, in the changeOrder function

    $scope.order = param;
    

    Where 'param' is the field you want to order by. If you don't do anything else, you are going to have the same problem you had, so then, after you have assigned the correct value to the order variable you go

    $scope.order = "";
    

    Which resets the ordering. The result is that the ordering is just going to be effective when the button is pressed and then it wont order again unless you press the button again. This can be changed so it orders again when, for example, you have finished editing an item, as you wanted.

    0 讨论(0)
提交回复
热议问题