Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
Datos AJAX en la plantilla de Select2

Estoy llenando Select2 con datos AJAX. Mi código es como a continuación.

 function formatState(state) { if (!state.id) { return state.text; } var output = $('<span></span>'); // I would like get data `text[1]` of `processResults` here. return output; }; //more code here cellEle.html(`<select multiple="multiple"></select>`); let selectEle = cellEle.children("select").select2({ //more code here ajax: { //more code here processResults: function (data) { var options = []; if (data) { $.each(data, function (index, text) { options.push({ id: index, text: text[0]}); }); } return { results: options, more: false }; }, }, templateSelection: formatState, });

Me gustaría obtener el text[1] de processResults dentro de formatState() de templateSelection .

Cómo puedo obtenerlo ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede pasar cualquier dato adicional a través de processResults . En tu caso puede ser

 var options = []; if (data) { $.each(data.items, function (index, text) { options.push({ id: index, text: text[0], text2: text[1] /* <-- extra data */}); }); } return { results: options, more: false };

Luego acceda a él en templateSelection

 if (!state.id) { return state.text; } return $(`<span>${state.text} & ${state.text2}</span>`);

http://jsfiddle.net/moshfeu/3hrupdn1/22/

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!