Select2 not using my templateResults or templateSelection options
Asked Answered
K

2

16

I am trying to use the select2 ajax call with templates. I am getting the ajax in just fine but it is not using my template functions.

the ajax data is:

[
   {"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"}
]

the select2 code (which i took a lot from here ) is:

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


    }
};

The error I am getting is Uncaught TypeError: Cannot read property 'toUpperCase' of undefined on line 356 of select2.js for Version: 3.5.2.

It seems to me that select2 is not using my templateSelection and templateResults function.

Kinard answered 19/3, 2015 at 14:6 Comment(0)
F
48

You are looking at the 4.0.0 documentation but are using 3.5.2. You can still access the 3.5.2 documentation.

Specifically, the templateSelection and templateResult options only exist in 4.0.0. They were called formatSelection and formatResult in 3.5.2.

Few answered 19/3, 2015 at 14:48 Comment(0)
C
3

In select2 3.5.2., it is "formatResult" (singular, not formatResults)

Coast answered 26/5, 2015 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.