select2插件官方下载地址:http://select2.github.io/
下载好之后,主要使用到的是select2.js,对应的语言提示以及样式select2.css,可以复制到自己的项目中
页面:
<select class="select2-multiple" multiple="multiple" style=" 100%" name="system" id="system"> </select>
前端脚本:
$('.select2-multiple').select2({ ajax: { url: '/admin/search-system', dataType: 'json', type: 'POST', delay: 250, data: function (params) { var param = new Object(); param.condition = params.term; return param; }, processResults: function (resp) { var array = new Array(); if (resp.system) { for (var i = 0; i < resp.syetem.length; i++) { var client = resp.client[i]; array.push({ id: system.systemId, text: system.systemId + ',' + system.systemName }); } } var ret = new Object(); ret.results = array; return ret; }, cache: true }, placeholder: '请输入关键字', });
后端数据源:
@RequestMapping(value = "search-system", method = RequestMethod.POST) @ResponseBody public JSONObject searchSystem(HttpServletRequest request, String condition) { JSONObject jsonObject = new JSONObject(); List<System> systemList = searchSystemByCondition(condition); JSONArray jsonArray = JSONArray.fromObject(systemList); jsonObject.put("system", jsonArray);return jsonObject; }
参考链接: