简体   繁体   English

extjs4如何多次显示字段

[英]extjs4 how to display field multiple times

I have an extjs form panel. 我有一个extjs表单面板。 Based on the value selected in the combo box, I need to display a fieldset multiple times. 基于在组合框中选择的值,我需要多次显示一个字段集。 That is,display the field set once if value chosen is 1, twice if value chosen is 2. 也就是说,如果选择的值为1,则显示一次设置的字段。如果选择的值为2,则显示两次。

The problem that I am facing is that, the field set is being displayed only once. 我面临的问题是,该字段集仅显示一次。 Since, I am changing the title of the fieldset, I can tell more clearly that the fieldset being displayed is the one in the last iteration of the for loop. 因为我正在更改字段集的标题,所以我可以更清楚地告诉我们显示的字段集是for循环的最后一次迭代中的那个。

However,I can see the log messages in the js console for all the iterations of the for loop which means that the loop is being executed properly. 但是,我可以在js控制台中看到for循环的所有迭代的日志消息,这意味着该循环正在正确执行。

Here is my code: 这是我的代码:

 Ext.define('ELM.view.cl.Edit', {
    extend : 'Ext.form.Panel',
    alias : 'widget.cform',
    addMode : false,
    layout : 'fit',
    autoShow : true,
    initComponent : function() {
        this.items = this.buildItems();
        this.callParent(arguments);
    },
    buildItems : function() {
        return [ {
            xtype : 'form',
            id : 'details',
            items : [
                    {
                    xtype : 'fieldset',
                    columnWidth : 0.5,
                    title : 'Details',
                    items : [
                            {
                                    fieldLabel : 'Number Of Scripts Required',
                                    xtype : 'combo',
                                    name : 'noOfScriptsRequired',
                                    id : 'noOfScriptsRequired',
                                    store : new Ext.data.SimpleStore({
                                    fields : [ 'no', 'val' ],
                                    data : [['1','1'],['2','2'],['3','3']]
                                    }),
                                    displayField : 'no',
                                    valueField : 'val',
                                    listeners : {
                                        select : function(combo, value) {
                                            var formpanel = Ext.widget('cform');
                                            var sd = this.up('form').getComponent(
                                                            'scriptdetails');
                                            for ( var i=1; i<=combo.getValue();i++){
                                                console.log(i);
                                                var title="Script details "+i;
                                                sd.setTitle(title);
                                                sd.show();
                                                sd.hidden = false;
                                                console.log(sd);
                                            }
                                        }
                                    }
                                }, ]
                    }, {
                        xtype : 'fieldset',
                        id : 'scriptdetails',
                        columnWidth : '0.5',
                        hidden : true,
                        title : 'Script Details',
                        items : [ {
                            xtype : 'textfield',
                            fieldLabel : 'Script Name',
                            name : 'scriptName'
                        } ]
                    }

            ]
        } ];
    }

});

UPDATE: Here is the working code: 更新:这是工作代码:

{
    fieldLabel : 'Name :',
    xtype : 'textfield',
    name : 'name'
},{
    fieldLabel : 'Number Of Scripts Required',
    xtype : 'combo',
    name : 'noOfScriptsRequired',
    id : 'noOfScriptsRequired',
    store : new Ext.data.SimpleStore({
        fields : [ 'no', 'val' ],
        data : [ [ '1', '1' ],  [ '2', '2' ],[ '3', '3' ] ]
        }),
    displayField : 'no',
    valueField : 'val',
    listeners : {
        select : function(combo, value) {
            var dynamicPanel = Ext.getCmp("dynamicPanel");
            var scriptField = {
                xtype : 'fieldset',
                items : [
                    {
                    xtype : 'textfield',
                    fieldLabel : 'Script Name',
                    name : 'scriptName'
                    },
                    {
                    xtype : 'textfield',
                    fieldLabel : 'Script Parameters',
                    name : 'scriptParameters'
                    } ]
                    };
            dynamicPanel.removeAll(true);
            for ( var i = 0; i < combo.getValue(); i++) {
                var index = dynamicPanel.items.length;
                var j = i + 1;
                scriptField.title = "Script Details "+ j;
                dynamicPanel.insert(index,scriptField);
                dynamicPanel.doLayout();
            }
        }
        }

Thanks in advance 提前致谢

You are using id:'scriptdetails' in fieldset. 您在字段集中使用id:'scriptdetails'。 In web pages each component or element should have an unique id. 在网页中,每个组件或元素都应具有唯一的ID。 If there are elements with same id then there will be problems in rendering the elements like single element is rendered with errors or element may not be rendered. 如果存在具有相同ID的元素,则在渲染元素时会出现问题,例如渲染单个元素时出错或元素可能无法渲染。

In your case as you have to repeat the field set, don't not use fixed id. 对于您的情况,因为您必须重复字段集,所以请勿使用固定ID。 Use a random generated id from server or use 'itemId' which ExtJS provides. 使用从服务器生成的随机ID或使用ExtJS提供的“ itemId”。

Refer: here and here 参考: 这里这里

Update: Working fiddle is here 更新:工作小提琴在这里

Ext.onReady(function() {

    var store = new Ext.data.ArrayStore({
        id: 0,
        fields: [
            'myId',
            'displayText'
        ],
        data: [
            [1, '1'],
            [2, '2'],
            [3, '3'],
        ]
    });

    var scriptField = {
        xtype: 'fieldset',
        columnWidth: '0.5',
        title: 'Script Details',
        items: [{
            xtype: 'textfield',
            fieldLabel: 'Script Name',
            hiddenName: 'scriptName'
        }]
    };

    var container = new Ext.Panel({
        layout: 'hbox',
        height: '700px',
        items: [{
            xtype: 'panel',
            id: 'parentContainer',
            height: '700px',
            layout: 'form',
            items: [{
                xtype: 'combo',
                editable: false,
                triggerAction: 'all',
                mode: 'local',
                store: store,
                valueField: 'myId',
                displayField: 'displayText',
                fieldLabel: 'Show Number of Items',
                listeners: {
                    select: function(combo, value) {
                        var dynamicPanel = Ext.getCmp("dynamicPanel");
                        dynamicPanel.removeAll(true);
                        for (var i = 0; i < combo.getValue(); i++) {
                            scriptField.title = "Script Details " + i;
                            dynamicPanel.add(scriptField);
                            dynamicPanel.doLayout();
                            dynamicPanel.ownerCt.doLayout();
                            dynamicPanel.ownerCt.ownerCt.doLayout();
                        }
                    }
                }
            }, {
                xtype: 'panel',
                id: 'dynamicPanel',
                items: []
            }]
        }]
    });

    container.render(Ext.getBody());

});

Your buildItems code is executed once, at the beginning. 您的buildItems代码在开始时执行一次。 Afterwards, you never really add any more items. 之后,您再也不会真正添加任何项目了。 Which you would want to do using this function: http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.container.AbstractContainer-method-add 您想使用此功能执行的操作: http : //docs.sencha.com/extjs/4.2.2/#!/api/Ext.container.AbstractContainer-method-add

If you want to add an item, you have to create a new one. 如果要添加一项,则必须创建一个新项。 You can't add the same item into a container twice. 您不能将同一项目两次添加到容器中。 So you would have to create them on the fly: http://docs.sencha.com/extjs/4.2.2/#!/api/Ext-method-create 因此,您必须动态创建它们: http : //docs.sencha.com/extjs/4.2.2/#!/api/Ext-method- create

And for this, you should use some defined "blueprint", where you define that it is a fieldset, contains a textfield, and so on: http://docs.sencha.com/extjs/4.2.2/#!/api/Ext-method-define 为此,您应该使用一些已定义的“蓝图”,在其中定义它是一个字段集,包含一个文本字段,依此类推: http : //docs.sencha.com/extjs/4.2.2/#!/api /外部方法定义

A blueprint of an item should never contain an id. 一个项目的蓝图绝对不能包含id。 I would refer to all the items by using form.items[i] , and omit the id entirely. 我将使用form.items[i]引用所有项目,并完全省略id。

You still have one problem, though: Your items all contain a textfield by the same name. 但是,您仍然有一个问题:您的所有商品都包含一个具有相同名称的文本字段。 This won't work well with submit() . 这不适用于submit() But that's another story entirely. 但这完全是另一个故事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM