在ExtJS中,如何为不同的json输入格式定义jsonreader?

时间:2011-08-02 14:46:03

标签: json extjs coldfusion proxy jsonreader

我有一个以给定格式生成JSON的外部源。我需要使用ScriptTagProxy来访问该数据。数据格式为(两条记录):

{
"COLUMNS":["KEDBID","EMPID","KE","SOLUTION","ATTACH_KE","ATTACH_SOLUTION"],
"DATA":[
[1,36661,"While adding a new user, if his\/her profile is missing, it could be LDAP    server issue.","Contact Mr. ABC from infra team to get user's profile corrected on server","screenshots.jpg","conacts.doc"],
[2,36661,"The error code # 123445 is trivial that may occur at particular time.","To resolve this issue, we will need to ask DBA team to refresh database. That will fix this type of errors","NA","NA"]
]
} 

如何为这些数据定义jsonreader?正如我所见,root应该像:

{ROOT: [ {field1:data1,field2:data1},{field1:data2,field2:data2}]}

而我的JSON就像:

{fieldnames:['field1','field2'],
 ROOT:[ [data1ForField1,data1ForField2],[data2ForField1,data2ForField2] ]
}

更新

再添一个案例: 同样,我们可以有一个jsonreader;

    {"ROWCOUNT":8,
"COLUMNS":["CATID","CATEGORY"],
"DATA":{"CATID":[1,2,3,4,5,6,7,8],"CATEGORY":["Optimization","Automation","Process Improvement","Tool","Other","Another One","ThisHas'","More^!@#(){}"]}} 

其中1 - >优化,2 - >自动化,3 - >流程改进等。

基本上我需要通过序列化查询对象来从ColdFusion查询对象返回数据。 CF Query中的序列化可以返回上述两种格式的数据。

我还是Ext World的新手!!希望得到一些支持。

最诚挚的问候, Tushar Saxena


以下是我面临问题的代码。这些数据正在存储,因为它可供ComBox使用..但无法使用每个功能或其他方式读取数据?那么存储中的数据只能被提供给组件吗?在另一个例子中,我使用loadDATA()手动创建了一个simplestore(ArrayStore)paased数据..每个函数都在工作!!!

在下面的情况下,如果我在使用ADD()加载后添加一条记录,每个函数将执行一次,显示我刚添加的数据......并且只是添加的数据不可用于组件!怎么来!!!

可能我错过了一些非常基本的概念,因为我对EXT还很新。

回应真的很棒。

var proxy = new Ext.data.ScriptTagProxy({
            url: 'http://127.0.0.1:8500/extex/kebyid.cfm',
            method: 'POST'
        });

        var rec = Ext.data.Record.create([
            {name: 'EMPID', mapping: 1},
                     {name: 'KE', mapping: 2},
                     {name: 'SOLUTION', mapping: 3},
                     {name: 'ATTACH_KE', mapping: 4},
                     {name: 'ATTACH_SOLUTION', mapping: 5}
        ]);

        var myReader = new Ext.data.ArrayReader({
                    idIndex: 0,
                    root: 'DATA'
                }, rec);



        var store = new Ext.data.ArrayStore({
                 proxy: new Ext.data.HttpProxy({
                        url: '/extex/kebyid.cfm',
                            method: 'POST'
                }),

                autoSave: true,
                autoLoad: true,
                root: 'DATA',
                fields: [
                     {name: 'KEID', mapping: 0},
                     {name: 'EMPID', mapping: 1},
                     {name: 'KE', mapping: 2},
                     {name: 'SOLUTION', mapping: 3},
                     {name: 'ATTACH_KE', mapping: 4},
                     {name: 'ATTACH_SOLUTION', mapping: 5}
                ]
            });


    store.each(function(){
        alert('!!!!'); //**************NOT WORKING
    });
    var catCB = new Ext.form.ComboBox({
            fieldLabel: 'Category',
            layout:'absolute',
            x: 100,
             y: 5,
            store: store, //************* DATA IS THERE AS STORE IS PROVIDING DATA TO COMBOBOX
            emptyText:'Select Category...',
            valueField : 'KEID',
            displayField : 'KE',
            hiddenName : 'category',
            hiddenvalue : 'None', 
            mode: 'local',
            editable : false,
            lastQuery: '',
            renderTo: Ext.get("d1"),
            listeners: {

                'beforequery': function(qe){
                      qe.forceAll = true;
                }}
        });

1 个答案:

答案 0 :(得分:1)

首先,您不能将服务器的输出用于scriptTagProxy。 scriptTagProxy使用JSONP technology。所以输出应该如下所示:

callback({
  "COLUMNS":["KEDBID","EMPID","KE","SOLUTION","ATTACH_KE","ATTACH_SOLUTION"],
  "DATA":[
    [1,36661,"While adding a new user, if his\/her profile is missing, it could be LDAP    server issue.","Contact Mr. ABC from infra team to get user's profile corrected on server","screenshots.jpg","conacts.doc"],
    [2,36661,"The error code # 123445 is trivial that may occur at particular time.","To resolve this issue, we will need to ask DBA team to refresh database. That will fix this type of errors","NA","NA"]
  ]
});

然后只需使用ArrayStore作为商店。它默认使用ArrayReader

var store = new Ext.data.ArrayStore({
    proxy: new Ext.data.ScriptTagProxy({
        url: 'http://example.com/get_data.php'
    }),
    root: 'DATA',
    fields: [
       'id',
       // ...,
       // ...,
       // ...
    ]
});

UPDATE
我忘了您使用'DATA'作为root属性。将其添加到商店配置。