简体   繁体   English

ExtJS 4 ComboBox multiSelect显示所选值的数量

[英]ExtJS 4 ComboBox multiSelect display number of selected values

I am writing a POC and my client wishes to display the number of selected values underneath a multi-select combo box. 我正在写一个POC,我的客户希望在多选组合框下面显示所选值的数量。

I can get the number of values from the select event listener but wanted to encapsulate the functionality in the component itself rather than have DIV where i perform a DOM update. 我可以从select事件侦听器获取值的数量,但是想要将功能封装在组件本身中,而不是让我执行DOM更新的DIV

The code for the multi-select combo box is as follows: 多选组合框的代码如下:

Ext.onReady(function () {
    // The data store containing the list of states
    var states = Ext.create('Ext.data.Store', {
        fields: ['abbr', 'name'],
        data: [{
            name: 'ALABAMA',
            abbreviation: 'AL'
        }, {
            name: 'ALASKA',
            abbreviation: 'AK'
        }, {
            name: 'AMERICAN SAMOA',
            abbreviation: 'AS'
        }, {
            name: 'ARIZONA',
            abbreviation: 'AZ'
        }, {
            name: 'ARKANSAS',
            abbreviation: 'AR'
        }, {
            name: 'CALIFORNIA',
            abbreviation: 'CA'
        }, {
            name: 'COLORADO',
            abbreviation: 'CO'
        }, {
            name: 'CONNECTICUT',
            abbreviation: 'CT'
        }, {
            name: 'DELAWARE',
            abbreviation: 'DE'
        }, {
            name: 'DISTRICT OF COLUMBIA',
            abbreviation: 'DC'
        }, {
            name: 'FEDERATED STATES OF MICRONESIA',
            abbreviation: 'FM'
        }, {
            name: 'FLORIDA',
            abbreviation: 'FL'
        }, {
            name: 'GEORGIA',
            abbreviation: 'GA'
        }, {
            name: 'GUAM',
            abbreviation: 'GU'
        }, {
            name: 'HAWAII',
            abbreviation: 'HI'
        }, {
            name: 'IDAHO',
            abbreviation: 'ID'
        }, {
            name: 'ILLINOIS',
            abbreviation: 'IL'
        }, {
            name: 'INDIANA',
            abbreviation: 'IN'
        }, {
            name: 'IOWA',
            abbreviation: 'IA'
        }, {
            name: 'KANSAS',
            abbreviation: 'KS'
        }, {
            name: 'KENTUCKY',
            abbreviation: 'KY'
        }, {
            name: 'LOUISIANA',
            abbreviation: 'LA'
        }, {
            name: 'MAINE',
            abbreviation: 'ME'
        }, {
            name: 'MARSHALL ISLANDS',
            abbreviation: 'MH'
        }, {
            name: 'MARYLAND',
            abbreviation: 'MD'
        }, {
            name: 'MASSACHUSETTS',
            abbreviation: 'MA'
        }, {
            name: 'MICHIGAN',
            abbreviation: 'MI'
        }, {
            name: 'MINNESOTA',
            abbreviation: 'MN'
        }, {
            name: 'MISSISSIPPI',
            abbreviation: 'MS'
        }, {
            name: 'MISSOURI',
            abbreviation: 'MO'
        }, {
            name: 'MONTANA',
            abbreviation: 'MT'
        }, {
            name: 'NEBRASKA',
            abbreviation: 'NE'
        }, {
            name: 'NEVADA',
            abbreviation: 'NV'
        }, {
            name: 'NEW HAMPSHIRE',
            abbreviation: 'NH'
        }, {
            name: 'NEW JERSEY',
            abbreviation: 'NJ'
        }, {
            name: 'NEW MEXICO',
            abbreviation: 'NM'
        }, {
            name: 'NEW YORK',
            abbreviation: 'NY'
        }, {
            name: 'NORTH CAROLINA',
            abbreviation: 'NC'
        }, {
            name: 'NORTH DAKOTA',
            abbreviation: 'ND'
        }, {
            name: 'NORTHERN MARIANA ISLANDS',
            abbreviation: 'MP'
        }, {
            name: 'OHIO',
            abbreviation: 'OH'
        }, {
            name: 'OKLAHOMA',
            abbreviation: 'OK'
        }, {
            name: 'OREGON',
            abbreviation: 'OR'
        }, {
            name: 'PALAU',
            abbreviation: 'PW'
        }, {
            name: 'PENNSYLVANIA',
            abbreviation: 'PA'
        }, {
            name: 'PUERTO RICO',
            abbreviation: 'PR'
        }, {
            name: 'RHODE ISLAND',
            abbreviation: 'RI'
        }, {
            name: 'SOUTH CAROLINA',
            abbreviation: 'SC'
        }, {
            name: 'SOUTH DAKOTA',
            abbreviation: 'SD'
        }, {
            name: 'TENNESSEE',
            abbreviation: 'TN'
        }, {
            name: 'TEXAS',
            abbreviation: 'TX'
        }, {
            name: 'UTAH',
            abbreviation: 'UT'
        }, {
            name: 'VERMONT',
            abbreviation: 'VT'
        }, {
            name: 'VIRGIN ISLANDS',
            abbreviation: 'VI'
        }, {
            name: 'VIRGINIA',
            abbreviation: 'VA'
        }, {
            name: 'WASHINGTON',
            abbreviation: 'WA'
        }, {
            name: 'WEST VIRGINIA',
            abbreviation: 'WV'
        }, {
            name: 'WISCONSIN',
            abbreviation: 'WI'
        }, {
            name: 'WYOMING',
            abbreviation: 'WY'
        }]
    });

    // Create the combo box, attached to the states data store
    Ext.create('Ext.form.ComboBox', {
        labelAlign: 'top',
        labelPad: 10,
        fieldLabel: 'Choose State',
        store: states,
        queryMode: 'local',
        editable: false,
        displayField: 'name',
        valueField: 'abbreviation',
        renderTo: Ext.getBody(),
        multiSelect: true,
        width: 400,
        displayTpl: '<tpl for=".">' +
            '{name}' +
            '<tpl if="xindex < xcount">, </tpl>' +
            '</tpl>',
        listConfig: {
            itemTpl: '{name} <div class="chkbox"></div>'
        },
        listeners: {
            select: function (combo, records) {
                console.log(records.length);   
            }
        }
    });

});

Is there any easy way to achieve this? 有没有简单的方法来实现这一目标?

You can write a simple plugin for combobox. 你可以为combobox编写一个简单的插件。 Then it can be assigned to any combobox of your application. 然后它可以分配给您的应用程序的任何组合框。

For example: 例如:

Ext.define('comboSelectedCount', {
    alias: 'plugin.selectedCount',
    init: function(combo) {
        var fl = combo.getFieldLabel();
        combo.on({
            'select': function(me, records) {
                me.setFieldLabel(fl + ' (' + records.length + ' selected)');
            },
            'beforedeselect': function(me) {
                me.setFieldLabel(fl);
            }
        });
    }
});

And in your combo: 在你的组合中:

plugins: ['selectedCount'],

See live example on jsfiddle 查看jsfiddle上的实例

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

相关问题 Extjs 网格具有多选功能以检索选定列表的值 - Extjs grid with multiselect feature to retrieve value of selected lists 没有模型的MVC多选选定值 - MVC multiselect selected values without model ExtJS 如何将表单值与选定记录进行比较 - ExtJS How to compare form values to a selected record Extjs4组合框设置值字段和显示文件 - Extjs4 combobox set value field and display fileld 移动替代多选组合框? - Mobile Alternative to multiselect combobox? React 组件中的多选 - 如何设置所有选定值的状态? - Multiselect in React Component - how to set state with all selected values? PHP Multiselect与选择的jQuery-返回所有值,不仅是选定的 - PHP Multiselect with Chosen jQuery - Returning all values not just selected 有没有办法在多选列表中发布所有值,而不仅仅是选定的值? - Is there a way to post all values in a multiselect list and not just the selected ones? 根据在MultiSelect Box中选择的值将电子邮件表格发送到特定的电子邮件地址 - Email Form to specific email addresses based on values selected in MultiSelect Box 如何使用Extjs Form loadRecord设置checkbox和combobox的值? - How to set values of checkbox and combobox by using Extjs Form loadRecord?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM