Extjs - 填充组合框whit存储不匹配

时间:2013-05-31 22:25:27

标签: extjs

我需要在商店中填充一个组合框,所有工作都很好,除了一件事。 ¿为什么商店只填充 displayField 而不是 valueField

在这一行

form.loadRecord(records[1]); 

我在表单中设置了该记录,很好,但是当我尝试提交表单时,我希望值为“2”而不是值“Media”。

关于Jsfiddle的代码和示例,以便更好地解释。

http://jsfiddle.net/jjgrn/5/

    var store = Ext.create('Ext.data.Store', {
        fields: ['id', 'status'],

        data: [
                { id: '1', status: 'Baja' },
                { id: '2', status: 'Media' },
                { id: '3', status: 'Alta' },
                { id: '4', status: 'Urgente' }
        ]
    });

    var formPanel = Ext.create('Ext.form.Panel', {
        title: 'Edit Country',
        url: 'http://aaa.com/',
        items: [
            {
                xtype: 'combobox',
                fieldLabel: 'Nombre',
                name: 'status',
                anchor: '50%',                          
                displayField: 'status',            
                valueField: 'id',
                store: store
            }
        ],
        buttons: [
            {
                text: 'Guardar',
                handler: function () {
                    var form = formPanel.getForm();
                    var value = form.getValues();

                    alert(value.status);
                }
            }
        ],
        renderTo: Ext.getBody()
    });



    store.load({
        scope: this,
        callback: function(records, operation, success) {
            var form = formPanel.getForm();
            form.loadRecord(records[1]);
        }
    });

感谢。

2 个答案:

答案 0 :(得分:0)

当您致电form.getValues()时,您只获得该值,如果您想要一个值的关联记录,则必须搜索该商店。 http://jsfiddle.net/jjgrn/7/

    var rec = store.getById(value.status);                
    alert(rec.get('status'));

关键是要了解getValues()只为表单中的每个字段调用getValue()getValue不会返回记录,只会记录您告诉它使用valueField: 'id',的记录中的字段。

答案 1 :(得分:0)

这是因为form.loadRecord()并没有完全按照你的期望去做。 你想要它做的是告诉combobox使用该特定记录(记录[1])。 实际上它的作用是告诉组合框:“现在将你的价值设定为媒体”,虽然把它与特定的记录联系起来太过“愚蠢”,但这种组合有礼貌。

这是固定版本,不确定这种解决方案是否符合您的需求: http://jsfiddle.net/jjgrn/6/

var store = Ext.create('Ext.data.Store', {
    fields: ['id', 'status'],

    data: [
            { id: '1', status: 'Baja' },
            { id: '2', status: 'Media' },
            { id: '3', status: 'Alta' },
            { id: '4', status: 'Urgente' }
    ]
});

var formPanel = Ext.create('Ext.form.Panel', {
    title: 'Edit Country',
    url: 'http://aaa.com/',
    items: [
    {
        xtype: 'combobox',
        fieldLabel: 'Nombre',
        name: 'status',
        anchor: '50%',                          
        displayField: 'status',            
        valueField: 'id',
        store: store
    }
    ],
    buttons: [
    {
        text: 'Guardar',
        handler: function () {
        var form = formPanel.getForm();
        var value = form.getValues();
        alert(value.status);
        }
    }
    ],
    renderTo: Ext.getBody()
});



store.load({
    scope: this,
    callback: function(records, operation, success) {
    // the operation object
    // contains all of the details of the load operation
    //var form = formPanel.getForm();
    //form.loadRecord(records[1]);
    formPanel.items.first().select(records[1]);
    }
});
相关问题