bootstrapTable+treegrid树形表格

bootstrapTable+treegrid树形表格

效果入下:
在这里插入图片描述
data数据:
这里需要有个id和pid,就是下面需要用的idField和treeShowField属性

var data=[{
        "name":"状态",
        "orderBy":1,
        "description":null,
        "id":"297ea48b661ed59101661f8eaf150004",
        "code":"ZT",
        "zid":""
    },
    {
        "name":"服务区域",
        "orderBy":1,
        "description":null,
        "id":"402881a0670bff6401670c160b46007a",
        "code":"FWQY",
        "zid":""
    },
    {
        "name":"基本设置",
        "orderBy":1,
        "description":"基本设置模块",
        "id":"402882ae70ec9c190170eca02c94000b",
        "code":"Base",
        "zid":""
    },
    {
        "name":"生产资料建模",
        "orderBy":5,
        "description":"生产资料建模中的字典",
        "id":"402882ae7134e86b017135256c7000a7",
        "code":"FactoryData",
        "zid":""
    },
    {
        "name":"项目管理设置",
        "orderBy":6,
        "description":null,
        "id":"402882ae715ec26801715ee5428f0015",
        "code":"ProjctManger",
        "zid":""
    },
    {
        "name":"通用状态",
        "orderBy":0,
        "description":null,
        "id":"297ea48b66187b6a01661892133f0001",
        "code":"CommDataStatus",
        "zid":"297ea48b661ed59101661f8eaf150004"
    },
    {
        "name":"单据状态",
        "orderBy":0,
        "description":null,
        "id":"402881e06699ee6801669a8cec940007",
        "code":"OrderStatus",
        "zid":"297ea48b661ed59101661f8eaf150004"
    },
    {
        "name":"确认状态",
        "orderBy":0,
        "description":null,
        "id":"402881e066be9a7e0166be9f35ec0002",
        "code":"ConfirmStatus",
        "zid":"297ea48b661ed59101661f8eaf150004"
    },
    {
        "name":"费用标识",
        "orderBy":0,
        "description":null,
        "id":"402881e066be9a7e0166be9f7e110003",
        "code":"FeeSign",
        "zid":"297ea48b661ed59101661f8eaf150004"
    },
    {
        "name":"城市级别",
        "orderBy":0,
        "description":null,
        "id":"297ea48b661ed59101661f914a330007",
        "code":"CityLevel",
        "zid":"297ea48b661ed59101661f8eaf150004"
    },
    {
        "name":"区域状态",
        "orderBy":1,
        "description":null,
        "id":"297ea48b661ed59101661f95615f000e",
        "code":"status",
        "zid":"297ea48b661ed59101661f8eaf150004"
    },
    {
        "name":"单据类型",
        "orderBy":1,
        "description":null,
        "id":"4028f0816680f302016680fb2bc50001",
        "code":"OrderType",
        "zid":"297ea48b661ed59101661f8eaf150004"
    },
	{
        "name":"送货车型",
        "orderBy":0,
        "description":null,
        "id":"402881a0670bff6401670c166fa6007e",
        "code":"zlinterval",
        "zid":"402881a0670bff6401670c160b46007a"
    }
]

引入css和js


<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">

<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>

html代码

<!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 =var data=[{
        "name":"状态",
        "orderBy":1,
        "description":null,
        "id":"297ea48b661ed59101661f8eaf150004",
        "code":"ZT",
        "zid":""
    },
    {
        "name":"服务区域",
        "orderBy":1,
        "description":null,
        "id":"402881a0670bff6401670c160b46007a",
        "code":"FWQY",
        "zid":""
    },
    {
        "name":"基本设置",
        "orderBy":1,
        "description":"基本设置模块",
        "id":"402882ae70ec9c190170eca02c94000b",
        "code":"Base",
        "zid":""
    },
    {
        "name":"生产资料建模",
        "orderBy":5,
        "description":"生产资料建模中的字典",
        "id":"402882ae7134e86b017135256c7000a7",
        "code":"FactoryData",
        "zid":""
    },
    {
        "name":"项目管理设置",
        "orderBy":6,
        "description":null,
        "id":"402882ae715ec26801715ee5428f0015",
        "code":"ProjctManger",
        "zid":""
    },
    {
        "name":"通用状态",
        "orderBy":0,
        "description":null,
        "id":"297ea48b66187b6a01661892133f0001",
        "code":"CommDataStatus",
        "zid":"297ea48b661ed59101661f8eaf150004"
    },
    {
        "name":"单据状态",
        "orderBy":0,
        "description":null,
        "id":"402881e06699ee6801669a8cec940007",
        "code":"OrderStatus",
        "zid":"297ea48b661ed59101661f8eaf150004"
    },
    {
        "name":"确认状态",
        "orderBy":0,
        "description":null,
        "id":"402881e066be9a7e0166be9f35ec0002",
        "code":"ConfirmStatus",
        "zid":"297ea48b661ed59101661f8eaf150004"
    },
    {
        "name":"费用标识",
        "orderBy":0,
        "description":null,
        "id":"402881e066be9a7e0166be9f7e110003",
        "code":"FeeSign",
        "zid":"297ea48b661ed59101661f8eaf150004"
    },
    {
        "name":"城市级别",
        "orderBy":0,
        "description":null,
        "id":"297ea48b661ed59101661f914a330007",
        "code":"CityLevel",
        "zid":"297ea48b661ed59101661f8eaf150004"
    },
    {
        "name":"区域状态",
        "orderBy":1,
        "description":null,
        "id":"297ea48b661ed59101661f95615f000e",
        "code":"status",
        "zid":"297ea48b661ed59101661f8eaf150004"
    },
    {
        "name":"单据类型",
        "orderBy":1,
        "description":null,
        "id":"4028f0816680f302016680fb2bc50001",
        "code":"OrderType",
        "zid":"297ea48b661ed59101661f8eaf150004"
    },
	{
        "name":"送货车型",
        "orderBy":0,
        "description":null,
        "id":"402881a0670bff6401670c166fa6007e",
        "code":"zlinterval",
        "zid":"402881a0670bff6401670c160b46007a"
    }
];

$(function () {
        var $table = $("#gridTable");
        $table.bootstrapTable({
            data:data
            idField: 'id',
            clickToSelect: true,
            singleSelect: true,
            columns: [
                {
                    field: 'check', checkbox: true, formatter: function (value, row, index) {
                        if (row.check == true) {
                            //设置选中
                            return {checked: true};
                        }
                    }
                },
                {title: '名称', field: 'name', width: 200, align: 'left', sortable: true},
                {title: '编号', field: 'code', width: 200, align: 'center', sortable: true},
                {title: '排序', field: 'orderBy', width: 80, align: 'center', sortable: true},
                {title: "备注", field: "description", width: 200, align: "left", sortable: true}
            ],
            //在哪一列展开树形
            treeShowField: 'name',
            //指定父id列
            parentIdField: 'zid',
            onResetView: function (data) {
                $table.treegrid({
                    // initialState: 'collapsed', // 所有节点都折叠
                    initialState: 'expanded',// 所有节点都展开,默认展开
                    treeColumn: 1,  //设置点击树形位置
                     // expanderExpandedClass: 'glyphicon glyphicon-minus',  //图标样式
                     // expanderCollapsedClass: 'glyphicon glyphicon-plus',
                    onChange: function () {
                        $table.bootstrapTable('resetWidth');
                    }
                });
                 //只展开树形的第一级节点
                //$table.treegrid('getRootNodes').treegrid('expand');
            },
        });
    });
    </script>

注意事项:这里没有使用分页 数据是List对象,
bootstrapTable在使用分页的情况下 数据对象Map : total 总数、rows数据集

碰到的问题:使用bootstrap-table-treegrid插件来展示表格树,未进行treegrid配置的时候,表格正常展示,但配置之后数据就不展示了:一直报错。
在这里插入图片描述

查看数据库里的数据才发现用作指定父id列 parentIdField: 'zid’在数据库中根节点的值设为了root,但是treegrid要求当前记录的 parentIdField为 null 或者空字符串的时候,这个节点就是根节点,然后根据根节点去递归找子节点,不然就会报错。

引用:https://www.cnblogs.com/daniao11417/p/11425146.html

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值