国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

bootstrap-table+treegrid實現(xiàn)樹形表格

2024-05-06 15:36:37
字體:
供稿:網(wǎng)友

實現(xiàn)一個樹形表格的時候有多種方法:比如把 ztree 的樹形直接拼接成表格,或者用強大的 jqgrid 實現(xiàn),今天介紹一個比較輕量級的實現(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、后臺傳到前臺的 json 必須含有 id、pid字段,有 id pid 才能形成樹結(jié)構(gòu)(這里為了演示,把 json 寫成固定的了,實際中要從后臺獲取);
3、在使用過程中可以參考 bootstrap-table 的設(shè)置參數(shù),通過不同的設(shè)置以達到自己想要的效果;

完整代碼示例:

<!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="external nofollow" 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>樹形表格 : 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() { //控制臺輸出一下數(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: '名稱' },  // {field: 'id', title: '編號', sortable: true, align: 'center'},  // {field: 'pid', title: '所屬上級'},  { 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  //在哪一列展開樹形  treeShowField: 'name',  //指定父id列  parentIdField: 'pid',  onResetView: function(data) {  //console.log('load');  $table.treegrid({   initialState: 'collapsed',// 所有節(jié)點都折疊   // initialState: 'expanded',// 所有節(jié)點都展開,默認展開   treeColumn: 1,   // expanderExpandedClass: 'glyphicon glyphicon-minus', //圖標樣式   // expanderCollapsedClass: 'glyphicon glyphicon-plus',   onChange: function() {   $table.bootstrapTable('resetWidth');   }  });  //只展開樹形的第一級節(jié)點  $table.treegrid('getRootNodes').treegrid('expand');  },  onCheck:function(row){  var datas = $table.bootstrapTable('getData');  // 勾選子類  selectChilds(datas,row,"id","pid",true);  // 勾選父類  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(''); } // 格式化類型 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> /** * 選中父項時,同時選中子項 * @param datas 所有的數(shù)據(jù) * @param row 當前數(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("請至少選擇一行");  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>            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 休宁县| 安溪县| 铁岭市| 磴口县| 南皮县| 永靖县| 伽师县| 瑞安市| 婺源县| 卫辉市| 辛集市| 泰顺县| 友谊县| 顺昌县| 井研县| 桐乡市| 延寿县| 遂宁市| 苏尼特右旗| 邯郸县| 栾川县| 喀喇| 平和县| 佛冈县| 聂荣县| 东港市| 莱西市| 西充县| 通许县| 吉林省| 成安县| 安阳县| 上栗县| 余姚市| 黄龙县| 秀山| 富裕县| 青阳县| 宁远县| 台东市| 营口市|