当有多个select2实例时,如何访问Select2的文本?

时间:2018-09-28 19:37:13

标签: javascript jquery jquery-select2

我有以下与第一个选择器一起使用的代码,并提供了我要寻找的结果,即第一个到第k个字符匹配的结果的排名高于仅在选项字符串中存在这些字符的结果的排名。 (即输入字符串“ ka”会将堪萨斯州排名在阿拉斯加之上)

在站点上其他select2实例之一中运行时失败。代码如下:

$(document).ready(() => {
userSettings.init()    
$('.select2').select2({        
    sorter: function(data) {
        return data.sort(function (a, b) {
            // Declare levenshtein algorithm
            var levenshtein = require('fast-levenshtein');

            // Trim and set to lowercase on the inputs
            a = a.text.trim().toLowerCase();
            b = b.text.trim().toLowerCase();

            /* This is the problem.  I need a better way to retrieve the
            input value in order to test it's fitness against my options. */
            var input = $('.select2-search__field').val().trim().toLowerCase();

            // Weigh by those that match the input in the beginning of the string first, and then list the rest that only contain the string next.
            if (!isUndefined(input) && input.length > 0) {
                if(input == a.substring(0, input.length))  {
                    return -1;
                } else if (input == b.substring(0, input.length))  {
                    return 1;
                } else {
                    // Input doesn't match the beginning of the string, still return strings with matching characters. 
                    return levenshtein.get(a, input) - levenshtein.get(b, input);
                }
            }
        });
        // Function to ensure we are not operating on something that is undefined.
        function isUndefined(value){
            // Obtain `undefined` value that's guaranteed to not have been re-assigned.
            var undefined = void(0);
            return value === undefined;
        }
    }
})

});

如您所见,与声明var input相比,我需要一种更好的获取输入的方法。

****编辑****

我想我有解决办法。

我怀疑这种排序发生得如此之快,而且经常如此,以至于它只会放弃。我们通过简化工作流程来对此进行了测试,并且会失掉一部分。

作为解决方案,我将分配从实际的排序函数中拉出,然后将其传递。

$(document).ready(() => {
    userSettings.init()    
    $('.select2').select2({sorter: s2_sorter})
});


//Function to do sort on a Select2 Field.
let s2_sorter = function(data) {

/* Obtain the value out of the text field in order to test agaist the options during sort. */
var input = {d:$('.select2-container--open .select2-search__field').val().trim().toLowerCase()};
return data.sort(function (a, b) {
    // Access the varible inside sort.
    let i = input.d;

    // Declare levenshtein algorithm
    var levenshtein = require('fast-levenshtein');

    // Trim and set to lowercase on the inputs
    a = a.text.trim().toLowerCase();
    b = b.text.trim().toLowerCase();

    // Wieght by those that match character first, and then all with the character next.
    if (!isUndefined(i) && i.length > 0) {
        console.log('hassomethign')
        if(i == a.substring(0, i.length))  {
            return -1;
        } else if (i == b.substring(0, i.length))  {
            return 1;
        } else {
            // Input doesn't match the begining of the string, still return strings with matching characters. 
            return levenshtein.get(a, i) - levenshtein.get(b, i);
        }
    }
});
function isUndefined(value){
    // Obtain `undefined` value that's guaranteed to not have been re-assigned.
    var undefined = void(0);
    return value === undefined;
}

};

0 个答案:

没有答案