Bootstrap treeview实现动态加载数据并添加快捷搜索(2)
3.js 脚本
$(function () { var defaultData; $.ajax({ type: "post", url: "项目请求路径方法.json", dataType: "json", success: function (result) { defaultData=result; var initSearchableTree = function() { return $('#treeview-searchable').treeview({ data: defaultData, nodeIcon: 'glyphicon glyphicon-globe', emptyIcon: '', //没有子节点的节点图标 //collapsed: true, }); }; var $searchableTree = initSearchableTree(); $('#treeview-searchable').treeview('collapseAll', { silent : false//设置初始化节点关闭 }); var findSearchableNodes = function() { return $searchableTree.treeview('search', [ $.trim($('#input-search').val()), { ignoreCase: false, exactMatch: false } ]); }; var searchableNodes = findSearchableNodes(); // Select/unselect/toggle nodes $('#input-search').on('keyup', function (e) { var str = $('#input-search').val(); if($.trim(str).length>0){ searchableNodes = findSearchableNodes(); } else { $('#treeview-searchable').treeview('collapseAll', { silent : false //设置初始化节点关闭 }); } //$('.select-node').prop('disabled', !(searchableNodes.length >= 1)); }); var search = function(e) { var pattern = $.trim($('#input-search').val()); var options = { ignoreCase: $('#chk-ignore-case').is(':checked'), exactMatch: $('#chk-exact-match').is(':checked'), revealResults: $('#chk-reveal-results').is(':checked') }; var results = $searchableTree.treeview('search', [ pattern, options ]); var output = '<p>' + results.length + ' 匹配的搜索结果</p>'; $.each(results, function (index, result) { output += '<p>- <span style="color:red;">' + result.text + '</span></p>'; }); $('#search-output').html(output); } $('#btn-search').on('click', search); $('#input-search').on('keyup', search); $('#btn-clear-search').on('click', function (e) { $searchableTree.treeview('clearSearch'); $('#input-search').val(''); $('#search-output').html(''); $('#treeview-searchable').treeview('collapseAll', { silent : false//设置初始化节点关闭 }); }); }, error: function () { alert("省市区地域信息加载失败!") } }); });
三、后台主要代码 后台采用SpringMVC+Spring+Mybatis框架, 以下为Controller层代码
@ResponseBody @RequestMapping(value = "/queryAreaInfo", method = { RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_VALUE) public List<Object> queryAreaInfo() { List<AreaVO> areaInfo=new ArrayList<>(); try { //获取缓存中的省市区信息(本项目直接从缓存中获取,也可以单独写方法到Service、Dao层查询) EcncSysConfig sysConfig = new EcncSysConfig(); areaInfo = sysConfig.INIT_SYS_ECNC_AREAVO; } catch (Throwable e) { e.printStackTrace(); } //盛放省份 List<Object> result=new ArrayList<>(); for (AreaVO areaVO : areaInfo) { Map<String, Object> map= new HashMap<>(); if("2".equals(areaVO.getGrade())){ map.put("text", areaVO.getName()); //盛放地市 List<Object> cList=new ArrayList<>(); for (AreaVO cVO : areaInfo) { Map<String, Object> cMap=new HashMap<>(); if (cVO.getParentId() != null && cVO.getParentId().equals(areaVO.getId())) { cMap.put("text", cVO.getName()); //盛放区县 List<Object> rList=new ArrayList<>(); for (AreaVO rVO : areaInfo) { Map<String, Object> rMap=new HashMap<>(); if (cVO.getId().equals(rVO.getParentId())) { rMap.put("text", rVO.getName()); rList.add(rMap); } } cMap.put("nodes", rList); cList.add(cMap); } } map.put("nodes", cList); result.add(map); } } return result; }
内容版权声明:除非注明,否则皆为本站原创文章。