Property Grid in ExtJs

一曲冷凌霜 提交于 2019-12-02 11:52:46

See the partially implemented solution in this fiddle

Ext.create('Ext.data.Store', {
    storeId: 'myStore',
    fields:[ 'Type', 'Value', 'IsRequired', 'Identifier', 'Data'],
    data: [
        {
            "Type": 0,
            "IsRequired": false,
            "Identifier": "id-1",
            "Data": 1001
        },
        {
            "Type": 0,
            "IsRequired": true,
            "Identifier": "id-2",
            "Data": 2002
        },
        {
            "Type": 1,
            "IsRequired": true,
            "Identifier": "id-3",
            "Data": 0.71
        },
        {
            "Type": 3,
            "IsRequired": true,
            "Identifier": "id-4",
            "Data": "24.10.18 00:00"
        }
    ]
});

Ext.create({
    title: 'Properties Grid',
    xtype: 'grid',
    renderTo: Ext.getBody(),
    height: 400,
    width: 600,
    plugins: ['cellediting'],
    store: Ext.data.StoreManager.lookup('myStore'),
    selModel: 'cellmodel',
    columns: [
        { text: 'Identifier', dataIndex: 'Identifier' },
        {
            text: 'Data',
            dataIndex: 'Data',
            editor: {
                xtype: 'textfield' // will default to this
            },
            getEditor: function(record) {
                var xtype = null,
                    args = {
                    fieldLabel: record.get('Hint'),
                    allowBlank: !record.get('IsRequired'),
                    value: record.get('Data'),
                    disabled: false
                };

                console.log({
                    type: record.get('Type')
                });

                switch (record.get('Type')) {
                    case 0: // int
                        xtype = 'Ext.form.field.Number';
                        args.allowDecimals = false;
                    break;
                    case 1: // decimal
                        xtype = 'Ext.form.field.Number';
                        args.allowDecimals = true;
                    break;
                    case 2: // text
                        xtype = 'Ext.form.field.Text';
                    break;
                    case 3: // date
                        xtype = 'Ext.form.field.Date';
                        args.emptyText = 'дд.мм.гггг чч:мм';
                        args.format = 'd.m.y H:i';
                    break;
                    case 4: // enum
                    case 5: // sql
                        var dataValues = Ext.util.JSON.decode(record.get('EnumList'));
                        var dataArray = Object.keys(dataValues).map(function(k) { return [k, dataValues[k]] });

                        xtype = 'Ext.form.field.ComboBox ';
                        args.store = new Ext.data.ArrayStore({
                            fields: [
                                {name: 'myId', type: 'string'},
                                {name: 'displayText'}
                            ],
                            data: dataArray
                        });
                    break;
                }

                if (!xtype) {
                    return false;
                }

                return Ext.create('Ext.grid.CellEditor', {
                    field: Ext.create(xtype, args)
                });
            }
        }
    ]
});

PS: It is not entirely clear what you are trying to achieve, consider elaborating a bit more on the use case in your description.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!