Ext Form Model加载两个具有相同数据的字段

时间:2015-06-26 09:06:45

标签: extjs

我有一个表格的Ext模型。我需要从密码字段加载单个数据值到密码和确认密码字段。是否可以在EXT中加载类似的记录?

1 个答案:

答案 0 :(得分:1)

Yes it is possible. When a record is loaded in a form, the data is obtained by simply calling getData() on the record. Therefore, you can tweak your model to provide a copy of the password in another field like this:

Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: [
        'id',
        'name',
        'password'
    ],
    getData: function() {
        var data = this.callParent(arguments);
        data['confirm_password'] = data['password'];
        return data;
    }
});

Full example: https://fiddle.sencha.com/#fiddle/pgr

ALTERNATIVE APPROACH

If you do not want to change the model, then get the Password field mirror its value to the Confirm Password field whenever value is set through setValue:

var form = Ext.create('Ext.form.Panel', {
    defaultType: 'textfield',
    items: [
        {
            fieldLabel: 'Name',
            name: 'name'
        },
        {
            fieldLabel: 'Password',
            setValue: function(value) {
                this.superclass.setValue.apply(this, arguments);
                this.up('form').down('#confirm_password').setValue(value);
            },
            //inputType: 'password',
            name: 'password',
        },
        {
            fieldLabel: 'Confirm Password',
            //inputType: 'password',
            name: 'confirm_password',
            itemId: 'confirm_password'
        }
    ],
    renderTo: Ext.getBody()
})

Full example: https://fiddle.sencha.com/#fiddle/pgt