2015-03-19 15 views
14

Sto provando a utilizzare la chiamata ajax select2 con i modelli. Sto ottenendo l'ajax bene, ma non sta usando le funzioni del mio modello.Select2 non utilizza il mio modelloRisultati o modelloSelezione opzioni

i dati 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"} 
] 

il codice select2 (che ho preso molto da 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; 
     } 


    } 
}; 

L'errore che sto ottenendo è Uncaught TypeError: Cannot read property 'toUpperCase' of undefined on line 356 di select2.js per Versione: 3.5.2.

Mi sembra che select2 non stia utilizzando la funzione templateSelection e templateResults.

risposta

38

Si sta guardando la documentazione 4.0.0 ma si sta utilizzando 3.5.2. You can still access the 3.5.2 documentation.

In particolare, le opzioni templateSelection e templateResult esistono solo nella 4.0.0. Si chiamavano formatSelection e formatResult in 3.5.2.

3

In select2 3.5.2., È "formatResult" (singolare, non formatResults)

Problemi correlati