帮助中心
获取牛币
联系我们
关于我们
版权声明
素材牛首页
微博登录
QQ登录
微信扫码登录
注册
登录
新浪微博
QQ登陆
微信登录
热门搜索:
小程序
支付
Java
后台模板
上传
商城模板
jQuery
手机
Thinkphp
微信
Vue.js
首 页
网页特效
整站源码
PHP实例
网站模版
工具箱
常用代码
论坛
游戏源码
资源分享
当前位置:
首页
»
精选常用代码
» jQuery实现的tree,支持懒加载,父子节点关联多选
jQuery实现的tree,支持懒加载,父子节点关联多选
浏览:335次
-
评论:0次
-
发布时间:2022-05-19
Html
Css
Js
获取选中ids
ul { list-style:none; padding-left:20px; } .fold-icon { font-size:12px; color:blue; cursor:pointer; } .fold-icon i { display:inline-block; width:0; height:0; border-left:5px solid #999999; border-right:5px solid transparent; border-top:5px solid transparent; border-bottom:5px solid transparent; } .fold-icon em { display:inline-block; width:0; height:0; border-left:5px solid transparent; border-right:5px solid transparent; border-top:5px solid #999999; border-bottom:5px solid transparent; position:relative; top:3px; } .fold-icon b { display:inline-block; width:10px; } ul li p { display:flex; align-items:center; color:#333333; font-size:14px; margin:0; line-height:30px; } ul li p:hover { background-color:#F6F6F6; }
$(function() { let KeyProps = {}; /** * 初始渲染函数 * @param treeData 源数据 * @param props 字段key 映射 */ function renderTree(treeData, props = {}) { KeyProps = props; function renderUl(data) { let str = ''; data.forEach(item => { if (!item.children) { str += `
${item[KeyProps['name']]}
`; } else { str += `
${item[KeyProps['name']]}
${renderUl(item.children)}
`; } }); return `
${str}
`; } $('.tree').html(renderUl(treeData)); addCheckboxEvent(); } function addCheckboxEvent() { $('input[name="treeCheckbox"]').each(function() { $(this).on('click', function() { if ($(this).is(':checked')) { ($(this).parent().parent().find('input[name="treeCheckbox"]')).each(function() { $(this).prop('checked', true); }); } else { ($(this).parent().parent().find('input[name="treeCheckbox"]')).each(function() { $(this).prop('checked', false); }); } }); }); } let treeData = [{ id: 1, name: '节点1', children: [{ id: '1-1', name: 'jj-2' }] }, { id: 2, name: '节点2' }, { id: 3, name: '节点3' }, { id: 4, name: '节点4' }, { id: 5, name: '节点5' }, ]; renderTree(treeData, { id: 'id', name: 'name' }); function fold(element) { if ($(element).html() === '
') { $(element).parent().parent().find('ul').css({ 'display': 'none' }); $(element).html('
'); } else { let fid = ($(element).next().attr('nodeId')); if ($(element).attr('catch') !== 'catch') { if (true) { $(element).attr('catch', 'catch'); console.log('模拟ajax'); let data = [{ id: fid + '-1', name: fid + '-子节点-1' }, { id: fid + '-2', name: fid + '-子节点-2' }, { id: fid + '-3', name: fid + '-子节点-3' }, ]; let str = ''; data.forEach(item => { str += `
${item[KeyProps['name']]}
`; }); $(element).parent().parent().find('ul').html(str); addCheckboxEvent(); $(element).parent().parent().find('ul').css({ 'display': 'block' }); $(element).html('
'); } else { //无子节点 $(element).html('
'); } } else { $(element).parent().parent().find('ul').css({ 'display': 'block' }); $(element).html('
'); } } } //获取选中的tree $('.save').on('click', function() { let ids = []; ($('input[name="treeCheckbox"]:checked')).each(function() { ids.push($(this).attr('nodeId')); }); console.log('选中的节点id', ids); }); })
↑上面代码改变,会自动显示代码结果
jQuery调用版本:
1.11.3
<!doctype html><html><head><meta charset="utf-8"><title>jQuery实现的tree,支持懒加载,父子节点关联多选-www.sucainiu.com</title><script src="https://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script><style>
</style></head><body>
<script>
</script>
</body></html>
立即下载
收藏
积分说明:注册即送10牛币,每日签到可获得5牛币,成为VIP会员可永久免牛币下载!
充值积分
充值会员
更多说明»
代码描述:原生jquery实现tree,支持懒加载,父子节点关联多选
炫酷的鼠标点击绽放烟花特效
简易版JS二级联动效果代码
讨论这个常用代码(0)
回答他人问题或分享心得会奖励牛币
〒_〒 居然一个评论都没有……
文明上网,理性发言! 😉 阿里云幸运券,
戳我领取
发表评论
提交回复
我的积分余额:
0
已下载次数:
335
所需牛币:
5
开始下载
牛币获取:
签到、评论、充值
» 在线充值
(10牛币=1元)
成为VIP可永久免积分下载全部常用代码
» 查看详情
VIP
客服
签到
充值