Recently done a project to use Select2, want to bind JSON data into Select2, select2 the default acceptable JSON format of the data is {ID: "", Text: "} Such a key value pair to save, so new into an array to do the data processing
This is what I do in the Ajax callback function, which is directly bound to the Select2 plugin after processing.
1 //Query Company Methods2 functionQuerycompany () {3 $.ajax ({4Type: "POST",5DataType: "JSON",6URL: "Handler/report_monitoringrecord.ashx",7Successfunction(data) {8 //Console.log ("Successful Execution");9 //console.log (data);Ten varOptions =NewArray (); One$ (data). each (function(i, O) { A -Options.push ({//get select2 required fields, ID and text - Id:o.companyid, the Text:o.company_name - }); - }); - //console.log (options); +$ ("#companySelect"). Select2 ({ - data:options + }) A at }, -Errorfunction () { -Console.log ("Execution failed"); - } - }); -}
About binding JSON data to Select2