当前位置:   article > 正文

zTree 插件与bootstrap-table 实现树与列表联动

ztree bootstrap 多表联查

实现效果:

 界面代码:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width" />
  5. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  6. <title>Index</title>
  7. <link href="~/Content/bootstrapStyle/bootstrapStyle.css" rel="stylesheet" />
  8. <script src="~/Content/js/jquery-2.1.4.js"></script>
  9. <script src="~/Content/js/jquery.ztree.core.js"></script>
  10. <script src="~/Content/js/jquery.ztree.excheck.js"></script>
  11. <script src="~/Content/js/jquery.ztree.exedit.js"></script>
  12. <link href="~/Content/bootstrap.css" rel="stylesheet" />
  13. <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
  14. <script src="~/Scripts/bootstrap.min.js"></script>
  15. <!-- 引入bootstrap-table -->
  16. <link href="~/Content/bootstrap-table.min.css" rel="stylesheet" />
  17. <script src="~/Content/bootstrap-table.min.js"></script>
  18. <script src="~/Content/bootstrap-table-zh-CN.min.js"></script>
  19. <style>
  20. .showbox {
  21. position: absolute;
  22. border: 1px solid #d2d2d2;
  23. background-color: #fff;
  24. box-shadow: 0 2px 4px rgba(0,0,0,.12);
  25. padding: 10px 10px 10px 10px;
  26. position: absolute;
  27. z-index: 66666666;
  28. margin: 5px 0;
  29. border-radius: 2px;
  30. min-width: 530px;
  31. height: 400px;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <input class="" id="showid">
  37. <div class="showbox">
  38. <div style="width: 100%; height: 32px;text-align:right;">
  39. <button id="close">关闭</button>
  40. </div>
  41. <div style="width: 30%; float: left; border: 1px solid #d2d2d2; height: 340px; overflow: auto;">
  42. <ul id="treeDemo" class="ztree"></ul>
  43. </div>
  44. <div style="width: 68%; border: 1px solid #d2d2d2; height: 340px; float: right; overflow: auto;">
  45. <table id="table" style="height: 100%;"></table>
  46. </div>
  47. </div>
  48. <script type="text/javascript">
  49. $(function () {
  50. $(".showbox").hide();
  51. })
  52. $("#showid").click(function () {
  53. var t = $("#showid").offset().top + $("#showid").outerHeight() + "px";
  54. var l = $("#showid").offset().left + "px";
  55. $(".showbox").css({ top:t, left: l});
  56. $(".showbox").show();
  57. })
  58. //点击其他区域关闭
  59. $(document).mouseup(function (e) {
  60. var userSet_con = $(".showbox");
  61. if (!userSet_con.is(e.target) && userSet_con.has(e.target).length === 0) {
  62. $(".showbox").hide();
  63. }
  64. });
  65. $(function () {
  66. $.post("/home/index", function (data) {
  67. var setting = {
  68. check: {
  69. enable: false,
  70. },
  71. data: {
  72. simpleData: {
  73. enable: true,
  74. idKey: 'id'
  75. }
  76. },
  77. callback: {
  78. onClick: function (event, treeId, treeNode) {
  79. parent_id = treeNode.id;
  80. //选择部门更换数据
  81. $("#table").bootstrapTable('refresh', { query: { id: parent_id } });
  82. }
  83. },
  84. };
  85. var zNodes = [
  86. { id: 1, pId: 0, name: "xx部门", open: true },
  87. { id: 101, pId: 1, name: "部门1" },
  88. { id: 102, pId: 1, name: "部门2" },
  89. { id: 103, pId: 1, name: "部门3" },
  90. { id: 104, pId: 1, name: "部门4" },
  91. { id: 2, pId: 0, name: "xxxx部门", open: false },
  92. { id: 201, pId: 2, name: "部门1" },
  93. { id: 206, pId: 2, name: "部门2" },
  94. { id: 207, pId: 2, name: "部门3" },
  95. ];
  96. var nodetree = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  97. nodetree.expendAll(true);//全部展开
  98. })
  99. });
  100. $("#table").bootstrapTable({ // 对应table标签的id
  101. url: "../Content/usersList.json", // 获取表格数据的url
  102. method: 'get',
  103. striped: true, //表格显示条纹,默认为false
  104. pagination: false, // 在表格底部显示分页组件,默认false
  105. dataType: "json",
  106. sortOrder: 'desc',
  107. columns: [
  108. {
  109. checkbox: true, // 显示一个勾选框
  110. align: 'center' // 居中显示
  111. }, {
  112. field: 'userName', // 返回json数据中的name
  113. title: '用户名', // 表格表头显示文字
  114. align: 'center', // 左右居中
  115. valign: 'middle' // 上下居中
  116. }, {
  117. field: 'userSex',
  118. title: '性别',
  119. align: 'center',
  120. valign: 'middle'
  121. }
  122. ],
  123. onCheck: function (rows) {
  124. getselect();
  125. },
  126. onUncheck: function (rows) {
  127. getselect();
  128. },
  129. onCheckAll: function (rows) {
  130. getselect();
  131. },
  132. onUncheckAll: function (rows) {
  133. getselect();
  134. },
  135. })
  136. //关闭
  137. $("#close").click(function () {
  138. $(".showbox").hide();
  139. })
  140. //获取数据赋值文本框
  141. function getselect()
  142. {
  143. var rows = $("#table").bootstrapTable('getSelections');
  144. var arrays = new Array();// 声明一个数组
  145. $(rows).each(function () {
  146. arrays.push(this.userName);
  147. });
  148. var idcard = arrays.join(','); // 获得要删除的id
  149. $("#showid").val(idcard);
  150. }
  151. </script>
  152. </body>
  153. </html>

  

 

转载于:https://www.cnblogs.com/lemonmoney/p/10557375.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/322321?site
推荐阅读
相关标签
  

闽ICP备14008679号