如何在ExtJS(和Sencha架构师)的GridPanel中显示嵌套对象?

问题描述 投票:2回答:2

我有一个传入的Provide对象数组,如下所示:

[{
  "id" : 16,
  "price" : 500,
  "quantity" : 2000,
  "denomination" : "case",
  "denominationPlural" : "cases",
  "product" : {
    "id" : 14,
    "description" : "This is the text description for product 14."
  }
}, {
  "id" : 18,
  "price" : 500,
  "quantity" : 1,
  "denomination" : "study",
  "denominationPlural" : "studies",
  "product" : {
    "id" : 17,
    "description" : "This is a text description for product 17."
  }
}]

产品是产品乘以价格的数量。

现在我想在GridPanel中显示这些产品,但也包括这些行中的嵌套产品信息。以下是我之前的做法:

Ext.define('Offering', {
            extend: 'Ext.data.Model',
            fields: [
                {name: 'id', type: 'number'},
                {name: 'price', type: 'number'},
                {name: 'quantity', type: 'number'},
                {name: 'denomination', type: 'string'},
                {name: 'denominationPlural', type: 'string'},
                {name: 'product.description', type: 'string'},
            ]
        });



var offeringStore = Ext.create('Ext.data.Store', {
             autoDestroy: true,
             model: 'Offering',
             proxy: {
                 type: 'ajax',
                 url: '/offerings',
                 reader: {
                     type: 'json',
                     root: 'success'
                 }
             },
             autoLoad:true
         });

var offeringGrid = Ext.create('Ext.grid.Panel', {
            store: offeringStore,
            columns: [{
                    header: 'id',
                    dataIndex: 'id'
                }, {
                    header: 'price',
                    dataIndex: 'price'
                }, {
                    header: 'quantity',
                    dataIndex: 'quantity'
                }, {
                    header: 'denomination',
                    dataIndex: 'denomination'
                }, {
                    header: 'description',
                    dataIndex: 'product.description'
                },
            };

这工作得很好。然后,在某个地方(包括升级到ExtJS 5.1.1(来自ExtJS 4.2.1)和使用Sencha Architect,它破了。

问题1:Sencha Architect阻止在产品模型中为“product.description”创建一个条目,抱怨“。”字符。但是如果你把它创建为“whateveryouwant”,你可以进入模型领域并将其重命名为“product.description”。

问题2:解决“。”问题后。发出并运行应用程序时,“product.description”列的单元格为空白。

问题3:javascript控制台产生零错误。传入的数据看起来很好。

我该怎么做才能显示这个嵌套数据?

javascript extjs extjs5 sencha-architect extjs-grid
2个回答
4
投票

我这样做很简单。只需将mapping属性添加到模型中:

{
    name: 'product.description', 
    type: 'string',
    mapping : 'product.description'
}

不需要renderer


1
投票

我当前的解决方法可以帮助处于相同情况的其他人,使用“渲染器”子句来显示所需的信息。

{
    xtype: 'gridpanel',
    title: 'Offerings',
    bind: {
        store: '{OfferingsStore}'
    },
    columns: [
            ...
            {
                xtype: 'gridcolumn',
                renderer: function(value, metaData, record, rowIndex, colIndex, store, view) {
                    return record.getData().product.description;
                },
                text: 'Product Description'
            },
            ...
      ]

但是我还是想在开发模型定义中使用“product.description”而不是这个渲染技巧。

© www.soinside.com 2019 - 2024. All rights reserved.