繁体   English   中英

Parse + Angular orderBy Issue

[英]Parse + Angular orderBy Issue

我在Angular中有一个ng-repeat表。 数据来自Parse。

    <tr dir-paginate="ticket in tickets | orderBy:sortType:sortReverse | itemsPerPage: 10">
        <td><input type="checkbox" class="checkthis" /></td>
        <td>{{ ticket.id }}</td>
        <td>${{ ticket.get("total").toFixed(2) }}</td>
        <td>${{ ticket.get("tax").toFixed(2) }}</td>
        <td>{{ ticket.get("paymentType") }}</td>
        <td>{{ ticket.createdAt | date: 'short' }}</td>
    </tr>

当我命令'id'或'createdAt'时,排序正常。 如何按总额,税金或paymentType进行订购?

解析对象的属性是通过get()访问的,我认为orderBy: filter依赖于具有本机getter。

因此,在角度方面,一种方法是创建一个扩展骨干对象的服务,并提供本机的getter和setter:

(function() {
    'use strict';
    angular.module('myApp.services').factory('MyClass', f);

    function f() {

        var MyClass = Parse.Object.extend("MyClass", {
            // instance methods

            // manually built getter like this
            attributeA : function() {
                return this.get("attributeA");
            },

        }, {
            // class methods    
        });

        // or code-built getters/setters like this
        _.each(["attributeB", "attributeC"], function(p) {
            Object.defineProperty(MyClass.prototype, p, {
                get: function() {return this.get(p);},
                set: function(aValue) {this.set(p, aValue);}
            });
        });
        return MyClass;
    }
})();

无论如何,即使您不需要orderBy的属性,这也许是一种很好的做法。 例如,服务类是放置承诺返回查询的好地方。

如果您的数据来自Parse JavaScript SDK,您可以将它们作为解析对象接收,您可以通过.toJSON()方法将其转换为更适合AngularJS的普通对象:

plainObject = parseObject.toJSON();

暂无
暂无

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

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