About binding JSON data to Select2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.