首页 文章

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

提问于
浏览
0

我需要在商店里安装一个组合框,所有工作都很好,除了一件事 . ¿为什么商店只填充 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

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

    这是固定版本,不确定这种解决方案是否符合您的需求: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]);
        }
    });
    
  • 0

    当您调用 form.getValues() 时,您只获取值,如果您想要值的关联记录,则必须搜索商店 . http://jsfiddle.net/jjgrn/7/

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

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

相关问题