select2下拉內容獲取后台數據


controller(id給select;text給另外的input框)
@RequestMapping(value = "findUnit")
public @ResponseBody
JSONArray findUnit(TPetitionReport tPetitionReport, RedirectAttributes redirectAttributes) {
Dict dict = new Dict();
dict.setType("work_space");
List<Dict> list = dictService.findList(dict);
String st = "[";//構造json
st+="{id: '" + list.get(0).getValue()
+ "',text: '" + list.get(0).getLabel()+"'}";
for(int i= 1;i<list.size();i++){
st+=",{id: '" + list.get(i).getValue()
+ "',text: '" + list.get(i).getLabel()+"'}";
}
st+="]";
JSONArray json = JSONArray.fromObject(st);
return json;
}


jsp
$("#shouli"+index).select2({
ajax: {
url: "${ctx}/xffy/tPetitionReport/findShouli",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term
};
},
results: function (data) {
return {
results: data
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; },
formatSelection : function formatRepo(repo) { //選中一個后返回值
$("#score1"+index).val(repo.text);
return repo.id;
},
formatResult: function formatRepoSelection(repo) { //返回所有內容到下拉框
return repo.id;
},
initSelection: function (element, callback) { // 初始化時設置默認值
var id = $(element).val()==null?"請選擇":$(element).val();
var text = $("#score1"+index).val()==null?"0":$("#score1"+index).val(); //把text的值傳給input
var data = { id: id, text: text};//這里是初始化的數據,你可以通過id來從服務器上獲取(ajax),再裝載進去
callback(data);
}
});


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM