This article implements the use of bootstrap treeview to dynamically load data, and adds a quick search function. Friends who need it can refer to it
Write in front:
The jQuery multi-level list tree plug-in is based on Bootstrap and displays some inheritance tree structures, such as view trees, list trees, etc., in a simple and elegant way.
Practical Bootstrap tree menu special effects plug-in Bootstrap Tree View, a very good Bootstrap plug-in. Now many pages produced by Bootstrap require this function. This plug-in requires Bootstrap 3 version and jQuery 2.0 or above version support, and supports many parameters The customization functions, such as colors, background colors, icons, links, etc., are still very good.
For specific effects, please temporarily move to: http://jonmiles.github.io/bootstrap-treeview/
Okay, without further ado, let’s get started.
Requires users to quickly query the regional name information of provinces and cities (for user reference, used to standardize writing and import templates), and display it in a tree structure.
Final effect display:
1. Data table structure The data source is the provincial and municipal regional information table, which is a self-associated structure. As shown in the picture:
2. Front page
1. Page introduction
2. Page display area
快捷搜索
**_ _**
省市区名称层级树
查询结果展示
3.js script
$(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 = '' + results.length + ' 匹配的搜索结果
'; $.each(results, function (index, result) { output += '- ' + result.text + '
'; }); $('#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("省市区地域信息加载失败!") } }); });
3. The main code in the background uses the SpringMVC Spring Mybatis framework. The following is the Controller layer code
@ResponseBody @RequestMapping(value = "/queryAreaInfo", method = { RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_VALUE) public List
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future. help.
Related articles:
Using ui-route to implement multi-layer nested routing in AngularJS (detailed tutorial) Using Vue jquery How to shrink text in specified columns of a table How to implement shopping mall using vue How to implement web mouse effects (detailed tutorial)The above is the detailed content of How to dynamically load data using treeview in the Bootstrap framework. For more information, please follow other related articles on the PHP Chinese website!