How to limit minimum character in selectize tags

☆樱花仙子☆ 提交于 2019-12-22 05:19:16

问题


I want to limit minimum 3 characters for Selectize tags input. Is it possible? is there any event in selectize?


回答1:


I had the same problem. Its as Rory has mentioned, via plugins.

Its actually quite simple.

The official example for tag minimum word length filtering you can find here

$('#select-words-length').selectize({
    create: true,
    createFilter: function(input) { return input.length >= MIN_LENGTH; }
});

Another thing that you can do is filter the search itself

//restricts the matches to fulfill MIN_SEARCH_LENGTH via the 'score' callback
//see https://github.com/brianreavis/selectize.js/blob/master/docs/usage.md#callbacks
score: function scoreFilter(search) {
    var ignore = search && search.length < MIN_SEARCH_LENGTH;
    var score = this.getScoreFunction(search);
    //the "search" argument is a Search object (see https://github.com/brianreavis/selectize.js/blob/master/docs/usage.md#search).
    return function onScore(item) {
        if (ignore) {
            //If 0, the option is declared not a match.
            return 0;
        } else {
            var result = score(item);
            return result;
        }
    };
},

Hope that helps :)




回答2:


There is a working example in the docs. See the "Remote Source — Rotten Tomatoes" example. I adapted to something like this:

load: function(query, callback) {
    if (!query || query.length < 3) return callback();  // <- this line
    $.ajax({ 
        // ajax options...



回答3:


Here is a dirty workaround that works for me, hope it helps.

var checkLength = function() {
    if (selectize.$control_input.val().length < 3) selectize.close()
};

selectize.on('dropdown_open', checkLength)



回答4:


  1. Download selectize.js plugin

  2. Include jquery and

Use this code, It will work.

$('#your-id').selectize({ maxItems: 3 });



来源:https://stackoverflow.com/questions/31155089/how-to-limit-minimum-character-in-selectize-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!