Select2 不使用我的 templateResults 或 templateSelection 选项

Select2 not using my templateResults or templateSelection options

我正在尝试使用带有模板的 select2 ajax 调用。我得到 ajax 就好了,但它没有使用我的模板函数。

ajax数据是:

[
   {"name":"First thing","otherData":"asdfg"},
   {"name":"Second thing","otherData":"qqerr"},
   {"name":"third thing","otherData":"yerty"},
   {"name":"fourth thing","otherData":"hgjfgh"},
   {"name":"fifth thing","otherData":"fhgkk"}
]

select2 代码(我从 here 中获取了很多)是:

FundSearch = {
    create: function (selector, theURL) {
      $(selector).select2({
        ajax: {
          url: theURL,
          dataType: 'json',
          delay: 250,
          data: function (params) {
            console.log(params.term);
            return {
              q: params.term, // search term
              page: params.page
            };
          },
          results: function (data, page) {
            // parse the results into the format expected by Select2.
            // since we are using custom formatting functions we do not need to
            // alter the remote JSON data
            return {
              results: data
            };
          },
          cache: true
        },
        escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
        minimumInputLength: 1,
        templateResult: formatData,
        templateSelection: formatDataSelection
      });

      function formatData (data) {
          if (data.loading) return data.name;

          markup = "<h1>" + data.name + "</h1>" + "<p>" + data.otherData + "</p>";

          return markup;
        }

        function formatDataSelection (data) {
          return data.name;
        }


    }
};

我得到的错误是 select2.js 第 356 行的 Uncaught TypeError: Cannot read property 'toUpperCase' of undefined 版本:3.5.2.

在我看来,select2 没有使用我的 templateSelection 和 templateResults 函数。

您正在查看 4.0.0 文档,但使用的是 3.5.2。 You can still access the 3.5.2 documentation.

具体来说,templateSelectiontemplateResult 选项仅存在于 4.0.0 中。它们在 3.5.2 中被称为 formatSelectionformatResult

在select2 3.5.2.中,是"formatResult"(单数,不是formatResults)