是/否到Y / N Ex

时间:2018-03-10 22:45:48

标签: javascript checkbox extjs boolean

我正在尝试更改default发送的checkbox值。我有这段代码:

{
    xtype: 'checkcolumn',
    header: 'Contacto de Emergencia',
    dataIndex: 'contactoEmergencia',
    listeners: {
        beforecheckchange: function() {
            return false;
        },
    },
    width: 100,
    flex: 1,
    editor: {
        xtype: 'checkbox',
        cls: 'x-grid-checkheader-editor',
        inputValue: 'Y',
        uncheckedValue: 'N'
    }
}

我也设置了我的模型:

{
    name: 'contactoEmergencia',
    mapping: 'CONTACTO_EMERGENCIA',
    convert: function(v) {
        return v === 'Y';
    },
    serialize: function(v) {
        return v ? 'Y' : 'N';
    }
},

但它没有用。默认值true/false不会更改为Y/N

任何人都知道为什么不改变?我做错了什么?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下代码段: -

var store = Ext.create('Ext.data.Store', {
    fields: ['name', 'email', 'phone', 'active'],
    data: [{
        name: 'Lisa',
        email: 'lisa@simpsons.com',
        phone: '555-111-1224',
        active: 'Y'
    }, {
        name: 'Bart',
        email: 'bart@simpsons.com',
        phone: '555-222-1234',
        active: 'Y'
    }, {
        name: 'Homer',
        email: 'homer@simpsons.com',
        phone: '555-222-1244',
        active: 'N'
    }, {
        name: 'Marge',
        email: 'marge@simpsons.com',
        phone: '555-222-1254',
        active: 'Y'
    }]
});

Ext.create('Ext.grid.Panel', {
    title: 'Simpsons',
    height: 200,
    width: 400,
    renderTo: Ext.getBody(),
    store: store,
    columns: [{
        text: 'Name',
        dataIndex: 'name'
    }, {
        text: 'Email',
        dataIndex: 'email',
        flex: 1
    }, {
        text: 'Phone',
        dataIndex: 'phone'
    }, {
        editor: {
            xtype: 'checkbox',
            cls: 'x-grid-checkheader-editor',
            inputValue: 1,
            uncheckedValue: 0
        },
        text: 'Active',
        dataIndex: 'active',
        renderer: function (value, metadata, record, rowIndex, colIndex, store) {
            var tempVal = '',
                me = this;
            if (value === 'Y') {
                tempVal = 'checked';
            }
            return "<input name=" + record.get('id') + "_" + record.get('id') + " type='checkbox'" + tempVal + ">";
        }
    }],
    listeners: {
        cellclick: function (grid, td, cellIndex, record, tr, rowIndex, e, eOpts) {
            console.log(e);
            if (e.target.type && e.target.type === 'checkbox') {
                let checkVal = e.target.checked ? 'Y' : 'N';
                record.set('active', checkVal);
                console.log(record);
            }
        }
    }
});

您也可以查看fiddle,然后在控制台上查看更新的记录。

希望有所帮助:)