简体   繁体   English

编辑orderby-property时,AngularJS orderby不起作用

[英]AngularJS orderby doesn't work when orderby-property is edited

I have a list of objects in my scope and want to iterate over them, show some of their properties in ordered-by-some-property way and change them. 我在我的范围内有一个对象列表,并希望迭代它们,以有序的属性方式显示它们的一些属性并更改它们。

ng-repeat is used to display textboxes binded to each object of my list and orderby filter which takes "position" as a parameter is applied. ng-repeat用于显示绑定到列表中每个对象的文本框,以及使用“position”作为参数的orderby过滤器。

Once again, position is also editable! 再次,位置也是可编辑的!

Now we change position of certain object once(angular reorders the list as expected) and then change twice. 现在我们改变某个对象的位置一次(角度按预期重新排列列表),然后改变两次。 Angular does not reorder the list. Angular不会重新排序列表。

Could anyone explain how it is possible to fix this reorder-only-once situation and what are the reasons for such a behaviour? 任何人都可以解释如何修复这种仅重新排序的情况以及这种行为的原因是什么?

Here is fiddle: JSFiddle 这是小提琴: JSFiddle

HTML HTML

<div ng-controller="MyCtrl">
    <p>List of activities:</p>
    <div ng-repeat="activity in model.activities | orderBy: 'position'">
        <textarea type="text" ng-model="activity.name">

        </textarea>
        {{activity.name}}
        <input type="text" ng-model="activity.position">
    </div>
</div>

JS JS

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.model = { 
        activities: [
            {name: "activity 1", position: 1}, 
            {name: "activity 2", position: 2}, 
            {name: "activity 3", position: 3}, 
            {name: "activity 4", position: 4}, 
            {name: "activity 5", position: 5}
        ] 
    };
}

Here it goes. 在这里。

Why does the code behave like that? 为什么代码表现得那样?

position is bound to a text input. position被绑定到text输入。 That makes the text integers at first and then translated into Strings as soon as you start to type. 这首先使文本整数,然后在您开始输入时立即转换为字符串。 Then the ordering becomes odd and that's why it fails. 然后排序变得奇怪,这就是它失败的原因。

How do I fix this? 我该如何解决?

You have two options. 你有两个选择。 First option is to change type="text" by type="number" then it'll work. 第一个选项是通过type="number"更改type="text" type="number"然后它将起作用。 If that's not an option you can create a simple directive (Angular 1.2 onwards) 如果这不是一个选项,你可以创建一个简单的指令(Angular 1.2以上)

myApp.directive('integer', function() {
    return {
        require: 'ngModel',
        link: function(scope, el, attr, ctrl){
            ctrl.$parsers.unshift(function(viewValue){
                return parseInt(viewValue, 10);
            });
        }
    };
});

What that code does is to parse every value into an integer making it work. 该代码的作用是将每个值解析为一个整数,使其工作。

It occured because your position property is binded to the text value. 它发生的原因是您的position属性绑定到文本值。 It means that your position's value becomes the string after the editing through the input. 这意味着在通过输入进行编辑后,您的位置值将成为字符串。

You need to create custom sorting. 您需要创建自定义排序。

For example: 例如:

<div ng-repeat="activity in getOrderedData()">
    {{activity.name}}
    <input type="text" ng-model="activity.position">
</div>

$scope.getOrderedData = function(){
    return $scope.model.activities.sort(compare);
};

var compare = function(a,b){
    if (parseInt(a.position) < parseInt(b.position))
         return -1;
    if (parseInt(a.position) > parseInt(b.position))
        return 1;
    return 0;
};

This will work with every AngularJS version. 这适用于每个AngularJS版本。

Please see this JSFiddle for the whole working example. 有关整个工作示例,请参阅此JSFiddle

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM