需要注意的是,pid也就查找的父元素的字段和父元素的id字段,数据格式需要保持一致,可以都是number类型,也可以都是string类型,两者需要统一
<!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>系统管理</title> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <link href="https://cdn.bootcss.com/bootstrap-table/1.11.1/bootstrap-table.min.css" 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/> </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 = [{ "time": "2019-08-27", "id": 1, "content": "内容1", "pid": "" }, { "time": "2019-08-27", "id": 2, "content": "内容2", "pid": 1 }, { "time": "2019-08-27", "id": 3, "content": "内容3", "pid": 1 }, { "time": "2019-08-27", "id": 4, "content": "内容4", "pid": "" }, { "time": "2019-08-27", "id": 5, "content": "内容5", "pid": 2 }, { "time": "2019-08-27", "id": 6, "content": "内容6", "pid": "" }, { "time": "2019-08-27", "id": 7, "content": "内容7", "pid": 6 }, ]; $(function() { $table.bootstrapTable({ data: data, idField: 'id', dataType: 'jsonp', columns: [{ field: 'time', title: '时间', width: 140 }, { field: 'content', title: '主要内容' }, ], //在哪一列展开树形 treeShowField: 'time', //指定父id列 parentIdField: 'pid', onResetView: function(data) { //console.log('load'); $table.treegrid({ initialState: 'collapsed', // 所有节点都折叠 // initialState: 'expanded',// 所有节点都展开,默认展开 treeColumn: 0, // expanderExpandedClass: 'glyphicon glyphicon-minus', //图标样式 // expanderCollapsedClass: 'glyphicon glyphicon-plus', onChange: function() { $table.bootstrapTable('resetWidth'); } }); //只展开树形的第一级节点 //$table.treegrid('getRootNodes').treegrid('expand'); }, }); }); </script>