Layui如何使用折叠表格,以及默认自动折叠

时间:2022-10-18
我们有时在开发项目是需要用到树形接口,这时我们可以 Layui 的树形表格treetable插件。如果需要将树形表格保持展开要怎么操作呢?下面我们来看下参数:

参数说明:

参数

类型

是否必填

描述

 

treeColIndex

int

树形图标显示在第几列

 

treeSpid

object

最上级的父级id

 

treeIdName

string

id字段的名称

 

treePidName

string

pid字段的名称

 

treeDefaultClose

boolean

是否默认折叠

 

treeLinkage

boolean

父级展开时是否自动展开所有子级

 

 HTML代码:
<div>
    <div>
        <div>
            <script type="text/html" id="toolbarDemo">
                <div>
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
                </div>
            </script>
            <table id="munu-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
Js代码:
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;
        // 渲染表格
        layer.load(2);
        treetable.render({
            toolbar: '#toolbarDemo',
            treeColIndex: 1,
            treeSpid: -1,
            treeIdName: 'id',
            treePidName: 'pid',
            elem: '#munu-table',
            url: '{:url("union/Permission/getPermission")}',
            // url: '__STATIC__/union/api/menus.json',
            page: false,
            treeDefaultClose: true,
            cols: [[
                {type: 'numbers'},
                {field: 'name', minWidth: 200, title: '权限名称'},
                {field: 'ac', title: '方法'},
                {field: 'co', title: '控制器'},
                {field: 'url', title: '菜单url'},
                {field: 'ico', title: '图标'},
                {field: 'sort', width: 80, align: 'center', title: '排序号'},
                {
                    field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                        if (d.is_menu == 1) {
                            return '<span class="layui-badge layui-bg-gray">按钮</span>';
                        }else if (d.is_menu == 2) {
                            return '<span class="layui-badge layui-bg-gray">其他</span>';
                        }else {
                            return '<span class="layui-badge-rim layui-bg-blue">菜单</span>';
                        }
                    }, title: '类型'
                },
                {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });
 
        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });
 
        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });
 
        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
 
            if (layEvent === 'del') {
                layer.msg('删除' + data.id);
            } else if (layEvent === 'edit') {
                layer.msg('修改' + data.id);
            }
        });
    });
</script>
Json结构数据格式:
{
  "code": 0,
  "msg": "",
  "count": 19,
  "data": [
    {
      "authorityId": 1,
      "authorityName": "系统管理",
      "orderNumber": 1,
      "menuUrl": null,
      "menuIcon": "layui-icon-set",
      "createTime": "2018/06/29 11:05:41",
      "authority": null,
      "checked": 0,
      "updateTime": "2018/07/13 09:13:42",
      "isMenu": 0,
      "parentId": -1
    },
    {
      "authorityId": 2,
      "authorityName": "用户管理",
      "orderNumber": 2,
      "menuUrl": "system/user",
      "menuIcon": null,
      "createTime": "2018/06/29 11:05:41",
      "authority": null,
      "checked": 0,
      "updateTime": "2018/07/13 09:13:42",
      "isMenu": 0,
      "parentId": 1
                },
]
}
 
下一条:没有了 下一条:解决layui element树形表格全选失效,刷新保持原数据选中

相关文章

最新文章