gpt4 book ai didi

extjs - 你如何从 json 创建表列和字段? (动态网格)

转载 作者:行者123 更新时间:2023-12-04 06:23:20 25 4
gpt4 key购买 nike

我有一个 json 文件,我假设我对内容一无所知。我不知道型号。然而,它在 json 文件中给出了模型、数据和有关网格的其他信息。我将如何以这种方式创建列等?

最佳答案

Stackoverflow 充斥着与这个非常相似的问题。我研究了所有这些,但没有找到明确的解决方案。但是,大多数提供的答案都为我指明了正确的方向。我会给我最好的机会,把所有这些建议放在一起,并让其他人清楚这一点:

型号: (仅显示将在所有 JSON 响应中的 2 个字段。仍将被覆盖)

Ext.define('RTS.model.TestsModel', {
extend: 'Ext.data.Model',
alias: 'model.TestsModel',

fields: [
{
name: 'poll_date'
},
{
name: 'poller'
}
]
});

店铺:
Ext.define('RTS.store.TestsStore', {
extend: 'Ext.data.Store',
alias: 'store.TestsStore',

model: 'RTS.model.TestsModel',

constructor: function(cfg) {
var me = this;

cfg = cfg || {};

me.callParent([Ext.apply({
autoLoad: false,
proxy : {
type : 'ajax',
url : 'tests.php',
reader : {
type : 'json',
root : 'tests',
successProperty : 'success'
}
},
storeId: 'tests-store'
}, cfg)]);
}
});

查看: (列将在每个 JSON 响应中定义)
Ext.define('RTS.view.TestsView', {
extend: 'Ext.grid.Panel',
alias: 'widget.TestsView',

id: 'tests-view',
title: 'Tests',
emptyText: '',
store: 'TestsStore',

initComponent: function() {
var me = this;

Ext.applyIf(me, {
viewConfig: {

},
columns: [
]
});

me.callParent(arguments);
}

});

Controller : ( Controller 根据 JSON 响应完成强制 View 和模型更改的所有工作)。
Ext.define('RTS.controller.TestsController', {
extend: 'Ext.app.Controller',
alias: 'controller.TestsController',

stores: [
'TestsStore'
],
models: [
'TestsModel'
],
views: [
'TestsView'
],

init: function(application) {

// When store changes, trigger an event on grid
// to be handled in 'this.control'.

// NOTE : Ext JS does not allow control of
// non-component events.

// Ext JS 4.2 beta will allow the controller
// to detect non-component changes and handle them
var testsStore = this.getStore('TestsStore');
testsStore.on("metachange", metaChanged, this);
function metaChanged(store, meta) {
var grid = Ext.ComponentQuery.query('TestsView')[0];
grid.fireEvent('metaChanged', store, meta);
};


this.control({
"TestsView": {
metaChanged: this.handleStoreMetaChange
}
});
},

/**
* Will update the model with the metaData and
* will reconfigure the grid to use the
* new model and columns.
*/
handleStoreMetaChange: function(store, meta) {
var testsGrids = Ext.ComponentQuery.query('TestsView')[0];
testsGrids.reconfigure(store, meta.columns);
}

});

JSON 响应:
您的 json 响应必须包含“metaData”属性。它应该像在静态模型和通常定义为显示字段的 View 上一样定义字段。
{
"success": true,
"msg": "",
"metaData": {
"fields": [
{
"name": "poller"
},
{
"name": "poll_date"
},
{
"name": "PING",
"type": "int"
},
{
"name": "SNMP",
"type": "int"
},
{
"name": "TELNET",
"type": "int"
},
{
"name": "SSH",
"type": "int"
},
{
"name": "all_passed"
}
],
"columns": [
{
"dataIndex": "poller",
"flex": 1,
"sortable": false,
"hideable": false,
"text": "Poller"
},
{
"dataIndex": "poll_date",
"flex": 1,
"sortable": false,
"hideable": false,
"text": "Poll Date"
},
{
"dataIndex": "PING",
"flex": 1,
"sortable": false,
"hideable": false,
"text": "PING",
"renderer": "RenderFailedTests"
},
{
"dataIndex": "SNMP",
"flex": 1,
"sortable": false,
"hideable": false,
"text": "SNMP",
"renderer": "RenderFailedTests"
},
{
"dataIndex": "TELNET",
"flex": 1,
"sortable": false,
"hideable": false,
"text": "TELNET",
"renderer": "RenderFailedTests"
},
{
"dataIndex": "SSH",
"flex": 1,
"sortable": false,
"hideable": false,
"text": "SSH",
"renderer": "RenderFailedTests"
},
{
"dataIndex": "all_passed",
"flex": 1,
"sortable": false,
"hideable": false,
"text": "All Passed",
"renderer": "RenderFailedTests"
}
]
},
"tests": [
{
"poller": "CHI",
"poll_date": "2013-03-06",
"PING": "1",
"SNMP": "0",
"TELNET": "1",
"SSH": "0",
"all_passed": "0"
},
{
"poller": "DAL",
"poll_date": "2013-03-06",
"PING": "1",
"SNMP": "0",
"TELNET": "1",
"SSH": "0",
"all_passed": "0"
},
{
"poller": "CHI",
"poll_date": "2013-03-04",
"PING": "1",
"SNMP": "0",
"TELNET": "1",
"SSH": "0",
"all_passed": "0"
},
{
"poller": "DAL",
"poll_date": "2013-03-04",
"PING": "1",
"SNMP": "0",
"TELNET": "1",
"SSH": "0",
"all_passed": "0"
},
{
"poller": "CHI",
"poll_date": "2013-03-01",
"PING": "1",
"SNMP": "0",
"TELNET": "1",
"SSH": "0",
"all_passed": "0"
}
]
}

关于extjs - 你如何从 json 创建表列和字段? (动态网格),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10979269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com