javascript - Select2; how to load in an array that I load once from the database -
i loading array results of following action:
public actionresult getjudges(string q) { var judges = new list<judge>(); using (var con = new sqlconnection(configurationmanager.connectionstrings["db"].connectionstring)) { using (var cmd = con.createcommand()) { cmd.commandtype = commandtype.text; cmd.commandtext = "query"; con.open(); using (var rdr = cmd.executereader()) { while (rdr.read()) { var judge = default(judge); var id = convert.toint32(rdr["id"]); var email = convert.tostring(rdr["email"]); var name = convert.tostring(rdr["name"]); var jurisdiction = convert.tostring(rdr["jurisdiction"]); var code= convert.tostring(rdr["code"]); if (!judges.select(x => x.id).contains(id)) { judge = new judge { id = id, email = email, name = name, code= code }; judges.add(judge); } else { judge = judges.firstordefault(x => x.id == id); } judge.jurisdictions.add(jurisdiction); } } con.close(); } } return json(judges, jsonrequestbehavior.allowget); }
i trying load array select2 element, , having no success. select2 element empty. here javascript using load select2:
var judges = []; $(document).ready(function () { $.getjson('/home/getjudges', function (result) { judges = result; $(".select2").select2({ placeholder: "search judge", data: { results: result, id: "id", text: "text" } }); }); });
can tell me missing here?
look @ this example on how use select2. passing data incorrectly. result
should contain array of objects have id
property , text
property, , should passing select2 data
directly, rather wrapping in object.
//sample data var result = [{id: 0, text: 'judge 1'}, {id: 1, text: 'judge 2'}]; $(".select2").select2({ placeholder: "search judge", data: result });
Comments
Post a Comment