简体   繁体   English

限制显示的选择 tom-select

[英]limit displayed selections tom-select

I am using the javascript lib Tom-Select .我正在使用 javascript 库Tom-Select I would like to limit the display of how many items have been selected.我想限制显示已选择的项目数。 I do not want to limit the number of actual choices - only how many are displayed.不想限制实际选择的数量——只限制显示的数量。 Standard functionality shows all selections in a growing box.标准功能在不断增长的框中显示所有选择。 I would like to set a limit of 3 .我想将限制设置为3 Then if a user selects more than 3 the box will no longer grow but simply say 4 items selected (or 5, 6, etc).然后,如果用户选择了超过 3 个,则框将不再增长,而只是说4 items selected (或 5、6 等)。 Bonus points if I could limit selections by the element width instead of a count (forcing the element to always remain on one line of the form).如果我可以通过元素宽度而不是计数来限制选择(强制元素始终保持在表单的一行上),则加分。

You could trick using the render method and the items.length array, but how will you then let your users delete their own choices as you don't display the selected items?您可以使用 render 方法和items.length数组进行欺骗,但是当您不显示所选项目时,您将如何让您的用户删除他们自己的选择?

render: {
            option: function (data, escape) {
                return '<div class="d-flex"><span>' + escape(data.text) + '</span></div>';
            },
            item: function (data, escape) {
                //return '<span class="tag is-info mb-1 mr-1">' + escape(data.text) + '</span>';
                if (this.items.length >= 3){
                    return '<span class="tag is-info mb-1 mr-1" style="display:none">' + escape(data.text) + '</span>';
                }else{
                    return '<span class="tag is-info mb-1 mr-1">' + escape(data.text) + '</span>';
                }
            }
        }

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

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