You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
layui/examples/tree.html

262 lines
6.0 KiB

6 years ago
7 years ago
<!DOCTYPE html>
<html>
<head>
6 years ago
<meta charset="utf-8">
<title>树组件 - layui</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
7 years ago
6 years ago
<link rel="stylesheet" href="../src/css/layui.css" media="all">
<style>
6 years ago
body{padding: 100px;}
6 years ago
#test1,#test2{margin-bottom: 100px; width: 400px;}
</style>
7 years ago
</head>
<body>
6 years ago
<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>
6 years ago
<div id="test1"></div>
<div id="test2"></div>
7 years ago
<script src="../src/layui.js"></script>
7 years ago
<script>
6 years ago
layui.use(['tree', 'layer', 'util'], function(){
var $ = layui.$
,tree = layui.tree
6 years ago
,layer = layui.layer
6 years ago
,util = layui.util
6 years ago
,index = 100;
//数据源
var data1 = [{
6 years ago
title: '一级1'
6 years ago
,id: 1
,children: [{
6 years ago
title: '二级1-1'
6 years ago
,id: 3
,href: 'https://www.layui.com/doc/'
,children: [{
6 years ago
title: '三级1-1-3'
6 years ago
,id: 23
,children: [{
6 years ago
title: '四级1-1-3-1'
6 years ago
,id: 24
,children: [{
6 years ago
title: '五级1-1-3-1-1'
6 years ago
,id: 30
},{
6 years ago
title: '五级1-1-3-1-2'
6 years ago
,id: 31
}]
}]
},{
6 years ago
title: '三级1-1-1'
6 years ago
,id: 7
6 years ago
,checked: true
6 years ago
,children: [{
6 years ago
title: '四级1-1-1-1'
6 years ago
,id: 15
5 years ago
//,checked: true
6 years ago
,href: 'https://www.layui.com/doc/base/infrastructure.html'
}]
},{
6 years ago
title: '三级1-1-2'
6 years ago
,id: 8
,children: [{
6 years ago
title: '四级1-1-2-1'
6 years ago
,id: 32
}]
}]
},{
6 years ago
title: '二级1-2'
6 years ago
,id: 4
6 years ago
,spread: true
6 years ago
,children: [{
6 years ago
title: '三级1-2-1'
6 years ago
,id: 9
6 years ago
,checked: true
6 years ago
,disabled: true
},{
6 years ago
title: '三级1-2-2'
6 years ago
,id: 10
}]
},{
6 years ago
title: '二级1-3'
6 years ago
,id: 20
,children: [{
6 years ago
title: '三级1-3-1'
6 years ago
,id: 21
},{
6 years ago
title: '三级1-3-2'
6 years ago
,id: 22
}]
}]
},{
6 years ago
title: '一级2'
6 years ago
,id: 2
6 years ago
,spread: true
6 years ago
,children: [{
6 years ago
title: '二级2-1'
6 years ago
,id: 5
6 years ago
,spread: true
6 years ago
,children: [{
6 years ago
title: '三级2-1-1'
6 years ago
,id: 11
},{
6 years ago
title: '三级2-1-2'
6 years ago
,id: 12
}]
},{
6 years ago
title: '二级2-2'
6 years ago
,id: 6
6 years ago
,checked: true
6 years ago
,children: [{
6 years ago
title: '三级2-2-1'
6 years ago
,id: 13
},{
6 years ago
title: '三级2-2-2'
6 years ago
,id: 14
,disabled: true
}]
}]
},{
6 years ago
title: '一级3'
6 years ago
,id: 16
,children: [{
6 years ago
title: '二级3-1'
6 years ago
,id: 17
,fixed: true
,children: [{
6 years ago
title: '三级3-1-1'
6 years ago
,id: 18
},{
6 years ago
title: '三级3-1-2'
6 years ago
,id: 19
}]
},{
6 years ago
title: '二级3-2'
6 years ago
,id: 27
,children: [{
6 years ago
title: '三级3-2-1'
6 years ago
,id: 28
},{
6 years ago
title: '三级3-2-2'
6 years ago
,id: 29
}]
}]
}];
5 years ago
//数据源
var data2 = [{
title: '控制台'
,id: '1000'
,spread: true
,checked: true
,children: [{
title: '概览'
,id: '1001'
,spread: true
,checked: true
},{
title: '域名'
,id: '1002'
,spread: true
,checked: true
}]
}]
7 years ago
6 years ago
tree.render({
6 years ago
elem: '#test1'
,data: data1
6 years ago
,id: 'demoId1'
6 years ago
,click: function(obj){
6 years ago
layer.msg(JSON.stringify(obj.data));
6 years ago
console.log(obj);
7 years ago
}
6 years ago
,oncheck: function(obj){
5 years ago
//console.log(obj);
6 years ago
}
,operate: function(obj){
var type = obj.type;
if(type == 'add'){
//ajax操作返回key值
return index++;
6 years ago
}else if(type == 'update'){
6 years ago
console.log(obj.elem.find('.layui-tree-txt').html());
}else if(type == 'del'){
console.log(obj);
};
7 years ago
}
6 years ago
,showCheckbox: true //是否显示复选框
,accordion: 0 //是否开启手风琴模式
6 years ago
,onlyIconControl: true //是否仅允许节点左侧图标控制展开收缩
6 years ago
,isJump: 0 //点击文案跳转地址
6 years ago
,edit: true //操作节点图标
7 years ago
});
6 years ago
//按钮事件
util.event('lay-demo', {
getChecked: function(othis){
var checkedData = tree.getChecked('demoId1');
layer.alert(JSON.stringify(checkedData), {shade:0});
console.log(checkedData);
}
,setChecked: function(){
5 years ago
tree.setChecked('demoId1', [1000, 1001, 1002]);
6 years ago
}
,reload: function(){
tree.reload('demoId1', {
});
}
});
6 years ago
tree.render({
elem: '#test2'
,data: data1
//,expandClick: false
,showLine: false //关闭连接线
,click: function(obj, state){
6 years ago
console.log(obj);
6 years ago
}
,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);
}
});
7 years ago
});
</script>
</body>
6 years ago
</html>