javascript - 如何在 Extjs 商店阅读器中读取这个 json 字符串?

标签 javascript extjs extjs4

我的 json 字符串如下所示:

[{
        "totalCount" : 134
    }, {
        "items" : [{
                "id" : 1669,
                "check" : false,
                "name" : "1800mm趟门衣柜",
                "part_number" : "DP101669"
            }, {
                "id" : 1670,
                "check" : false,
                "name" : "1800mm趟门衣柜",
                "part_number" : "DP101670"
            }, {
                "id" : 1671,
                "check" : false,
                "name" : "2118mm趟门衣柜",
                "part_number" : "DP101671"
            }, {
                "id" : 1672,
                "check" : false,
                "name" : "2118mm趟门衣柜",
                "part_number" : "DP101672"
            }, {
                "id" : 1673,
                "check" : false,
                "name" : "1800mm趟门衣柜",
                "part_number" : "DP101673"
            }, {
                "id" : 1674,
                "check" : false,
                "name" : "1800mm趟门衣柜",
                "part_number" : "DP101674"
            }, {
                "id" : 1675,
                "check" : false,
                "name" : "1800mm趟门衣柜",
                "part_number" : "DP101675"
            }, {
                "id" : 1676,
                "check" : false,
                "name" : "1800mm趟门衣柜",
                "part_number" : "DP101676"
            }, {
                "id" : 1677,
                "check" : false,
                "name" : "2118mm趟门衣柜",
                "part_number" : "DP101677"
            }, {
                "id" : 1678,
                "check" : false,
                "name" : "2118mm趟门衣柜",
                "part_number" : "DP101678"
            }
        ]
    }
]

我的商店看起来像:

var item_store = Ext.create('Ext.data.Store', {

    model:'ItemModel',
    autoLoad: true,
    pageSize: 5,
    proxy: {
        type: 'ajax',
        reader: {
            type:'json',
            root: 'items',
            totalProperty: 'totalCount'
        },
        url: 'item_access.jsp?fullpage=true&item_cat=167&group_id='+groupId

    }
});

但它无法读取商店的根节点,现在我的表看起来像这样: 只有两个空白行, 看起来像节点totalCount和items:

enter image description here

那么我应该如何配置阅读器来读取该字符串?

这是我的网格:

    var itemPanel=Ext.create('Ext.grid.Panel', {

    id: 'item-panel',
    title: '产品',
    store: item_store,
    columns: [
        { header: 'id',  dataIndex: 'id' , flex: 1 },
        { header: 'part_number',  dataIndex: 'part_number' , flex: 2 },
        { header: 'name', dataIndex: 'name', flex: 3 },
        { xtype: 'checkcolumn',header: '可见', dataIndex: 'check' , flex: 2 ,
        listeners:{
                 checkchange: function(col, idx, isChecked) {
                    var view = itemPanel.getView(),
                        record = view.getRecord(view.getNode(idx));
                        postData(record.get('id'),"ITEM",isChecked);

                }
            }
        }

    ],
     region: 'center',
     layout: 'card',

    // paging bar on the bottom
    bbar: Ext.create('Ext.PagingToolbar', {
        store: item_store,
        displayInfo: true,
        displayMsg: 'Displaying topics {0} - {1} of {2}',
        emptyMsg: "No topics to display",
        items:[
            '-', {
            text: 'Show Preview',
            pressed: pluginExpanded,
            enableToggle: true,
            toggleHandler: function(btn, pressed) {
                var preview = Ext.getCmp('gv').getPlugin('preview');
                preview.toggleExpanded(pressed);
            }
        }]
    }),
});

我的模型:

Ext.define('ItemModel', {
    extend : 'Ext.data.Model',
    idProperty : 'id',
    fields : [{
            name : "name",
            convert : undefined
        }, {
            name : "id",
            type : types.INT
        }, {
            name : "part_number"
        }, {
            name : "check",
            type : types.BOOLEAN
        }

    ]

});

最佳答案

在 4.x 系列中,您可以传递 root/total 的函数:

totalProperty: function(data) {
    return data[0].totalCount
},
root: function(data) {
    return data[1].items
}

关于javascript - 如何在 Extjs 商店阅读器中读取这个 json 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19441703/

相关文章:

javascript - 我希望能够在网络上查看闭路电视屏幕

extjs - 如何在 Extjs 组合框中将 boolean 值转换为 Yes 或 No

model-view-controller - 集成 Ext.grid.panel 验证和 Ext.data.Model.validations

javascript - 制造变量名称

javascript - Extjs 存储不适合 JSON

javascript - 将句子 chop 到一定数量的单词

javascript - Dijit 1.7+ 小部件 : how to separate event logic from the present

IE 中 'a' href 中的 JavaScript

javascript - Extjs : url in store

javascript - EXTJS + startEditByPosition 函数在移动到其他行时不会引发焦点事件