简体   繁体   English

限制easyui组合框中的选择数量

[英]Limit the number of selections in easyui combobox

I have easyui combobox that is multi select. 我有多个选择的easyui组合框。 I am putting a limit on the number of items that can be selected so if you try to select more than the limit, it alerts the user and doesn't allow the item to be selected. 我对可以选择的项目数设置了限制,因此,如果您尝试选择的数量超出限制,则会提醒用户并且不允许选择该项目。 This is my code. 这是我的代码。

$("#assessment_content_items_select_standards").combobox({
        data: $rootScope.assessmentItemsFilters.selectedStandards,
        valueField: 'value',
        textField: 'label',
        multiple: true,           
        onSelect: function (standard)
        {
            var selectedStandardsCount = 0;
            angular.forEach($rootScope.assessmentItemsFilters.selectedStandards, function (stan) {
                if (stan.selected == true) {
                    selectedStandardsCount++;
                }
            });

            if (selectedStandardsCount >= 25) {
                alert("You have reached the maximum selected standards allowed of 25");
            }
            else {
                standard.selected = true;
            }
        },
        onUnselect: function (standard) {
            standard.selected = false;
        }                       
    });

In the onSelect method I get the selected count and if it's more that 25 it just does an alert and it doesn't set selected to true for that selection. 在onSelect方法中,我得到了选定的计数,如果它大于25,它只会发出警报,并且不会将该选择的selected设置为true。

The problem I'm having is that the UI still shows the item as selected even though the data shows as selected: false. 我遇到的问题是,即使数据显示为选中状态,UI仍将项目显示为选中状态:false。 I've tried the following to unselect but I see no difference. 我尝试了以下取消选择的选项,但没有区别。

standard.selected = false
$("#assessment_content_items_select_standards").combobox('unselect', standard);

My question is how do I stop options from being selected or is there a better way to put a limit on the dropdown? 我的问题是如何停止选择选项,或者是否有更好的方法限制下拉菜单?

Try this one: 试试这个:

$('#assessment_content_items_select_standards').combobox({
    onSelect: function (standard) {
        var count = $("div.combobox-item-selected").length;
        if (count > 25) {
            alert("You can only choose 25."); 
            $('#assessment_content_items_select_standards').combobox('unselect', standard. value);
        }
    }
});

Reference Documentation 参考文件

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

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