【问题标题】:Parse + Angular orderBy IssueParse + Angular orderBy 问题
【发布时间】:2015-05-05 00:14:47
【问题描述】:

我在 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>

当我 orderBy 'id' 或 'createdAt' 排序工作正常。如何按总计、税金或 paymentType 进行订购?

【问题讨论】:

  • 所讨论的对象具有total、tax、paymentType 作为属性,因为它是一个对象。

标签: javascript angularjs parse-platform angularjs-orderby


【解决方案1】:

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

因此,在 Angular 中,一种方法是创建一个扩展主干对象并提供本机 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 的属性。例如,服务类是放置承诺返回查询的好地方。

【讨论】:

    【解决方案2】:

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

    plainObject = parseObject.toJSON();
    

    【讨论】:

      猜你喜欢
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      相关资源
      最近更新 更多