需要使用TomSelect的最小滤波器输入长度(例如Select2'最低限量)

发布于 2025-01-28 12:34:48 字数 953 浏览 4 评论 0原文

我有一个带有多选字段的网页以用于名称,并且为了防止偏见,我只想在用户将至少3个字符输入过滤器/搜索字段后向用户显示列表条目。

Select2使用 MiniminIminputLength a href =“ https://jsbin.com/jehaxinaju/1/edit?html,js,output“ rel =” nofollow noreferrer“> jsbin> jsbin

是什么是最佳的方法来实现这一目标? tomSelect for tomSelect for tomSelect for tomSelect in terme

设置可能是一种说明:

使用showsload()回调来实现最小输入长度或其他输入验证。如果回调返回false,则不会调用load(),并且NOT_LOADING模板将添加到下拉列表而不是加载或NO_RESULTS模板中。

但是,似乎应该载荷仅限制load()将被调用以获取远程资源的其他结果,但是所有已经可用的结果都将始终显示,不管shore shore load返回()。

I have a webpage with a multi-select field for names, and to prevent biases, I want to only show list entries to users after they have entered at least 3 characters into the filter/search field.

Select2 supports this using the minimumInputLength setting (jsbin)

What would be the best way to achieve this with TomSelect?
jsbin-template for tomselect here

Their documentation lists the shouldLoad setting as a possibility, with this explanation:

Use the shouldLoad() callback to implement minimum input length or other input validation. If the callback returns false, load() will not be called and the not_loading template will be added to the dropdown instead of the loading or no_results templates.

However, it seems that shouldLoad only limits whether load() will be called to fetch additional results from the remote resource, but any results that are already available will always be shown, no matter what shouldLoad returns (jsbin mre).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风轻花落早 2025-02-04 12:34:48

应该有效有效,当您满足查询时,您必须在回调中返回, load 只有在返回应负载的返回时,才会调用API?
您可以设置Minimun,Regex或您想要的任何验证。

new TomSelect("#your_select", {
    valueField: "id",
    labelField: "name",
    searchField: "name",
    shouldLoad: function (query, callback) {
        if (query.length > 3) {//if search has at least 4 chars
            return true;
        }
    },
    load: function (query, callback) {
        var url = "/your/url/remote/" + encodeURIComponent(query);
        fetch(url)
            .then((response) => response.json())
            .then((json) => {
                callback(json);
            })
            .catch(() => {
                callback();
            });
    },
});

为了删除先前的结果,您可以使用 clear()清除选定的选择,并在每个加载之前 emoveItem

shouldLoad works, you have to return on callback when you satisfy with the query, load will only call the api if the return of shouldLoad is true shouldLoad?
You can set minimun, regex, or any validation that you want.

new TomSelect("#your_select", {
    valueField: "id",
    labelField: "name",
    searchField: "name",
    shouldLoad: function (query, callback) {
        if (query.length > 3) {//if search has at least 4 chars
            return true;
        }
    },
    load: function (query, callback) {
        var url = "/your/url/remote/" + encodeURIComponent(query);
        fetch(url)
            .then((response) => response.json())
            .then((json) => {
                callback(json);
            })
            .catch(() => {
                callback();
            });
    },
});

For remove your previous results, you can use clear() to clear selected and removeItem before each load.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文