【问题标题】:Sort a store data based on inner JSON field sencha根据内部 JSON 字段 sencha 对商店数据进行排序
【发布时间】:2014-05-25 12:39:49
【问题描述】:

我有一个格式如下的 json:

{
 "collection": [
 {
  "id": 4, 
  "tickets": [
      {
        "price": 40,
      },
      {
        "price": 50,
      }
    ],
  },
  {
  "id": 1, 
  "tickets": [
      {
        "price": 10,
      },
      {
        "price": 15,
      }
    ]
  },
 ]
}

商店:

Ext.define("myProject.store.ABCs", {
extend: "Ext.data.Store",
config: {
    model: "myProject.model.ABC",
    autoLoad: false,            
    proxy: {
        type: "ajax",
        url: '',  //myURL
        reader: {
            type: "json",
            rootProperty: "collection",   // this is the first collection
        },
    },

 }
});

对于这个特定的 JSON,我将模型创建为:

Ext.define("myProject.model.ABC", {
extend: "Ext.data.Model",
config: {
    idProperty: "id",
    fields:[
        {name: "id", type: "int" },
    ],
    hasMany: [
        {
            model: "myProject.model.XYZ",
            name: "tickets",
            associationKey: "tickets",
        },
    ],
  }
});

第二个模型为:

Ext.define("myProject.model.XYZ", {
extend: "Ext.data.Model",
config: {
    // idProperty: "id",
    fields:[
     {name: "inner_id", type: "int" },

    ],
     belongsTo: 'myProject.model.ABC'
 }
});

此特定代码创建一个商店并正确填充模型。

var store = Ext.getStore('ABCs');

现在我想根据 store.tickets().getAt(0).get('price') 对这家商店进行排序,这是根据 XYZ 的第一个价格属性对 ABC 记录进行排序。

在上面的json中。 ABC 记录将是:[{id:4}, {id:1}] 但由于 XYZ 的第一个价格(40 > 10),我想对它们进行排序并创建 [{id:1}, {id:4}]

【问题讨论】:

  • 这是 Ext JS 还是 Sencha Touch?什么版本?
  • 煎茶触摸。 2.3版

标签: json sorting extjs model store


【解决方案1】:

查看Ext.util.Sorter 类,您可以在其中设置sorterFn。请参阅页面顶部的示例 - 您应该能够按照您描述的方式简单地编写对记录进行排序的逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多