【问题标题】:How can I disable automatic filtering in selectize.js? Built-in / plugin / modilfy source?如何在 selectize.js 中禁用自动过滤?内置/插件/修改源?
【发布时间】:2015-11-29 11:38:34
【问题描述】:

我有一个 selectize.js 下拉菜单,它使用 ajax 从服务器加载项目列表。服务器提供给定字符串的自动完成,所以我不需要 selectize 的本机过滤。此外,我真的需要关闭它:服务器输出可能与 selectize 的完全不同。

数据可以很好地输入到 JavaScript 对象中,但是 selectize 甚至不显示弹出窗口,因为这些项目与 selectize 的过滤器不匹配。如何禁用或修改本机过滤和匹配突出显示算法?使用内置选项还是使用插件?还是只能去修改源码?

编辑:

searchField: false / function() 不起作用(并且文档没有提到它们作为可用的选项值)

EDIT2:

最终想出了这个技巧:为每个项目添加一个假字段,为其分配一个搜索字符串并告诉 selectize 使用 searchField。但显然,应该有更好的方法,所以问题仍然悬而未决。

【问题讨论】:

    标签: javascript selectize.js


    【解决方案1】:

    我使用这个解决方案(如果服务器的结果排序正确):

        score: function() { return function() { return 1; }; },
    

    或者这个(如果需要订购)

        score: function(search) {
            var score = this.getScoreFunction(search);
            return function(item) {
                return 1 + score(item);
            };
        },
    

    Sifter 使用 score 函数进行过滤。得分结果必须> 0。

    【讨论】:

    • 这是一个更清洁、更安全、更面向未来的解决方案。我只想补充一点,如果 Selectize 控件连接到返回排名结果的后端搜索(例如弹性搜索),那么可以更改此代码以返回排名字段而不是内部生成的分数:score: function() { return function(item) { return item.rank; } }
    • 这是最好的方法。但我有一个问题。使用此代码使我的字段将新结果(来自 ajax)添加到项目列表中,而不是删除旧的! (我正在搜索的任何东西,结果都在增长)。我不得不在加载功能上手动刷新结果:load: function (query, callback) { this.clearCache('option'); this.clearOptions(); this.refreshOptions(true); ...
    【解决方案2】:

    我需要禁用搜索,以便 iPhone 不会显示键盘。我选择的解决方案通过挂钩到 selectize 设置使搜索字段只读(不修改实际源,因此 selectize 仍然是可更新的)。这是代码,如果有人需要的话:

    // Put this code after you've included Selectize
    // but before any selectize fields are initialized
    var prevSetup = Selectize.prototype.setup;
    
    Selectize.prototype.setup = function () {
        prevSetup.call(this);
    
        // This property is set in native setup
        // Unless the source code changes, it should
        // work with any version
        this.$control_input.prop('readonly', true);
    };
    

    【讨论】:

    • 这是一个干净的解决方案,是在 3rd 方库上实现自定义行为的最佳方式,所以接受它
    【解决方案3】:

    所以,搜索代码,我发现,Sifter 模块(搜索/排序引擎,Selectize 依赖),它确实有一个禁用过滤的选项,我们只需要将它转发到 Selectize。我可以建议以下补丁:

    在 Selectize main .js 文件中找到函数 getSearchOptions()

    https://github.com/brianreavis/selectize.js/blob/master/dist/js/selectize.js

    这是之前的:

    getSearchOptions: function () {
        var settings = this.settings;
        var sort = settings.sortField;
        if (typeof sort === 'string') {
            sort = [{field: sort}];
        }
    
        return {
            fields:      settings.searchField,
            conjunction: settings.searchConjunction,
            sort:        sort
        };
    }
    

    这是之后:(添加了一个逗号、5 行 cmets 和补丁本身)

    ...
    getSearchOptions: function () {
        var settings = this.settings;
        var sort = settings.sortField;
        if (typeof sort === 'string') {
            sort = [{field: sort}];
        }
    
        return {
            fields:      settings.searchField,
            conjunction: settings.searchConjunction,
            sort:        sort,
            // A patch to allow to disable native filtering, in the case,
            // when we want to provide search results on the server side.
            // Negative form of the setting is to avoid changing the standard
            // behaviour, (and, possibly, ruining the existing code), when this
            // parameter is missing.
            filter      : !settings.dontFilter
        };
    },
    ...
    

    对不起,我只是没有时间在 Github 上创建一个分支,项目截止日期快到了,而且,实际上我不确定我现在能否成为一名优秀的贡献者,因为缺乏一些在 Github 工作的经验。因此,只需发布​​一个快速解决方法。

    【讨论】:

    • 通过简短的测试,使用这种方法,当更改搜索字符串时,不会删除来自先前远程加载的“旧”选项。例如。如果一个人键入“foobar”很慢,导致远程搜索“foo”和“foobar”,“foo”远程搜索返回的项目也会在“foobar”远程搜索返回并更新到组件后显示。
    • 只需在加载方法中执行:删除 self.loadedSearches[query]; $.each(self.options, function(k, v) { delete self.options[k]; delete self.sifter.items[k]; });
    【解决方案4】:

    我在选择参数中使用onInitialize 方法解决了:

    $("select").selectize({
            onInitialize: function() {
                 this.$control_input.attr('readonly', true);
            }
    });
    

    【讨论】:

      【解决方案5】:

      使用一点 CSS 和一点 JS,我们可以创建它。而且看起来很完美。

      var select = $("#my-select-input");
      $(select).next().find("div.selectize-input").addClass("no-searchable"); // Adding style to the div
      $(select).next().find("div.selectize-input > input").addClass("no-searchable"); // Adding style to the input
      $(select).next().find("div.selectize-input > input").prop("readonly", true); // Setting the input to read-only
      $(select).next().find("div.selectize-input > input").prop("inputmode", "none"); // Guarantee in case it opens on the cell phone and click on the input no keyboard is opened
      $(select).next().find("div.selectize-input > input").focus(function () { // Hack for when the search input gets the focus it will automatically blur.
          $(this).blur();
      });
      
      .no-searchable {
          cursor: pointer !important;
          background-color: #FFFFFF !important;
      }
      .has-items input.no-searchable {
          width: 1px !important;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-20
        • 2020-12-24
        • 2016-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多