ExtJS DataView只显示最后一个JSON元素

ExtJS DataView showing only last JSON element

本文关键字:JSON 元素 最后一个 显示 DataView ExtJS      更新时间:2023-11-04

我有以下代码:

new Ext.DataView({
    store: new Ext.data.JsonStore({
        url: 'request/getAssetsJSON',
        baseParams: {
            _id: this.request._id
        },
        root: 'results',
        fields: [ 'attachment', 'id', 'name', 'property', 'type' ],
        autoLoad: true
    }),
    emptyText: 'No images to display',
    tpl: new Ext.XTemplate(
        '<tpl for=".">',
            '<div class="thumb-wrap" id="{name}">',
            '<div class="thumb"><p>Test</p><img height="45" src="request/getAsset?id={id}&attachment={attachment}" title="{name}"></div>',
            '<span class="x-editable">{name}</span></div>',
        '</tpl>',
        '<div class="x-clear"></div>'
    )
})

request/getAssetsJSON发送的JSON是:

[{"attachment":"img1.png","id":"4dc90e2a6ba440e37601d5d074011e2e","name":"img1"},   
{"attachment":"img2.png","id":"4dc90e2a6ba440e37601d5d074011e2e","name":"img2"},
{"attachment":"img3.png","id":"4dc90e2a6ba440e37601d5d074011e2e","name":"img3"}]

我的问题是,DataView只显示最后一个图像(img3.png),并且只有一个GET请求来请求/getAsset。

我想ExtJS在我的模板上迭代的方式,或者它加载数据的方式有问题,但我在这里不知所措。

有人有什么想法吗?

所有项目都有相同的id"id":"4dc90e2a6ba440e37601d5d074011e2e"。您可以在服务器端更改json响应,也可以为此数据创建模型,并设置idProperty:_id(默认idProperty:id)

Ext.define('YouModel', {
    extend: 'Ext.data.Model',
    config: {
        idProperty : '_id',
        fields: [
            {name: 'id',          type: 'auto'},
            {name : 'attachment',      type : 'string'},
            {name : 'name',  type : 'string'}
        ]
    }
});
new Ext.DataView({
    store: new Ext.data.JsonStore({
        url: 'request/getAssetsJSON',
        baseParams: {
            _id: this.request._id
        },
        root: 'results',
        model: 'YourModel',
        autoLoad: true
    }),
  ....
})