Easy-to-Use TreeTable Plugin

Add-ons
2026-07-26 04:15 4 次浏览 • 0 次下载 112.7KB
Easy-to-Use TreeTable Plugin

Note: An external JSON data file is loaded. Local previews may encounter cross-origin issues, so the project must be run on a server.

treetable-lay

Implementation of the layui Tree Table treeTable

1. Introduction

Implemented as an extension of the layui data table.

2. Usage method

2.12. Import the Module

Download the entire module/treetable-lay folder and place it in your project. Then use it through the module loading mechanism:

1
2
3
4
5
6
7
8
layui.config({
base: 'module/'
}).extend({
treetable: 'treetable-lay/treetable'
}).use(['treetable'], function () {
var treetable = layui.treetable;
});

2.2.Render the Table

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<table id="table1" class="layui-table" lay-filter="table1"></table>
<script>
layui.use(['treetable'], function () {
var treetable = layui.treetable;
// Render the table
treetable.render({
treeColIndex: 2, // New treeTable parameter
treeSpid: -1, // New treeTable parameter
treeIdName: 'd_id', // New treeTable parameter
treePidName: 'd_pid', // New treeTable parameter
treeDefaultClose: true, // New treeTable parameter
treeLinkage: true, // New treeTable parameter
elem: '#table1',
url: 'json/data1.json',
cols: [[
{type: 'numbers'},
{field: 'id', title: 'id'},
{field: 'name', title: 'name'},
{field: 'sex', title: 'sex'},
{field: 'pid', title: 'pid'},
]]
});
});
</script>

Note:

You can pass data using either url or data. When using url, the parameters are passed through the where field, consistent with the usage of the layui data table.

Data Format

In short, the data must use the id and pid structure rather than the subMenus structure. Of course, the names of the id and pid fields can be customized:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"code": 0,
"msg": "ok",
"data": [{
"id": 1,
"name": "xx",
"sex": "male",
"pid": -1
},{
"id": 2,
"name": "xx",
"sex": "male",
"pid": 1
}
]
}

2.3.Parameter Description

All parameters supported by the layui data table are available. In addition, treeTable introduces the following parameters:

ParametersTypeRequiredDescription
treeColIndexintYesColumn index where the tree icons are displayed
treeSpidobjectYesID of the top-level parent
treeIdNamestringNoName of the id field
treePidNamestringNoName of the pid field
treeDefaultClosebooleanNoWhether to collapse by default
treeLinkagebooleanNoWhether to automatically expand all child nodes when a parent node is expanded

treeColIndex

Column index where the tree icons (arrows, folders, and file icons) are displayed. The index corresponds to the array index in the cols array.

treeSpid

The ID of the top-level parent. For example, you can define pid as 0 or -1 to represent the top-level directory.

treeIdName

treetable renders a tree structure using the id and pid fields. If your data does not contain id and pid fields, you can specify custom field names for them.

treePidName

The name of the pid field in your data.

treeDefaultClose

By default, all nodes are expanded. To collapse all nodes by default, simply add treeDefaultClose:true.

treeLinkage

Whether to automatically expand all child nodes when a parent node is expanded

2.4.Note

  • Pagination is not supported. Even if you set page:true, this parameter will be ignored.

  • Sorting is not supported. Do not enable sorting.

  • table.reload() cannot refresh the table. Please refer to the refresh implementation in the demo.

  • Except for the treetable.xxx methods documented in the documentation, use table.xxx for all other data table methods.

  • It is recommended to refresh (re-render) the table after completing delete or update requests on the backend. Avoid using obj.delete to delete records.

2.5.Other Methods

Expand All

1
treetable.expandAll('#table1');

Collapse All

1
treetable.foldAll('#table1');

2.6.How to Change Icons

Modify icons using CSS. The content property contains the Unicode character of the icon.

Change the folder icons:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/** Collapsed */
.treeTable-icon .layui-icon-layer:before {
content: "\e638";
}
/** Expanded */
.treeTable-icon.open .layui-icon-layer:before {
content: "\e638";
}
Change the file icons:
.treeTable-icon .layui-icon-file:before {
content: "\e621";
}
Change the arrow icons:
/** Collapsed */
.treeTable-icon .layui-icon-triangle-d:before {
content: "\e623";
}
/** Expanded */
.treeTable-icon.open .layui-icon-triangle-d:before {
content: "\e625";
}
星河码客

发现和分享优质开源项目,传承互联网精神,为开发者服务。

关于本站

联系我们

微信/广告合作:ITcloub (备注星河码客)

免责声明:本站资源版权归原作者所有,仅供学习与交流。如有侵权,请通过「关于我们 - 侵权投诉」联系删除。

Powered by Nuxt · Deployed on Cloudflare