使用配置 FormatSelection 设置选定值 Remote Select2

Set Selected Value Remote Select2 With Configuration FormatSelection

我有一个问题,当我从 Select 中选择时,它会显示格式为 Selection 的选项,但当我设置时Select 的选定值未从 data_selection

获取数据对象变量 formatRepoSelectionLanguage
function setSelect2Data(id, data_id = null, data_text = null, data_image = null, data_description = null, data_path = null, data_select = ""){
  /*
    id: selection class or ID of component has use Select2
    data_id: id 
    data_text: text
    data_image: image
    data_path: path
    data_description: description
    data_select: String HTML Selection
  */ 
  var data_selection = {
    id         : data_id||null,
    text       : data_text||null,
    image      : data_image||null,
    path       : data_path||null,
    description: data_description||null
  }
  $(id)
      .empty()
      .append(data_select);
  //$(id).select2('data'  ,data_selection);
  $(id).append(data_selection).trigger('change');
  $(id).trigger({
    type: 'select2:select',
    params: {
        object: data_selection,
        formatSelection: formatRepoSelectionLanguage(data_selection),
        formatResult: formatRepoLanguage(data_selection)
    }
  });
}

这是格式RepoLanguage:

function formatRepoLanguage (repo) {
  if (repo.loading) {
    return repo.text;
  }

  var $container = $(
    "<div class='select2-result-language clearfix'>" +
      "<div class='select2-result-language__icon'><img src='" + base_url_icon + repo.image + "' /></div>" +
      "<div class='select2-result-language__meta'>" +
        "<div class='select2-result-language__title'></div>" +
        "<div class='select2-result-language__description'></div>" +
      "</div>" +
    "</div>"
  );

  $container.find(".select2-result-language__title").text(repo.text);
  $container.find(".select2-result-language__description").text(repo.path)

  return $container;
}

这是 formatRepoSelectionLanguage:

function formatRepoSelectionLanguage (state) {
  if (!state.id) {
    return state.text;
  }
  var $state = $(
    '<span id = "language_'+ state.id +'"><img class="img-flag" /> <span></span></span>'
  );

  // Use .text() instead of HTML string concatenation to avoid script injection issues
  $state.find("span").text(state.text);
  $state.find("img").attr("src", base_url_icon + state.image);

  return $state;
}

我已经尝试将数据(如字符串)设置为选项元素并且成功了!实际上,我认为这是 Select2 的一个错误,我使用的是 4.0.13 版,现在我认识到我不需要使用 select2 函数,因为它从我附加到 [= 的选项元素中获取数据12=]元素!这就是我对我的问题的回答,如果有人有好主意请告诉我!感谢您的宝贵时间!