bootstrap-table+treegrid實(shí)現(xiàn)樹(shù)形表格
實(shí)現(xiàn)一個(gè)樹(shù)形表格的時(shí)候有多種方法:比如把 ztree 的樹(shù)形直接拼接成表格,或者用強(qiáng)大的 jqgrid 實(shí)現(xiàn),今天介紹一個(gè)比較輕量級(jí)的實(shí)現(xiàn):使用bootstrap-table + treegrid 。
1、引入 jquery.js、bootstrap-table.js、bootstrap-table-treegrid.js、jquery.treegrid.js 以及相應(yīng)的 css 文件:bootstrap.css、bootstrap-table.css、jquery.treegrid.css;
2、后臺(tái)傳到前臺(tái)的 json 必須含有 id、pid字段,有 id pid 才能形成樹(shù)結(jié)構(gòu)(這里為了演示,把 json 寫(xiě)成固定的了,實(shí)際中要從后臺(tái)獲?。?;
3、在使用過(guò)程中可以參考 bootstrap-table 的設(shè)置參數(shù),通過(guò)不同的設(shè)置以達(dá)到自己想要的效果;
完整代碼示例:
<!DOCTYPE HTML> <html lang="zh-cn"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta content="width=device-width,initial-scale=1.0" name="viewport"> <meta content="yes" name="apple-mobile-web-app-capable"> <meta content="black" name="apple-mobile-web-app-status-bar-style"> <meta content="telephone=no" name="format-detection"> <meta content="email=no" name="format-detection"> <title>系統(tǒng)管理</title> <link rel="stylesheet"> <link rel="stylesheet"> <link rel="stylesheet" href=https://cdn.bootcss.com/jquery-treegrid/0.2.0/css/jquery.treegrid.min.css > </head> <body> <div class="container"> <h1>樹(shù)形表格 : Table Treegrid</h1> <table id="table"></table> <br/> <button onclick="test()">選擇</button> </div> </body> <script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script> <script src="https://cdn.bootcss.com/bootstrap-table/1.12.1/bootstrap-table.min.js"></script> <script src="https://cdn.bootcss.com/bootstrap-table/1.12.0/extensions/treegrid/bootstrap-table-treegrid.js"></script> <script src="https://cdn.bootcss.com/jquery-treegrid/0.2.0/js/jquery.treegrid.min.js"></script> <script type="text/javascript"> var $table = $('#table'); var data = JSON.parse( '[{"id":1,"pid":0,"status":1,"name":"用戶管理","permissionValue":"open:user:manage"},' + '{"id":2,"pid":0,"status":1,"name":"系統(tǒng)管理","permissionValue":"open:system:manage"},' + '{"id":3,"pid":1,"status":1,"name":"新增用戶","permissionValue":"open:user:add"},' + '{"id":4,"pid":1,"status":1,"name":"修改用戶","permissionValue":"open:user:edit"},' + '{"id":5,"pid":1,"status":0,"name":"刪除用戶","permissionValue":"open:user:del"},' + '{"id":6,"pid":2,"status":1,"name":"系統(tǒng)配置管理","permissionValue":"open:systemconfig:manage"},' + '{"id":7,"pid":6,"status":1,"name":"新增配置","permissionValue":"open:systemconfig:add"},' + '{"id":8,"pid":6,"status":1,"name":"修改配置","permissionValue":"open:systemconfig:edit"},' + '{"id":9,"pid":6,"status":0,"name":"刪除配置","permissionValue":"open:systemconfig:del"},' + '{"id":10,"pid":2,"status":1,"name":"系統(tǒng)日志管理","permissionValue":"open:log:manage"},' + '{"id":11,"pid":10,"status":1,"name":"新增日志","permissionValue":"open:log:add"},' + '{"id":12,"pid":10,"status":1,"name":"修改日志","permissionValue":"open:log:edit"},' + '{"id":13,"pid":10,"status":0,"name":"刪除日志","permissionValue":"open:log:del"}]'); $(function() { //控制臺(tái)輸出一下數(shù)據(jù) console.log(data); $table.bootstrapTable({ data:data, idField: 'id', dataType:'jsonp', columns: [ { field: 'check', checkbox: true, formatter: function (value, row, index) { if (row.check == true) { // console.log(row.serverName); //設(shè)置選中 return { checked: true }; } } }, { field: 'name', title: '名稱(chēng)' }, // {field: 'id', title: '編號(hào)', sortable: true, align: 'center'}, // {field: 'pid', title: '所屬上級(jí)'}, { field: 'status', title: '狀態(tài)', sortable: true, align: 'center', formatter: 'statusFormatter' }, { field: 'permissionValue', title: '權(quán)限值' }, { field: 'operate', title: '操作', align: 'center', events : operateEvents, formatter: 'operateFormatter' }, ], // bootstrap-table-treegrid.js 插件配置 -- start //在哪一列展開(kāi)樹(shù)形 treeShowField: 'name', //指定父id列 parentIdField: 'pid', onResetView: function(data) { //console.log('load'); $table.treegrid({ initialState: 'collapsed',// 所有節(jié)點(diǎn)都折疊 // initialState: 'expanded',// 所有節(jié)點(diǎn)都展開(kāi),默認(rèn)展開(kāi) treeColumn: 1, // expanderExpandedClass: 'glyphicon glyphicon-minus', //圖標(biāo)樣式 // expanderCollapsedClass: 'glyphicon glyphicon-plus', onChange: function() { $table.bootstrapTable('resetWidth'); } }); //只展開(kāi)樹(shù)形的第一級(jí)節(jié)點(diǎn) $table.treegrid('getRootNodes').treegrid('expand'); }, onCheck:function(row){ var datas = $table.bootstrapTable('getData'); // 勾選子類(lèi) selectChilds(datas,row,"id","pid",true); // 勾選父類(lèi) selectParentChecked(datas,row,"id","pid") // 刷新數(shù)據(jù) $table.bootstrapTable('load', datas); }, onUncheck:function(row){ var datas = $table.bootstrapTable('getData'); selectChilds(datas,row,"id","pid",false); $table.bootstrapTable('load', datas); }, // bootstrap-table-treetreegrid.js 插件配置 -- end }); }); // 格式化按鈕 function operateFormatter(value, row, index) { return [ '<button type="button" class="RoleOfadd btn-small btn-primary" style="margin-right:15px;"><i class="fa fa-plus" ></i> 新增</button>', '<button type="button" class="RoleOfedit btn-small btn-primary" style="margin-right:15px;"><i class="fa fa-pencil-square-o" ></i> 修改</button>', '<button type="button" class="RoleOfdelete btn-small btn-primary" style="margin-right:15px;"><i class="fa fa-trash-o" ></i> 刪除</button>' ].join(''); } // 格式化類(lèi)型 function typeFormatter(value, row, index) { if (value === 'menu') { return '菜單'; } if (value === 'button') { return '按鈕'; } if (value === 'api') { return '接口'; } return '-'; } // 格式化狀態(tài) function statusFormatter(value, row, index) { if (value === 1) { return '<span class="label label-success">正常</span>'; } else { return '<span class="label label-default">鎖定</span>'; } } //初始化操作按鈕的方法 window.operateEvents = { 'click .RoleOfadd': function (e, value, row, index) { add(row.id); }, 'click .RoleOfdelete': function (e, value, row, index) { del(row.id); }, 'click .RoleOfedit': function (e, value, row, index) { update(row.id); } }; </script> <script> /** * 選中父項(xiàng)時(shí),同時(shí)選中子項(xiàng) * @param datas 所有的數(shù)據(jù) * @param row 當(dāng)前數(shù)據(jù) * @param id id 字段名 * @param pid 父id字段名 */ function selectChilds(datas,row,id,pid,checked) { for(var i in datas){ if(datas[i][pid] == row[id]){ datas[i].check=checked; selectChilds(datas,datas[i],id,pid,checked); }; } } function selectParentChecked(datas,row,id,pid){ for(var i in datas){ if(datas[i][id] == row[pid]){ datas[i].check=true; selectParentChecked(datas,datas[i],id,pid); }; } } function test() { var selRows = $table.bootstrapTable("getSelections"); if(selRows.length == 0){ alert("請(qǐng)至少選擇一行"); return; } var postData = ""; $.each(selRows,function(i) { postData += this.id; if (i < selRows.length - 1) { postData += ", "; } }); alert("你選中行的 id 為:"+postData); } function add(id) { alert("add 方法 , id = " + id); } function del(id) { alert("del 方法 , id = " + id); } function update(id) { alert("update 方法 , id = " + id); } </script> </html>
效果圖:
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊這里進(jìn)行學(xué)習(xí),再為大家附3個(gè)精彩的專(zhuān)題:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- bootstrap-table組合表頭的實(shí)現(xiàn)方法
- bootstrap-table實(shí)現(xiàn)表頭固定以及列固定的方法示例
- Bootstrap-table使用footerFormatter做統(tǒng)計(jì)列功能
- bootstrap-table實(shí)現(xiàn)服務(wù)器分頁(yè)的示例 (spring 后臺(tái))
- bootstrap-table.js擴(kuò)展分頁(yè)工具欄(增加跳轉(zhuǎn)到xx頁(yè))功能
- bootstrap-table formatter 使用vue組件的方法
- Bootstrap-table自定義可編輯每頁(yè)顯示記錄數(shù)
- Node.js中Bootstrap-table的兩種分頁(yè)的實(shí)現(xiàn)方法
- Bootstrap table學(xué)習(xí)筆記(2) 前后端分頁(yè)模糊查詢
- bootstrap table 服務(wù)器端分頁(yè)例子分享
- Bootstrap table分頁(yè)問(wèn)題匯總
- bootstrap-table后端分頁(yè)功能完整實(shí)例
相關(guān)文章
JS實(shí)現(xiàn)上傳圖片的三種方法并實(shí)現(xiàn)預(yù)覽圖片功能
在用戶注冊(cè)頁(yè)面,需要用戶在本地選擇一張圖片作為頭像,并同時(shí)預(yù)覽,實(shí)現(xiàn)思路有兩種,具體實(shí)現(xiàn)方法和實(shí)例代碼大家參考下本文2017-07-07微信小程序車(chē)牌號(hào)碼模擬鍵盤(pán)輸入功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了微信小程序車(chē)牌號(hào)碼模擬鍵盤(pán)輸入功能的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11BOM操作querySelector?querySeletorAll獲取標(biāo)簽對(duì)象
這篇文章主要為大家介紹了BOM操作querySelector?querySeletorAll獲取標(biāo)簽對(duì)象步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11給html超鏈接設(shè)置事件不使用href來(lái)完成跳
有時(shí)候我們需要使用a這個(gè)超級(jí)鏈接,而又不使用href來(lái)完成跳轉(zhuǎn),針對(duì)這個(gè)問(wèn)題,可以采用下面的解決方案2014-04-04