【问题标题】:ExtJS 4.1.1: Custom display text for multi-select comboboxExtJS 4.1.1:多选组合框的自定义显示文本
【发布时间】:2018-05-17 16:40:00
【问题描述】:

我有一个多选组合框,我需要按照特定模式“选择 [x]”设置其显示值,其中 x 对应于所选项目的数量。不幸的是,在尝试了多种不同的方法后,我找不到达到预期结果的方法。使用 fieldSubTpl 配置或覆盖 valueToRaw() 方法对我来说似乎是最有希望的方法。但是,我无法让他们中的任何一个工作。

我找到了 ExtJS 6 here 所需行为的工作示例。如果您选择了多个值,组合框会显示“x 个值已选择”,而不是简单地连接所选值。

如何在 ExtJS 4.1.1 中获得相同的结果?

【问题讨论】:

    标签: javascript extjs combobox extjs4.1 multi-select


    【解决方案1】:

    combo 需要使用 displayTpl 配置。

    在这个 FIDDLE 中,我在 ExtJS 4.x 中创建了与您在 ExtJS 6.x 中提供的相同示例。它工作正常。希望这将指导您或帮助您实现所需的解决方案。

    Ext.create('Ext.form.Panel', {
        renderTo: Ext.getBody(),
        title: 'Multiple Seletion Example in ExtJS 4.x for combo box',
        items: [{
            xtype: 'combo',
            margin: 20,
            fieldLabel: 'Character Name',
            store: Ext.create('Ext.data.Store', {
                fields: ['id', 'name'],
                data: [{
                    id: 0,
                    name: 'John Snow'
                }, {
                    id: 1,
                    name: 'Tyrion Lannister'
                }, {
                    id: 2,
                    name: 'Morgan Dexter'
                }, {
                    id: 3,
                    name: 'Lannister'
                }, {
                    id: 4,
                    name: 'Silicon Vally'
                }]
            }),
            displayField: 'name',
            /*
            If you use using this then initially you will see {0 values selected}
               displayTpl: new Ext.XTemplate('{[values instanceof Array ? values.length === 1 ? values[0]["' + combo.displayField + '"] : values.length + " values selected" : values]}'),
            */
            valueField: 'id',
            queryMode: 'local',
            multiSelect: true,
            filterPickList: true,
            listeners: {
                render: function (combo) {
                    //Use propery {displayTpl}
                    //The template to be used to display selected records inside the text field. An array of the selected records' data will be passed to the template.
                    combo.displayTpl = new Ext.XTemplate('{[values instanceof Array ? values.length === 1 ? values[0]["' + combo.displayField + '"] : values.length + " values selected" : values]}');
                    /*
                    you can also use like below function
                    combo.displayTpl = new Ext.XTemplate('{[this.getDisplayField(values)]}', {
                        getDisplayField: function (values) {
                            if (Ext.isArray(values)) {
                                var len = values.length;
                                return len == 1 ? values[0].name : (len + ' values selected');
                            }
                            return values;
                        }
                    });*/
                }
            }
        }]
    });
    

    【讨论】:

    • 谢谢,现在可以了!我过去也尝试过使用小提琴示例,但无论出于何种原因,当时它都不起作用。
    • 非常欢迎@TobsenB :)。可能是你错过的东西。
    猜你喜欢
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    相关资源
    最近更新 更多