选择2不使用我的templateResults或templateSelection选项

Bwa*_*ata 15 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"}
]
Run Code Online (Sandbox Code Playgroud)

select2代码(我从这里拿了很多)是:

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


    }
};
Run Code Online (Sandbox Code Playgroud)

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

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

Kev*_*own 42

您正在查看4.0.0文档,但使用的是3.5.2.您仍然可以访问3.5.2文档.

具体来说,templateSelectiontemplateResult选项仅存在于4.0.0中.他们被称为formatSelectionformatResult3.5.2.