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

时间:2015-03-19 14:06:07

标签: 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;
        }


    }
};

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

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

2 个答案:

答案 0 :(得分:40)

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

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

答案 1 :(得分:3)

在select2 3.5.2。中,它是&#34; formatResult&#34; (单数,不格式结果)