templates - Select2 不使用我的 templateResults 或 templateSelection 选项

标签 templates jquery-select2

我正在尝试将 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;
        }


    }
};

我得到的错误是 Uncaught TypeError: Cannot read property 'toUpperCase' of undefined在线 356 of select2.js版本: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 中。他们被称为 formatSelectionformatResult在 3.5.2.

关于templates - Select2 不使用我的 templateResults 或 templateSelection 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29146743/

相关文章:

c++ - 传递给 std::variant 的预定义类型列表

javascript - 如何格式化基于 ajax 的 Select2 预填充?

C++ 表达式模板 : operator what?

c++ - 给定 std::tuple<T...> 时可变参数模板类的实例化?

templates - 你如何传递多个对象去模板?

javascript - Select2.js v4.0 : how set the default selected value with a local array data source?

javascript - 如何将 select2 插件与 php 和 ajax 一起使用?

C++ 模板宏快捷方式

jquery - 以编程方式添加新的 jquery-select2-4 选项并重置搜索字段?

html - 使用 select2 时保留输入验证 CSS 类