js实现树状表

项目中需要展示表格结构为父子关系,如图所示
在这里插入图片描述
代码如下

//bootstrap代码
		function tableList(){
			$('#' + id).treegrid({
                pagination: false,
                pageNumber: 10,
                rowStyle: rowStyle,
                data: datas,
                idField: 'DBid',
                treeField: 'year',
                description: true,
                autowidth: true,
                shrinkToFit: true, //表格宽度自动充满
                detailView: true,
                columns: isdata,
                onLoadSuccess: function() {
                    $('#' + id).treegrid("collapseAll");
                    // $('#' + id).treegrid("expandAll");
                },
                onExpand: function() {
                    let heights
                    if (textid == 'recommend1') {
                        heights = $(`.jobInfoRecomand1 .datagrid-view2 .datagrid-body tbody:eq(0)`).innerHeight() * 1 + $(`.jobInfoRecomand1 .datagrid-view2 .datagrid-header`).innerHeight() * 1 + 20
                    } else if (textid == 'recommend2') {
                        heights = $(`.jobInfoRecomand2 .datagrid-view2 .datagrid-body tbody:eq(0)`).innerHeight() * 1 + $(`.jobInfoRecomand2 .datagrid-view2 .datagrid-header`).innerHeight() * 1 + 22
                    }
                    $('.panel-body-noheader').css("height", `${heights}px`);
                },
                onCollapse: function() {
                    let heights
                    if (textid == 'recommend1') {
                        heights = $(`.jobInfoRecomand1 .datagrid-view2 .datagrid-body tbody:eq(0)`).innerHeight() * 1 + $(`.jobInfoRecomand1 .datagrid-view2 .datagrid-header`).innerHeight() * 1 + 20
                    } else if (textid == 'recommend2') {
                        heights = $(`.jobInfoRecomand2 .datagrid-view2 .datagrid-body tbody:eq(0)`).innerHeight() * 1 + $(`.jobInfoRecomand2 .datagrid-view2 .datagrid-header`).innerHeight() * 1 + 22
                    }
                    $('.panel-body-noheader').css("height", `${heights}px`);
                }
            })

            if (textid == 'recommend1') {
                $('.jobInfoRecomand1 .datagrid-view2 .datagrid-body').attr('id', 'datagrid-body-id1')
                mainlayuiPages('datagrid-body-id1', 'recommendPage1');
            } else if (textid == 'recommend2') {
                $('.jobInfoRecomand2 .datagrid-view2 .datagrid-body').attr('id', 'datagrid-body-id2')
                mainlayuiPages('datagrid-body-id2', 'recommendPage2');
            }
		}
            
		function mainlayuiPages(tableID, pageDIV) {
            var workTablePageData = [];
            var workTablePageDatas = [];
            $(`#${tableID} tbody .treegrid-tr-tree`).find('tr').each(function(index, item) {
                workTablePageDatas.push($(item)[0].outerHTML);
            })
            $(`#${tableID} tbody`).find('tr').each(function(index, item) {
                if (workTablePageDatas.indexOf($(item)[0].outerHTML) == -1) {
                    workTablePageData.push($(item)[0].outerHTML + '');
                }
            })
            layui.use('laypage', function() {
                var laypage = layui.laypage;
                laypage.render({
                    elem: pageDIV,
                    limit: 8,
                    layout: ['prev', 'page', 'next'],
                    count: workTablePageData.length,
                    jump: function(obj) {
                        var isa = []
                        for (let i in workTablePageData.concat()) {
                            if (workTablePageData.concat()[i].indexOf("treegrid-tr-tree") != -1) {
                                isa.push(i)
                            }
                        }
                        //模拟渲染
                        $('#' + tableID + ' tbody')[0].innerHTML = function() {
                            var arr = [],
                                thisData = workTablePageData.concat().splice(obj.curr * obj.limit - obj.limit, isa.indexOf(`${obj.curr * obj.limit}`) > -1 ? obj.limit + 1 : obj.limit);
                            layui.each(thisData, function(index, item) {
                                arr.push(item);
                            });
                            return arr.join('');
                        }();
                        let heights
                        if (tableID == 'datagrid-body-id1') {
                            heights = $(`#${tableID} tbody:eq(0)`).innerHeight() * 1 + $(`.datagrid-view2 .datagrid-header`).innerHeight() * 1 + 22
                        } else if (tableID == 'datagrid-body-id2') {
                            heights = $(`#${tableID} tbody:eq(0)`).innerHeight() * 1 + $(`.datagrid-view2 .datagrid-header`).innerHeight() * 1 + 22
                        }
                        $('.panel-body-noheader').css("height", `${heights}px`);
                        $('.datagrid-view').css("height", `${heights}px`);
                        $('.datagrid-body').css("height", `${heights}px`);
                    }
                });
            });
        }

以此记录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值