【问题标题】:ExtJs 4 - Display a value instead of it's id inside a combobox within a gridExtJs 4 - 在网格内的组合框中显示一个值而不是它的 id
【发布时间】:2012-07-09 10:28:35
【问题描述】:

我在 StackOverflow 上的第一个问题,不要责怪我太多;)

我必须做一个界面,显示一个网格,其中包含从 ajax 请求加载的内容。

此时,没有什么困难,我设法创建了一个模型、一个存储和一个代理,以便从我的 php/mysql 脚本中获取值并将它们显示在网格上。

但是在返回的数据中,我有一些 ID 应该被转换为存储在另一个 MySQL 表中的等效值。

此时我被卡住了,在搜索了几个小时后,我仍然不明白如何将我拥有的 id 值重新映射到另一个商店/模型的标签。

代码如下:

面板视图:

Ext.define('Audiotel.views.TimeRangePanelView', {
extend : 'Ext.grid.Panel',
config : {
    columns : [{
        header : 'Start date',
        dataIndex : 'slo_time_start',
        editor : {
            xtype : 'timefield',
            format : 'H:i:s',
            increment : 60 // In minutes
        },
        renderer : function(value, metaData, record, rowIndex, colIndex, store, view) {
            return value;
        }
    }, {
        header : 'End Date',
        dataIndex : 'slo_time_end',
        editor : {
            xtype : 'timefield',
            format : 'H:i:s',
            increment : 60 // In minutes
        },
        renderer : function(value, metaData, record, rowIndex, colIndex, store, view) {
            return value;
        }
    }, {
        header : 'Days',
        dataIndex : 'slo_valid_days',
        editor : Ext.create('Ext.form.field.ComboBox', {
            typeAhead : true,
            triggerAction : 'all',
            selectOnTab : true,
            store : [['Everyday', 'Everyday'], ['WE and holiday', 'WE and holiday'], ['Week', 'Week']],
            lazyRender : true,
            listClass : 'x-combo-list-small'
        })
    }, {
        header : 'Range',
        dataIndex : 'slo_hou_id',
        editor : Ext.create('Ext.form.field.ComboBox', {
            selectOnTab : true,
            store : [['1', 'Peak'], ['2', 'Offpeak'], ['3', 'Night']],
            listClass : 'x-combo-list-small',
        }),
        renderer : function(value, metaData, record, rowIndex, colIndex, store, view) {
            return value;
        }
    }],
    height : 400,
    width : '100%',
    store : Audiotel.stores.StoreBuilder.factory('TimeRange').createStore('getDefaultTimeRange'),
    renderTo : "range_table_div",
    frame : true
},

constructor : function() {
    this.callParent()
}
});

商店建设者(使用代理):

Ext.define('Audiotel.stores.StoreBuilder', {

statics : {
    instance: null,
    factory: function(name) {
        if(!this.instance){
            this.instance = new this({storeName: name});
        }
        return this.instance;
    }
},

storeName: null,

config: {
    storeName: ''
},

constructor: function(config){
        this.initConfig(config);
},

applyStoreName: function(name){
    if(!name){
        throw new Error('['+ Ext.getDisplayName(arguments.callee) +'] Name of store to create.');
    }
    return name;
},


createStore: function(proxyMethod, proxyParams){
    var modelName = Audiotel.utils.AppUtils.BASE_PATH+'.'+Audiotel.utils.AppUtils.MODEL_PATH+'.'+this.getStoreName()+'Model';
    Ext.Loader.syncRequire(modelName);  
    return Ext.create('Ext.data.Store', {
            autoLoad: true,
            proxy: Ext.create('Audiotel.proxies.AudiotelProxyWrapper', {method: proxyMethod, params: proxyParams}).getWrappedProxy(),
            model:  modelName,
            storeId:    this.getStoreName()+'Store'
        });
}
});

型号:

Ext.define('Audiotel.models.TimeRangeModel', {
extend : 'Ext.data.Model',
alias : 'TimeRange',
fields : [{
    name : 'slo_time_start',
    type : 'string'
}, {
    name : 'slo_time_end',
    xtype : 'string'
}, {
    name : 'slo_valid_days',
    type : 'string'
}, {
    name : 'slo_hou_id',
    type : 'string'
}]
});

我要翻译的值是“slo_hou_id”,它仍然显示“1”而不是“峰值”。

提前感谢您的帮助!

ps : 我对 ExtJS 很陌生...

编辑:

我为我的其他表值创建了一个新存储:

Ext.define('Audiotel.models.HoursModel', {
extend : 'Ext.data.Model',
alias : 'Hours',
fields : [{
    name : 'hou_id',
    type : 'int'
}, {
    name : 'hou_label',
    xtype : 'string'
}]
});

我已经将数据加载到其中:

[{"hou_id":"1","hou_label":"Heures pleines"},{"hou_id":"2","hou_label":"Heures creuses"},{"hou_id":"3","hou_label":"Heures nuit"}]

但是当我尝试搜索值时,它似乎失败了:

var hours = Audiotel.stores.StoreBuilder.factory('Hours').createStore('getHoursValues');

console.log(hours.getById(1));
console.log(hours.findRecord('hou_id', 1));

全部返回 null... :(

【问题讨论】:

    标签: extjs extjs4


    【解决方案1】:

    你有两个选择:

    1. 第一个选项是您从 MySQL 返回您想要的内容,而不是在 Javascript 中执行“join”:因此,从 MySQL/PHP 返回“Peak”而不是“1”。
    2. 使用“Store.find*”函数将“1”映射到“Peak”。类似other_store.findRecord('slo_hou_id', 1).attribute_you_want

    【讨论】:

    • 感谢您的快速回答,但似乎我仍然缺少一些东西......我正在更新我的原始问题以添加信息。 ps : 不能直接返回值,因为后面需要索引...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多