匹配程序是否在没有事件的情况下执行?

时间:2019-07-08 10:29:25

标签: jquery jquery-select2 matcher

我为匹配器创建了一个个人功能,可以按字母过滤选择顺序的选项。然后,我将此函数添加到了属性匹配器中,但是以为该函数不需要事件就可以工作,但是我的匹配器没有执行,除非我使用所调用方法的事件.onChange

我见到你最好。我有2个js,在其中1个中,我正在调用一个函数来转换select 2中的select。

//This is exectuing when onLoad
inicializarFilterCombo("#mySelect", matcher);

//This is the function where i pass my custom matcher to make the search
function inicializarFilterCombo(nameSelect, matcher) {
  if (matcher) {
    $('#' + nameSelect).select2({
      multiple: false,
      theme: "bootstrap",
      minimumResultsForSearch: 1,
      matcher: matcher

    });
  } else {
    $('#' + nameSelect).select2({
      multiple: false,
      theme: "bootstrap"
    });
  }
}

//and this is my custom matcher
var matcher = function matchCustom(params, data) {
  // If there are no search terms, return all of the data
  if ($.trim(params.term) === '') {
    return data;
  }

  // Do not display the item if there is no 'text' property
  if (typeof data.text === '') {
    return null;
  }

  // `params.term` should be the term that is used for searching
  // `data.text` is the text that is displayed for the data object

  if (data.text.toLowerCase().startsWith(params.term.toLowerCase())) {
    var modifiedData = $.extend({}, data, true);
    //modifiedData.text += ' (matched)';
    //390 index en el combo

    // You can return modified objects from here
    // This includes matching the `children` how you want in nested data sets
    return modifiedData;
  }

  // Return `null` if the term should not be displayed
  return null;
}

0 个答案:

没有答案