checkbox_radio.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <!DOCTYPE html>
  2. <HTML>
  3. <HEAD>
  4. <TITLE> ZTREE DEMO - checkbox & radio</TITLE>
  5. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  6. <link rel="stylesheet" href="../../../css/demo.css" type="text/css">
  7. <link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css">
  8. <script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
  9. <script type="text/javascript" src="../../../js/jquery.ztree.core-3.5.js"></script>
  10. <!-- <script type="text/javascript" src="../../../js/jquery.ztree.excheck-3.5.js"></script>
  11. <script type="text/javascript" src="../../../js/jquery.ztree.exedit-3.5.js"></script>-->
  12. <SCRIPT type="text/javascript">
  13. <!--
  14. var IDMark_A = "_a";
  15. var setting = {
  16. view: {
  17. addDiyDom: addDiyDom
  18. },
  19. data: {
  20. simpleData: {
  21. enable: true
  22. }
  23. }
  24. };
  25. var zNodes =[
  26. { id:1, pId:0, name:"父节点 1", open:true},
  27. { id:11, pId:1, name:"叶子节点 1-1"},
  28. { id:12, pId:1, name:"叶子节点 1-2"},
  29. { id:13, pId:1, name:"叶子节点 1-3"},
  30. { id:2, pId:0, name:"父节点 2", open:true},
  31. { id:21, pId:2, name:"叶子节点 2-1"},
  32. { id:22, pId:2, name:"叶子节点 2-2"},
  33. { id:23, pId:2, name:"叶子节点 2-3"},
  34. { id:3, pId:0, name:"父节点 3", open:true},
  35. { id:31, pId:3, name:"叶子节点 3-1"},
  36. { id:32, pId:3, name:"叶子节点 3-2"},
  37. { id:33, pId:3, name:"叶子节点 3-3"}
  38. ];
  39. function addDiyDom(treeId, treeNode) {
  40. var aObj = $("#" + treeNode.tId + IDMark_A);
  41. if (treeNode.level == 0) {
  42. var editStr = "<input type='checkbox' class='checkboxBtn' id='checkbox_" +treeNode.id+ "' onfocus='this.blur();'></input>";
  43. aObj.before(editStr);
  44. var btn = $("#checkbox_"+treeNode.id);
  45. if (btn) btn.bind("change", function() {checkAccessories(treeNode, btn);});
  46. } else if (treeNode.level == 1) {
  47. var editStr = "<input type='radio' class='radioBtn' id='radio_" +treeNode.id+ "' name='radio_"+treeNode.getParentNode().id+"' onfocus='this.blur();'></input>";
  48. aObj.before(editStr);
  49. var btn = $("#radio_"+treeNode.id);
  50. if (btn) btn.bind("click", function() {checkBrand(treeNode, btn);});
  51. }
  52. }
  53. function checkAccessories(treeNode, btn) {
  54. var checkedRadio = getCheckedRadio("radio_"+treeNode.id);
  55. if (btn.attr("checked")) {
  56. if (!checkedRadio) {
  57. $("#radio_" + treeNode.children[0].id).attr("checked", true);
  58. }
  59. } else {
  60. checkedRadio.attr("checked", false);
  61. }
  62. }
  63. function checkBrand(treeNode, btn) {
  64. if (btn.attr("checked")) {
  65. var pObj = $("#checkbox_" + treeNode.getParentNode().id);
  66. if (!pObj.attr("checked")) {
  67. pObj.attr("checked", true);
  68. }
  69. }
  70. }
  71. function getCheckedRadio(radioName) {
  72. var r = document.getElementsByName(radioName);
  73. for(var i=0; i<r.length; i++) {
  74. if(r[i].checked) {
  75. return $(r[i]);
  76. }
  77. }
  78. return null;
  79. }
  80. $(document).ready(function(){
  81. $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  82. });
  83. //-->
  84. </SCRIPT>
  85. <style type="text/css">
  86. .radioBtn {height: 16px;vertical-align: middle;}
  87. .checkboxBtn {vertical-align: middle;margin-right: 2px;}
  88. </style>
  89. </HEAD>
  90. <BODY>
  91. <h1>checkbox / radio 共存</h1>
  92. <h6>[ 文件路径: super/checkbox_radio.html ]</h6>
  93. <div class="content_wrap">
  94. <div class="zTreeDemoBackground left">
  95. <ul id="treeDemo" class="ztree"></ul>
  96. </div>
  97. <div class="right">
  98. <ul class="info">
  99. <li class="title"><h2>实现方法说明</h2>
  100. <ul class="list">
  101. <li>zTree 默认的 checkbox 和 radio 是无法共存的,但可以利用 自定义控件 的功能实现此需求,具体需求可以参考 "添加 自定义控件" 的详细说明</li>
  102. <li class="highlight_red">对于 checkbox / radio 的联动规则,请根据需求制定自己的规则</li>
  103. </ul>
  104. </li>
  105. </ul>
  106. </div>
  107. </div>
  108. </BODY>
  109. </HTML>