Selectize.js =如何阻止它自动加载远程数据 - 建议来源?

Bro*_*iLD 7 jquery selectize.js

使用selectize.js插件, https://github.com/brianreavis/selectize.js/blob/master/docs/usage.md

每当我输入输入字段时,似乎都会调整load()函数.

我只是想在初始化期间加载一次...在这一小时试图找出它......我感觉它没有这样的功能,或者我错过了什么?

多谢你们...

For*_*ser 10

要仅在第一次进行选择性输入加载时,您可以执行以下操作:

$('#select').selectize({
    preload: true, // make selectize load options after initialization
    load: function(query,callback){
        this.settings.load = null; // prevent selectize from loading when typing
        // also instead of this you can 
        // override this.onSearchChange = function noop(){};
        $.ajax({...}).then(function(response){
            callback(response);
        },function(){
            callback();
        }) 
    }
});
Run Code Online (Sandbox Code Playgroud)


Bri*_*ell 2

我很难解决这个问题。该文档分为用法和API

selectize.js 的加载选项用于查询/响应服务,该服务将根据查询返回可用数据的子集。每次查询更改时它都会触发,这是有道理的。

当然,如果您的数据源不接受查询参数,则这是不必要的。在这种情况下,一次性加载数据就足够了。

根据 API,您可以获取 selectize 实例并调用其上的许多方法,包括加载。

<script>
    var $select = $('#select').selectize({
        valueField: 'value',
        labelField: 'label',
        searchField: ['label']
    });

    var selectize = $select[0].selectize;

    selectize.load(function (callback) {
        $.ajax({
            url: 'http://localhost:64596/api/things',
            type: 'GET',
            error: function (e) {
                console.error(e.responseText);
                callback();
            },
            success: function (data) {
                callback(data);
            }
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)