/////////////////////////////////////////////html
id="gwTeacherName" value="" placeholder="请选择顾问" />
/////////////////////////////////////////////jsp方法
function selectContant(){
$("#gwTeacherName").select2({
placeholder: "可选",
loadMorePadding:20,
width:"200px",
allowClear: true,
ajax: {
url:"/ajaxSelectTeacherByName.htm?action=ajaxSelectTeacherByName",
dataType: "json",
type:"post",
data: function(term, page){
return{
term: term,
pageLimit: 10,
page:page,
};
},
results: function(data, page){
var more = (page * 10) < data['total'];
return{results:data['data'], more: more};
},
},
escapeMarkup: function(m){
return m;
},
initSelection:function (element, callback) {},
formatLoadMore: function (pageNumber) {return "加载更多部门…"; },
formatSearching: function () { return "正在搜索…"; },
formatNoMatches: function () { return "没有匹配的部门"; },
});
}
///////////////////////////////////////////////////后台代码
@ResponseBody
@RequestMapping(value={"ajaxSelectTeacherByName.htm"})
public List
try {
response.setHeader("Cache-Control", "no-cache");
response.setContentType("text/json;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
String term = request.getParameter("term");
Integer pageLimit = Integer.parseInt(request.getParameter("pageLimit"));
Integer page = Integer.parseInt(request.getParameter("page"));
JSONArray array = new JSONArray();
JSONObject json = new JSONObject();
DetachedCriteria dc = DetachedCriteria.forClass(Teachers.class);
DetachedCriteria dcCount = DetachedCriteria.forClass(Teachers.class);
if(StringUtils.isNotEmpty(term)){
dc.add(Restrictions.like("name", term, MatchMode.ANYWHERE));
dcCount.add(Restrictions.like("name", term, MatchMode.ANYWHERE));
}
dc.add(Restrictions.eq("deleteStatus", 0));
dc.add(Restrictions.ne("status", 2));
List
for (Teachers teachers2 : teachers) {
JSONObject jo = new JSONObject();
jo.put("id", teachers2.getName());
jo.put("text", teachers2.getName());
array.add(jo);
}
Long cnt = teachersService.getCount(dcCount);
json.put("data", array);
json.put("total", cnt);
PrintWriter out = response.getWriter();
out.print(json);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/////////////////////////置空$('#gwTeacherName').select2("val", "");
////////////////////回显$("#teacher").select2("data",{id:value.teacher.id,text:value.teacher.name});