layui/examples/tree.html

254 lines
5.6 KiB
HTML
Raw Normal View History

2017-08-21 00:51:13 +00:00
<!DOCTYPE html>
<html>
<head>
2019-05-31 06:36:29 +00:00
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
2023-07-12 07:31:42 +00:00
<title>树组件 - layui</title>
<link rel="stylesheet" href="../src/css/layui.css" media="all">
2019-05-31 06:36:29 +00:00
<style>
2019-06-06 04:19:43 +00:00
body{padding: 100px;}
2019-05-31 06:36:29 +00:00
#test1,#test2{margin-bottom: 100px; width: 400px;}
</style>
2017-08-21 00:51:13 +00:00
</head>
<body>
2019-06-06 04:19:43 +00:00
<div class="layui-btn-container">
<button type="button" class="layui-btn" lay-demo="getChecked">获取选中数据</button>
<button type="button" class="layui-btn" lay-demo="setChecked">设置节点勾选</button>
<button type="button" class="layui-btn" lay-demo="reload">重载实例</button>
</div>
<div class="layui-form">
<div class="layui-form-item">
<label class="layui-form-label">表单内</label>
<div class="layui-input-block">
</div>
</div>
2019-05-31 06:36:29 +00:00
<div id="test1"></div>
<div id="test2"></div>
2017-08-21 00:51:13 +00:00
<script src="../src/layui.js"></script>
2017-08-21 00:51:13 +00:00
<script>
2019-06-06 04:19:43 +00:00
layui.use(['tree', 'layer', 'util'], function(){
var $ = layui.$
,tree = layui.tree
2019-05-31 06:36:29 +00:00
,layer = layui.layer
2019-06-06 04:19:43 +00:00
,util = layui.util
2019-05-31 06:36:29 +00:00
,index = 100;
//数据源
var data1 = [{
2023-07-12 07:31:42 +00:00
title: 'A-1'
2019-05-31 06:36:29 +00:00
,id: 1
,children: [{
2023-07-12 07:31:42 +00:00
title: 'B-1-1'
2019-05-31 06:36:29 +00:00
,id: 3
,href: 'https://www.layui.com/doc/'
,children: [{
2023-07-12 07:31:42 +00:00
title: 'C-1-1-3'
2019-05-31 06:36:29 +00:00
,id: 23
,children: [{
2023-07-12 07:31:42 +00:00
title: 'D-1-1-3-1'
2019-05-31 06:36:29 +00:00
,id: 24
,children: [{
2023-07-12 07:31:42 +00:00
title: 'E-1-1-3-1-1'
2019-05-31 06:36:29 +00:00
,id: 30
},{
2023-07-12 07:31:42 +00:00
title: 'E-1-1-3-1-2'
2019-05-31 06:36:29 +00:00
,id: 31
}]
}]
},{
2023-07-12 07:31:42 +00:00
title: 'C-1-1-1'
2019-05-31 06:36:29 +00:00
,id: 7
2019-06-06 04:19:43 +00:00
,checked: true
2019-05-31 06:36:29 +00:00
,children: [{
2023-07-12 07:31:42 +00:00
title: 'D-1-1-1-1'
2019-05-31 06:36:29 +00:00
,id: 15
2019-09-10 00:37:47 +00:00
//,checked: true
2019-05-31 06:36:29 +00:00
,href: 'https://www.layui.com/doc/base/infrastructure.html'
}]
},{
2023-07-12 07:31:42 +00:00
title: 'C-1-1-2'
2019-05-31 06:36:29 +00:00
,id: 8
,children: [{
2023-07-12 07:31:42 +00:00
title: 'D-1-1-2-1'
2019-05-31 06:36:29 +00:00
,id: 32
}]
}]
},{
2023-07-12 07:31:42 +00:00
title: 'B-1-2'
2019-05-31 06:36:29 +00:00
,id: 4
2019-06-06 04:19:43 +00:00
,spread: true
2019-05-31 06:36:29 +00:00
,children: [{
2023-07-12 07:31:42 +00:00
title: 'C-1-2-1'
2019-05-31 06:36:29 +00:00
,id: 9
2019-06-06 04:19:43 +00:00
,checked: true
2019-05-31 06:36:29 +00:00
,disabled: true
},{
2023-07-12 07:31:42 +00:00
title: 'C-1-2-2'
2019-05-31 06:36:29 +00:00
,id: 10
}]
},{
2023-07-12 07:31:42 +00:00
title: 'B-1-3'
2019-05-31 06:36:29 +00:00
,id: 20
,children: [{
2023-07-12 07:31:42 +00:00
title: 'C-1-3-1'
2019-05-31 06:36:29 +00:00
,id: 21
},{
2023-07-12 07:31:42 +00:00
title: 'C-1-3-2'
2019-05-31 06:36:29 +00:00
,id: 22
}]
}]
},{
2023-07-12 07:31:42 +00:00
title: 'A-2'
2019-05-31 06:36:29 +00:00
,id: 2
2019-06-06 04:19:43 +00:00
,spread: true
2019-05-31 06:36:29 +00:00
,children: [{
2023-07-12 07:31:42 +00:00
title: 'B-2-1'
2019-05-31 06:36:29 +00:00
,id: 5
2019-06-06 04:19:43 +00:00
,spread: true
2019-05-31 06:36:29 +00:00
,children: [{
2023-07-12 07:31:42 +00:00
title: 'C-2-1-1'
2019-05-31 06:36:29 +00:00
,id: 11
},{
2023-07-12 07:31:42 +00:00
title: 'C-2-1-2'
2019-05-31 06:36:29 +00:00
,id: 12
}]
},{
2023-07-12 07:31:42 +00:00
title: 'B-2-2'
2019-05-31 06:36:29 +00:00
,id: 6
2019-06-06 04:19:43 +00:00
,checked: true
2019-05-31 06:36:29 +00:00
,children: [{
2023-07-12 07:31:42 +00:00
title: 'C-2-2-1'
2019-05-31 06:36:29 +00:00
,id: 13
},{
2023-07-12 07:31:42 +00:00
title: 'C-2-2-2'
2019-05-31 06:36:29 +00:00
,id: 14
,disabled: true
}]
}]
},{
2023-07-12 07:31:42 +00:00
title: 'A-3'
2019-05-31 06:36:29 +00:00
,id: 16
,children: [{
2023-07-12 07:31:42 +00:00
title: 'B-3-1'
2019-05-31 06:36:29 +00:00
,id: 17
,fixed: true
,children: [{
2023-07-12 07:31:42 +00:00
title: 'C-3-1-1'
2019-05-31 06:36:29 +00:00
,id: 18
},{
2023-07-12 07:31:42 +00:00
title: 'C-3-1-2'
2019-05-31 06:36:29 +00:00
,id: 19
}]
},{
2023-07-12 07:31:42 +00:00
title: 'B-3-2'
2019-05-31 06:36:29 +00:00
,id: 27
,children: [{
2023-07-12 07:31:42 +00:00
title: 'C-3-2-1'
2019-05-31 06:36:29 +00:00
,id: 28
},{
2023-07-12 07:31:42 +00:00
title: 'C-3-2-2'
2019-05-31 06:36:29 +00:00
,id: 29
}]
}]
}];
2019-09-10 00:37:47 +00:00
//数据源
var data2 = [{
title: '控制台'
,id: '1000'
,spread: true
,checked: true
,children: [{
title: '概览'
,id: '1001'
,spread: true
,checked: true
},{
title: '域名'
,id: '1002'
,spread: true
,checked: true
}]
}]
2017-08-21 00:51:13 +00:00
2019-06-06 04:19:43 +00:00
tree.render({
2019-05-31 06:36:29 +00:00
elem: '#test1'
,data: data1
2019-06-06 04:19:43 +00:00
,id: 'demoId1'
2019-05-31 06:36:29 +00:00
,click: function(obj){
2019-06-02 15:14:47 +00:00
layer.msg(JSON.stringify(obj.data));
2019-06-02 23:57:12 +00:00
console.log(obj);
2017-08-21 00:51:13 +00:00
}
2019-05-31 06:36:29 +00:00
,oncheck: function(obj){
2019-09-10 00:37:47 +00:00
//console.log(obj);
2019-05-31 06:36:29 +00:00
}
,operate: function(obj){
var type = obj.type;
if(type == 'add'){
//ajax操作返回key值
return index++;
2019-06-02 23:57:12 +00:00
}else if(type == 'update'){
2019-05-31 06:36:29 +00:00
console.log(obj.elem.find('.layui-tree-txt').html());
}else if(type == 'del'){
console.log(obj);
};
2017-08-21 00:51:13 +00:00
}
2019-05-31 06:36:29 +00:00
,showCheckbox: true //是否显示复选框
,accordion: 0 //是否开启手风琴模式
2019-06-02 23:57:12 +00:00
,onlyIconControl: true //是否仅允许节点左侧图标控制展开收缩
2019-05-31 06:36:29 +00:00
,isJump: 0 //点击文案跳转地址
2019-06-02 23:57:12 +00:00
,edit: true //操作节点图标
2017-08-21 00:51:13 +00:00
});
2019-06-06 04:19:43 +00:00
//按钮事件
util.event('lay-demo', {
getChecked: function(othis){
var checkedData = tree.getChecked('demoId1');
layer.alert(JSON.stringify(checkedData), {shade:0});
console.log(checkedData);
}
,setChecked: function(){
2023-07-12 07:31:42 +00:00
tree.setChecked('demoId1', [11,12]);
2019-06-06 04:19:43 +00:00
}
,reload: function(){
tree.reload('demoId1', {
});
}
});
2019-05-31 06:36:29 +00:00
tree.render({
elem: '#test2'
,data: data1
//,expandClick: false
,showLine: false //关闭连接线
,click: function(obj, state){
2019-06-02 23:57:12 +00:00
console.log(obj);
2019-05-31 06:36:29 +00:00
}
,oncheck: function(obj, checked, child){
if(checked){
console.log(obj[0]);
}
}
,onsearch: function(data, num){
console.log(num);
}
,dragstart: function(obj, parent){
console.log(obj, parent);
}
,dragend: function(state, obj, target){
console.log(state, obj, target);
}
});
2017-08-21 00:51:13 +00:00
});
</script>
</body>
2019-05-31 06:36:29 +00:00
</html>