【问题标题】:How to pass json data from controller to view in EXTJS 4.0如何将 json 数据从控制器传递到 EXTJS 4.0 中的视图
【发布时间】:2014-08-26 08:46:48
【问题描述】:

我在网格中声明了一个组合框

header: '<b>Reasons</b>',
            width : 150,
            editor:
                {
                    xtype: 'combobox',
                    store: 'reasonstore',
                    displayField: 'displayText',
                    valueField: 'value',
                    queryMode: 'local',

                }

我的商店是

var reasonstore = Ext.create('Ext.data.Store', { 字段:['displayText', 'value'], id : 'resonstoreid' });

我已经打了一个休息电话,并在控制器中获取了我的 jsondata

successCallback:function(json){
            var mydata = json.reason;
Ext.getCmp('resonstoreid').getStore().loadData(json.reason);

但我得到的 Ext.getCmp('resonstoreid') 是未定义的。 我的视图只是先加载然后控制器。 那么如何将这个 json 数据从控制器加载到视图。

【问题讨论】:

    标签: extjs


    【解决方案1】:

    您应该使用 Ext.getStore("xxx") 来获取对您商店的引用!这应该可以解决您的问题。

    var store = Ext.getStore("resonstoreid");
    store.loadData(json.reason);
    

    其他建议

    不要进行 AJAX 调用,而是用 loadData(xxx) 填充存储,使用存储的 load() 函数。见这里:Ext.data.Store.load

    这样做,你的商店配置应该是这样的

    //create File store/Reasons.js 
    Ext.define('XYZ.store.Reasons', {  //XYZ ... you namespace
        extend: 'Ext.data.Store',
    
        fields: ['displayText', 'value'], 
        id : 'Reasons',
        autoLoad: true,
        proxy: {
            type: 'ajax',
            actionMethods:  {create: 'POST', read: 'GET', update: 'POST', destroy: 'POST'},
            url: 'xxx',
            method: 'POST',            
    
            reader: {
                type: 'json',
                root: 'data'
            }
        }
    });
    

    之后,你可以这样做:

    var store = Ext.getStore("Reasons");
    store.load(
        params: {
            group: 3,
            type: 'user'
        },
        callback: function(records, operation, success) {
            // do something after the load finishes
        },
        scope: this
    });
    

    优势将是一个更好的应用程序结构,load 为您执行 ajax 请求。这正是 extjs 的优点:将代码拆分为许多易于维护的小文件(代码重用等)。

    【讨论】:

    • 非常感谢...它起作用了..bt 现在当我从组合框中选择一个值时,在选择值字段而不是显示字段后显示...对此 plzz 的任何建议。 ..
    • 这通常表示组合存储未加载或未找到具有该值的记录。
    • 查看数据是否加载正确。在控制台中试试这个 store = Ext.getStore("resonstoreid");存储.data.items[0].data;输出是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多