怕忘了,做个记录
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE> </TITLE>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../../css/demo.css" type="text/css">
<link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="../../../js/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="../../../js/jquery.ztree.excheck-3.5.js"></script>
<SCRIPT type="text/javascript">
var IDMark_A = "_a";
var setting = {
view: {
addDiyDom: addDiyDom
},
data: {
simpleData: {
enable: true
}
}
};
var zNodes =[
{ id:1, pId:0, name:"父节点 1",page:0, open:true,rad:0},
{ id:11, pId:1, name:"叶子节点 1-1",page:1,rad:0},
{ id:112, pId:11, name:"叶子节点 1-1-1",page:0,rad:0},
{ id:12, pId:1, name:"叶子节点 1-2",page:1,rad:0},
{ id:13, pId:1, name:"叶子节点 1-3",page:1,rad:0},
{ id:2, pId:0, name:"父节点 2",page:0, open:true,rad:0},
{ id:21, pId:2, name:"叶子节点 2-1",page:1,rad:0},
{ id:22, pId:2, name:"叶子节点 2-2",page:1,rad:0},
{ id:23, pId:2, name:"叶子节点 2-3",page:1,rad:0},
{ id:3, pId:11, name:"父节点 3",page:0, open:true,rad:0},
{ id:31, pId:3, name:"叶子节点 3-1",page:0,rad:1},
{ id:32, pId:3, name:"叶子节点 3-2",page:0,rad:1},
{ id:33, pId:3, name:"叶子节点 3-3",page:0,rad:1}
];
function addDiyDom(treeId, treeNode) {
var aObj = $("#" + treeNode.tId + IDMark_A);
//alert(JSON.stringify(treeNode) )
if (treeNode.rad == 0) {
//alert(treeNode.pId)
var editStr = "<input type=‘checkbox‘ class=‘checkboxBtn‘ page=‘"+treeNode.page+"‘ name=‘checkbox_"+treeNode.pId+"‘ id=‘checkbox_" +treeNode.id+ "‘ onfocus=‘this.blur();‘></input>";
aObj.before(editStr);
var btn = $("#checkbox_"+treeNode.id);
if (btn) btn.bind("change", function() {checkAccessories(treeNode, btn);});
} else if (treeNode.rad == 1) {
var editStr = "<input type=‘radio‘ class=‘radioBtn‘ id=‘radio_" +treeNode.id+ "‘ name=‘radio_"+treeNode.getParentNode().id+"‘ onfocus=‘this.blur();‘></input>";
aObj.before(editStr);
var btn = $("#radio_"+treeNode.id);
if (btn) btn.bind("click", function() {checkBrand(treeNode, btn);});
}
}
//点击多选
function checkAccessories(treeNode, btn) {
var checkedRadio = getCheckedRadio("radio_"+treeNode.id);
//下一级单选
var r= document.getElementsByName("radio_"+treeNode.id) ;
//下一级多选
var pObj =document.getElementsByName("checkbox_"+treeNode.id);
if (btn.attr("checked")) {
//选中追溯父代
faBind("checkbox_"+treeNode.pId,true)
sonBind(treeNode.id,true)
} else {
//选中追溯子代
sonBind(treeNode.id,false)
}
}
//点击单选
function checkBrand(treeNode, btn) {
if (btn.attr("checked")) {
faBind("checkbox_"+treeNode.pId,true)
var pObj = $("#checkbox_" + treeNode.getParentNode().id);
if (!pObj.attr("checked")) {
pObj.attr("checked", true);
}
}
}
//追溯父代(父代一定为多选)
function faBind(pId,check)
{
if(check)
{
var parObj = $("#" +pId);
if(parObj.length>0)
{
$(parObj[0]).attr("checked",true)
faBind($(parObj[0]).attr("name"),check)
}
}
/*else
{
//当前取消的如果是个页面,那么判断是否还有其他页面存在,如果没有,那么就除去相应的上级(未完成)
var clr=2;
var checkObj =$("[name=‘checkbox_"+pId+"‘]");
checkObj.each(function(){
if($(this).attr("page")==1)
{
clr=1
if($(this).attr("checked"))
{
clr=2;
break;
}
}
})
}*/
}
//追溯子代选中(有可能为单选,也有可能为多选)
function sonBind(pId,check)
{
//多选框
var checkObj =$("[name=‘checkbox_"+pId+"‘]");
//单选框
var radioObj =$("[name=‘radio_"+pId+"‘]");
if(check)
{
checkObj.each(function(){
$(this).attr("checked",true);
sonBind($(this).attr("id").replace("checkbox_",""),check)
})
}
else
{
checkObj.each(function(){
$(this).attr("checked",false);
sonBind($(this).attr("id").replace("checkbox_",""),check)
})
radioObj.each(function(){
$(this).attr("checked",false);
sonBind($(this).attr("id").replace("radio_",""),check)
})
}
}
//追溯子代选中(父代一定为多选)
function faBind(pId,check)
{
if(check)
{
var parObj = $("#" +pId);
if(parObj.length>0)
{
$(parObj[0]).attr("checked",true)
faBind($(parObj[0]).attr("name"),check)
}
}
}
function getCheckedRadio(radioName) {
var r = document.getElementsByName(radioName);
for(var i=0; i<r.length; i++) {
if(r[i].checked) {
return $(r[i]);
}
}
return null;
}
$(document).ready(function(){
$.fn.zTree.init($("#treeDemo"), setting, zNodes);
});
//-->
</SCRIPT>
<style type="text/css">
.radioBtn {height: 16px;vertical-align: middle;}
.checkboxBtn {vertical-align: middle;margin-right: 2px;}
</style>
</HEAD>
<BODY>
<h1>checkbox / radio 共存</h1>
<h6>[ 文件路径: super/checkbox_radio.html ]</h6>
<div class="content_wrap">
<div class="zTreeDemoBackground left">
<ul id="treeDemo" class="ztree"></ul>
</div>
<div class="right">
<ul class="info">
<li class="title"><h2>实现方法说明</h2>
<ul class="list">
<li>zTree 默认的 checkbox 和 radio 是无法共存的,但可以利用 自定义控件 的功能实现此需求,具体需求可以参考 "添加 自定义控件" 的详细说明</li>
<li class="highlight_red">对于 checkbox / radio 的联动规则,请根据需求制定自己的规则</li>
</ul>
</li>
</ul>
</div>
</div>
</BODY>
</HTML>
原文地址:https://www.cnblogs.com/wuyus/p/12101335.html