Matcher выполняется без события? - PullRequest
0 голосов
/ 08 июля 2019

Я создал персональную функцию для сопоставителя, чтобы отфильтровать варианты выбора порядка по букве. Затем я добавил эту функцию в средство сопоставления свойств, но подумал, что эта функция работает без необходимости в событии, но мое сопоставление не выполняется, за исключением случаев, когда я использую событие .onChange метода, который я вызываю.

Лучше бы я тебя увидел. Я получил 2 js, и в 1 из них я вызываю функцию для преобразования моего выбора в select 2

//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;
}
...