来自JSON记录的Sencha填充选项卡

时间:2013-11-29 13:08:24

标签: javascript json extjs

我的JSON文件:

{
    "options":{
        "someString": "SomeText",
        "someNumber": 42,
        "someCombo": 2,
        "someBool" : true
    }
}

我为该JSON文件创建了一个JSON存储,它正确加载。

现在,我的选项标签:

items: [
    {
        xtype: 'panel',
        layout: {
            align: 'stretch',
            type: 'vbox'
        },
        title: 'Options Tab',
        tabConfig: {
            xtype: 'tab',
            flex: 1
        },
        items: [
            {
                xtype: 'textfield',
                id: 'someString',
                fieldLabel: 'Some String:',
            },
            {
                xtype: 'numberfield',
                id: 'someNumber',
                fieldLabel: 'Some Number',
            },
            {
                xtype: 'combobox',
                id: 'someCombo',
                fieldLabel: 'Some Combo',
                editable: false,
                store: [['0','Option Zero'],['1','Option One'],['2','Option Two']]
            },
            {
                xtype: 'checkboxfield',
                id: 'someBool',
                fieldLabel: 'Some Boolean',
                boxLabel: 'Yes'
            }
        ]
    }
]

经过一番尝试后,我还没有找到用JSON数据填充表单元素的方法:

onJsonstoreLoad: function(store, records, successful, eOpts) {
    // this.child("#sepString").update(store.getAt(0).fields.getByKey("sepString"));
    // Ext.fly("someString").update(store.getAt(0).fields.getByKey("someString"));
    // Ext.fly("someString").setValue(store.getAt(0).fields.getByKey("someString"));
    Ext.fly("someString").value=store.getAt(0).fields.getByKey("someString");
    ...
    Ext.fly("someBool").checked=store.getAt(0).fields.getByKey("someBool");
}

那么如何将数据输入表单元素呢?

1 个答案:

答案 0 :(得分:2)

使用FormPanel及其loadRecord方法。你也应该看看Ext.form.Basic#setValues

var formPanel = Ext.widget({
    xtype: 'form', // Use form xtype instead of panel
    renderTo: Ext.getBody(),
    layout: {
        align: 'stretch',
        type: 'vbox'
    },
    title: 'Options Tab',
    tabConfig: {
        xtype: 'tab',
        flex: 1
    },
    items: [{
        xtype: 'textfield',
        // We need names, not ids
        name: 'someString',
        fieldLabel: 'Some String:',
    },{
        xtype: 'numberfield',
        name: 'someNumber',
        fieldLabel: 'Some Number',
    },{
        xtype: 'combobox',
        name: 'someCombo',
        fieldLabel: 'Some Combo',
        editable: false,
        store: [['0','Option Zero'],['1','Option One'],['2','Option Two']]
    },{
        xtype: 'checkboxfield',
        name: 'someBool',
        fieldLabel: 'Some Boolean',
        boxLabel: 'Yes'
    }]
});

// Create a mock record
var MyRecord = Ext.define(null, {
    extend: 'Ext.data.Model'
    ,fields: ['someString', 'someNumber', 'someCombo', 'someBool']
});
var record = new MyRecord({
    "someString": "SomeText",
    "someNumber": 42,
    "someCombo": 2,
    "someBool" : true
});

// You would use the record loaded in your store instead:
//     var record = store.getAt(0);

// In recent version of Ext4, you can call formPanel.loadRecord directly
formPanel.getForm().loadRecord(record);