ExtJs:如何生成动态或通用数据字段?

问题描述 投票:0回答:1

如何生成动态表单字段?目前,数据通过viewModel从JSON文件获取负载。然后它绑定到面板的一些数据字段,如我的示例和当前状态如下:

Configuration.json - >将扩展更多条目

{
    "configuration": [
      {
        "name": "firstargument",
        "value": "123",
        "type": "string"
      } //I would like to extend that file later with more different data fields and data types
    ]
  }

ViewModel:Configuration.js - >必须以某种方式加载多个条目

Ext.define('QuickApp.model.Configuration', {
    extend: 'Ext.app.ViewModel',

    alias: 'viewmodel.configuration',

    data: {
        name: '', //here I need a data set with multiple entries
        value: ''
    },

    constructor: function(config) {
        var vm = this;

        this.callParent(arguments);

        vm.setStores({
            info: {
                fields: ['name','value'],
                proxy: {
                    type: 'ajax',
                    url: 'app/Configuration.json',
                    reader: {
                        tyoe: 'json',
                        rootProperty: 'configuration'
                    }
                },
                autoLoad: true,
                listeners: { //Or maybe some for each magic here? I don't know the syntax...
                    load: function(store, records) {
                        vm.set('name', store.getAt(0).get('name'));
                        vm.set('value', store.getAt(0).get('value'));
                    }
                }
            }
        });
    }
 });

配置Plugin.js - >如何在这里创建多个动态/通用子项?

Ext.define('QuickApp.view.configurationplugin.Configurationplugin',{
    extend: 'Ext.form.Panel',
    alias: 'widget.configurationplugin',

    title: 'Configuration Plugin',
    modal: true,
    draggable: true,
    floating: true,
    bodyPadding: 10,
    width: 300,
    centered: true,

    viewModel: {
        type: 'configuration'
    },

    items: [{
        xtype: 'textfield', //and how can I add multiple childs then here depending on the given types?
        name: 'firstargument',
        bind:{
            label: '{name}',
            value: '{value}',
        },
    }, {
        xtype: 'toolbar',
        docked: 'bottom',
        items: ['->', {
            xtype: 'button',
            text: 'Submit',
            iconCls: 'x-fa fa-check',
            handler: function() {
                this.up('configurationplugin').destroy();
            }
        }, {
            xtype: 'button',
            text: 'Cancel',
            iconCls: 'x-fa fa-close',
            handler: function() {
                this.up('configurationplugin').destroy();
            }
        }],
    }],
});

我知道,这是很多代码。但我会感激任何暗示!当前代码工作正常,只需一个数据。非常感谢!

javascript web extjs frontend single-page-application
1个回答
1
投票

您可以使用initialize事件为formpaneladd()方法添加基于类型的组件。

您可以直接添加组件,传递配置,如namelabel。你也可以使用binding

你可以在这里查看工作Fiddle

代码链

Ext.application({
    name: 'Fiddle',

    launch: function () {

        Ext.create('Ext.data.Store', {
            fields: ['name', 'value'],
            storeId: 'configuration',
            proxy: {
                type: 'ajax',
                url: 'Configuration.json',
                reader: {
                    tyoe: 'json',
                    rootProperty: 'configuration'
                }
            },
            autoLoad: true
        })

        Ext.define('QuickApp.model.Configuration', {
            extend: 'Ext.app.ViewModel',

            alias: 'viewmodel.configuration'
        });

        Ext.define('QuickApp.view.configurationplugin.Configurationplugin', {
            extend: 'Ext.form.Panel',
            alias: 'widget.configurationplugin',

            title: 'Configuration Plugin',

            modal: true,

            draggable: true,

            floating: true,

            bodyPadding: 10,
            width: 300,
            centered: true,

            viewModel: {
                type: 'configuration'
            },

            listeners: {
                initialize: function () {

                    var me = this,
                        items = [],
                        xtypes = {
                            'string': 'textfield',
                            'number': 'numberfield'
                        },
                        vm = me.getViewModel();

                    Ext.getStore('configuration').each(rec => {
                        let name = rec.get('name'),
                            value = name + 'Value';

                        items.push({
                            xtype: xtypes[rec.get('type')],
                            bind: {
                                label: `{${name}}`,
                                value: `{${value}}`,
                                name: `{${name}}`
                            }
                        });
                        vm.set({
                            [name]: name, [value]: rec.get('value')
                        })
                    });

                    /*
                    You colud direcly add like this without binding
                    items.push({
                        xtype: xtypes[rec.get('type')],
                        label:name,
                        value:rec.get('value')
                        name:name
                    });*/

                    items.push({
                        xtype: 'toolbar',
                        docked: 'bottom',
                        items: ['->', {
                            xtype: 'button',
                            text: 'Submit',
                            iconCls: 'x-fa fa-check',
                            handler: function () {
                                this.up('configurationplugin').destroy();
                            }
                        }, {
                            xtype: 'button',
                            text: 'Cancel',
                            iconCls: 'x-fa fa-close',
                            handler: function () {
                                this.up('configurationplugin').destroy();
                            }
                        }]
                    })
                    this.add(items)
                }
            }
        });

        Ext.create({
            xtype: 'container',
            fullscreen: true,
            items: [{
                xtype: 'button',
                margin: 5,
                ui: 'action',
                text: 'Create form',
                handler: function (btn) {
                    Ext.create({
                        xtype: 'configurationplugin',
                    }).showBy(btn);
                }
            }]
        });
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.