当前位置:   article > 正文

ztree模糊搜索及ztree与bootstrapTable表格数据左右增加和删除_ztree bootstrap

ztree bootstrap
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta http-equiv="X-UA-Compatible" content="IE=EDGE">
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>用户管理</title>
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <link rel="stylesheet" href="./static/libs/font-awesome/css/font-awesome.css">
  9. <link rel="stylesheet" href="./static/libs/web-icons/web-icons.css">
  10. <link rel="stylesheet" href="./static/libs/bootstrap/css/bootstrap.css">
  11. <link rel="stylesheet" href="./static/libs/bootstrap-table-master/bootstrap-table.css">
  12. <link rel="stylesheet" href="./static/libs/toastr/css/toastr.css">
  13. <link rel="stylesheet" href="./static/css/common/base.css">
  14. <link rel="stylesheet" href="./static/libs/bootstrap-validator/css/bootstrapValidator.css">
  15. <link rel="stylesheet" href="./static/libs/ztree/css/metroStyle/metroStyle_adm.css" type="text/css">
  16. <link rel="stylesheet" href="./static/libs/alertify.js-0.3.11/themes/alertify.core.css" type="text/css">
  17. <link rel="stylesheet" href="./static/libs/alertify.js-0.3.11/themes/alertify.bootstrap.css" type="text/css">
  18. <style>
  19. .pull-left {
  20. padding-left: 10px;
  21. }
  22. .pull-right {
  23. padding-right: 10px;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <a id="add-virtual-group-button" href="#" title="" onclick="return false;">添加</a>
  29. <!-- // 创建虚拟组 begin -->
  30. <div class="modal fade" id="add-virtual-group-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  31. <div class="modal-dialog modal-create" style="width: 700px;">
  32. <div class="modal-content">
  33. <div class="modal-header">
  34. <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
  35. ×
  36. </button>
  37. <h4 class="modal-title"> 创建虚拟组 </h4>
  38. </div>
  39. <form id="add-virtual-group-modal-form" method="post" class="form-horizontal" action="">
  40. <div class="modal-body">
  41. <!-- // modal-body begin -->
  42. <!-- Nav tabs -->
  43. <ul class="nav nav-tabs" role="tablist" id="add-virtual-group-modal-tab">
  44. <li role="presentation" class="active"><a href="#add-virtual-group-modal-base-info" aria-controls="add-virtual-group-modal-base-info" role="tab" data-toggle="tab">基本信息</a></li>
  45. <li role="presentation"><a href="#add-virtual-group-modal-users" aria-controls="add-virtual-group-modal-users" role="tab" data-toggle="tab">用户</a></li>
  46. </ul>
  47. <!-- Tab panes -->
  48. <div class="tab-content">
  49. <div role="tabpanel" class="tab-pane active" id="add-virtual-group-modal-base-info">
  50. <div class="clear"></div>
  51. <!-- 基本信息 bedin -->
  52. <div style="height: 30px; clear: both;"></div>
  53. <div class="form-group">
  54. <label class="col-lg-3 col-md-3 col-sm-3 control-label font-normal align-right">名 称:</label>
  55. <div class="col-lg-6 col-md-6 col-sm-6">
  56. <input type="text" class="form-control" id="add-virtual-group-modal-name" name="" placeholder="" onkeyup="checkAddVirtualGroupModalName(this);"/>
  57. </div>
  58. </div>
  59. <div class="form-group" id="add-virtual-group-modal-name-tips-content" style="display: none;">
  60. <label class="col-lg-3 col-md-3 col-sm-3 control-label font-normal align-right"></label>
  61. <div class="col-lg-9 col-md-9 col-sm-9" id="add-virtual-group-modal-name-tips" style="font-size: 12px; color: #ff0000">
  62. </div>
  63. </div>
  64. <!-- 基本信息 end -->
  65. </div>
  66. <div role="tabpanel" class="tab-pane" id="add-virtual-group-modal-users">
  67. <!-- // 主机 begin -->
  68. <table width="100%" border="0">
  69. <tr>
  70. <td width="45%" align="left" valign="top">
  71. <div style="height: 20px; clear: both;"></div>
  72. <div class="form-group" style="margin-left: 0; margin-right: 0;">
  73. <label class="sr-only" for="exampleInputAmount"></label>
  74. <div class="input-group">
  75. <input type="text" id="add-virtual-group-modal-users-input" onkeyup="autoMatchForAddVirtualGroupUsers();" class="form-control" placeholder="模糊匹配" style="border: 1px solid #cccccc; border-right: 0;"/>
  76. <div class="input-group-addon"><i class="glyphicon glyphicon-search"></i></div>
  77. </div>
  78. </div>
  79. <ul class="ztree strategy-tab-ul" id="add-virtual-group-modal-users-tree" style="height: 200px; overflow: auto; margin-top: 0; border: 1px solid #e4eaec;">
  80. </ul>
  81. </td>
  82. <td align="left" valign="middle">
  83. <a href="javascript:void(0)" class="btn btn-primary btn-xs" id="add-virtual-group-modal-users-add-button" style="margin: 20px;">添加</a>
  84. <br>
  85. <a href="javascript:void(0)" class="btn btn-primary btn-xs" id="add-virtual-group-modal-users-delete-button" style="margin: 20px;">删除</a>
  86. </td>
  87. <td width="45%" align="left" valign="top">
  88. <table id="add-virtual-group-modal-users-table" data-toggle="table" class="table table-bordered table-striped" style="margin-top: 0;"></table>
  89. </td>
  90. </tr>
  91. </table>
  92. <!-- 主机 end // -->
  93. </div>
  94. </div>
  95. <!-- modal-body end // -->
  96. </div>
  97. <div class="modal-footer">
  98. <button class="btn btn-primary" id="add-virtual-group-modal-submit" type="submit">
  99. 确 定
  100. </button>
  101. <button class="btn btn-default" type="button" data-dismiss="modal">
  102. 取 消
  103. </button>
  104. </div>
  105. </form>
  106. </div><!-- /Modal -->
  107. </div>
  108. </div><!-- 创建虚拟组 end // -->
  109. <script src="./static/libs/jquery/jquery-1.11.2.min.js"></script>
  110. <script src="./static/libs/bootstrap/js/bootstrap.js"></script>
  111. <script src="./static/libs/bootstrap-table-master/bootstrap-table.js"></script>
  112. <script src="./static/libs/bootstrap-table-master/locale/bootstrap-table-zh-CN.js"></script>
  113. <script src="./static/libs/toastr/js/toastr.min.js"></script>
  114. <script src="./static/libs/bootstrap-validator/js/bootstrapValidator.js"></script>
  115. <script src="./static/libs/ztree/js/jquery.ztree.core-3.5.js"></script>
  116. <script src="./static/libs/ztree/js/jquery.ztree.exedit.js"></script>
  117. <script src="./static/libs/ztree/js/jquery.ztree.excheck-3.5.js"></script>
  118. <script src="./static/libs/ztree/js/jquery.ztree.exhide-3.5.js"></script>
  119. <script src="./static/libs/alertify.js-0.3.11/lib/alertify.min.js"></script>
  120. <script src="./static/libs/My97DatePicker/WdatePicker.js"></script>
  121. <script>
  122. // 所有节点
  123. var allNodes = [];
  124. $("#add-virtual-group-button").click(function(){
  125. $("#add-virtual-group-modal").modal("show");
  126. initTreeForAddVirtualGroupUsers();
  127. var ztreeObj = $.fn.zTree.getZTreeObj("add-virtual-group-modal-users-tree");
  128. allNodes = ztreeObj.transformToArray(ztreeObj.getNodes());
  129. console.log(allNodes);
  130. });
  131. //添加用户表格
  132. var $addVirtualGroupModalUsersTable = $('#add-virtual-group-modal-users-table').bootstrapTable('destroy').bootstrapTable({
  133. url: '',
  134. method: 'POST',
  135. uniqueId: 'id',
  136. striped: false,
  137. cache: false,
  138. sortable: true,
  139. sortOrder: "asc",
  140. sidePagination: "client",
  141. undefinedText: '--',
  142. //singleSelect: true,
  143. //showRefresh : true,
  144. //showColumns : true,
  145. toolbar: '#user-table-toolbar',
  146. search: false,
  147. strictSearch: true,
  148. clickToSelect: true,
  149. pagination: true,
  150. pageNumber:1,
  151. pageSize:5,
  152. pageList: [5, 10, 20, 50, 100],
  153. paginationPreText:"上一页",
  154. paginationNextText:"下一页",
  155. paginationLoop:false,
  156. //showToggle: true,
  157. //cardView: false,
  158. //detailView: false,
  159. //showPaginationSwitch: true,
  160. //得到查询的参数
  161. queryParams : function (params) {
  162. //这里的键的名字和控制器的变量名必须一直,这边改动,控制器也需要改成一样的
  163. var temp = {
  164. rows: params.limit, //页面大小
  165. page: (params.offset / params.limit) + 1, //页码
  166. sort: params.sort, //排序列名
  167. sortOrder: params.order, //排位命令(desc,asc)
  168. };
  169. // console.log(temp);
  170. return temp;
  171. },
  172. columns: [
  173. {
  174. checkbox: true
  175. },{
  176. field: 'name',
  177. title: '用户'
  178. }
  179. ],
  180. onLoadSuccess: function () {
  181. //alert('表格加载成功!');
  182. },
  183. onLoadError: function () {
  184. showTips("数据加载失败!");
  185. },
  186. onDblClickRow: function (row, $element) {
  187. var id = row.ID;
  188. //$("#user-details-modal").modal("show");
  189. //EditViewById(id, 'view');
  190. },
  191. rowStyle: function (row, index) { //设置行的特殊样式
  192. //这里有5个取值颜色['active', 'success', 'info', 'warning', 'danger'];
  193. var strclass = "";
  194. if (index == 1) {
  195. //strclass = "warning";
  196. //console.log(row);
  197. }
  198. return { classes: strclass }
  199. }
  200. });
  201. /**
  202. * 功能:创建虚拟组(dsh)
  203. */
  204. function searchChildren(keyword, children){
  205. if(children == null || children.length == 0){
  206. return false;
  207. }
  208. for(var i = 0; i < children.length; i++){
  209. var node = children[i];
  210. if(node.name.indexOf(keyword) != -1 && node.isHidden === false){
  211. return true;
  212. }
  213. var result = searchChildren(keyword, node.children);
  214. if(result){
  215. return true;
  216. }
  217. }
  218. return false;
  219. }
  220. function searchParent(keyword, node){
  221. if(node == null){
  222. return false;
  223. }
  224. if(node.name.indexOf(keyword) != -1 && node.isHidden === false){
  225. return true;
  226. }
  227. return searchParent(keyword, node.getParentNode());
  228. }
  229. var hiddenNodesForAddVirtualGroupUsers = [];
  230. function autoMatchForAddVirtualGroupUsers(){
  231. setTimeout(function(){
  232. var ztreeObj = $.fn.zTree.getZTreeObj("add-virtual-group-modal-users-tree");
  233. ztreeObj.showNodes(hiddenNodesForAddVirtualGroupUsers);
  234. function filterFunc(node){
  235. var keyword = $("#add-virtual-group-modal-users-input").val();
  236. if(searchParent(keyword, node) || searchChildren(keyword, node.children)){
  237. return false;
  238. }
  239. return true;
  240. };
  241. hiddenNodesForAddVirtualGroupUsers = ztreeObj.getNodesByFilter(filterFunc);
  242. ztreeObj.hideNodes(hiddenNodesForAddVirtualGroupUsers);
  243. }, 300);
  244. }
  245. function initTreeForAddVirtualGroupUsers(){
  246. $.fn.zTree.init($("#add-virtual-group-modal-users-tree"), settingForAddVirtualGroupUsers, zNodes);
  247. }
  248. var zNodes = [
  249. { "id":0, "pId":-1, "name":"根主机组", "isParenta": true, "open": true, "icon":"/secret-hollow/static/img/computers.png"},
  250. { "id":1, "pId":0, "name":"主机组1", "isParenta": true, "open": true, "icon":"/secret-hollow/static/img/computers.png"},
  251. { "id":2, "pId":0, "name":"主机组2", "isParenta": true, "open": true, "icon":"/secret-hollow/static/img/computers.png"},
  252. { "id":6, "pId":1, "name":"主机1", "open": true, "icon":"/secret-hollow/static/img/computer.png"},
  253. { "id":7, "pId":1, "name":"主机2", "open": true, "icon":"/secret-hollow/static/img/computer.png"},
  254. { "id":3, "pId":0, "name":"主机组3", "isParenta": true, "open": true, "icon":"/secret-hollow/static/img/computers.png"},
  255. { "id":4, "pId":0, "name":"主机组4", "isParenta": true, "open": true, "icon":"/secret-hollow/static/img/computers.png"},
  256. { "id":5, "pId":0, "name":"主机组5", "isParenta": true, "open": true, "icon":"/secret-hollow/static/img/computers.png"}
  257. ]
  258. var settingForAddVirtualGroupUsers = {
  259. check : {
  260. enable : true,
  261. chkboxType : {
  262. "Y" : "ps",
  263. "N" : "ps"
  264. }
  265. },
  266. view : {
  267. dblClickExpand : false
  268. },
  269. data: {
  270. simpleData : {
  271. enable : true,
  272. idKey : "id",
  273. pIdKey : "pId",
  274. rootPId : null
  275. }
  276. },
  277. callback: {
  278. beforeClick : function(treeId, treeNode) {
  279. var zTree = $.fn.zTree.getZTreeObj("add-virtual-group-modal-users-tree");
  280. zTree.checkNode(treeNode, !treeNode.checked, null, true);
  281. return true;
  282. },
  283. onCheck : function(e, treeId, treeNode) {
  284. }
  285. }
  286. };
  287. // 点击添加按钮
  288. $("#add-virtual-group-modal-users-add-button").click(function(){
  289. // 获取树对象
  290. var ztreeObj = $.fn.zTree.getZTreeObj("add-virtual-group-modal-users-tree");
  291. // 选择的节点
  292. var selectedNodes = ztreeObj.getCheckedNodes(true);
  293. console.log(selectedNodes);
  294. var len = selectedNodes.length;
  295. // 表格中节点集合
  296. var nodesArrForTable = [];
  297. // 用于删除的节点集合
  298. var nodesArrForRemove = [];
  299. for(var i = 0; i < len; i++){
  300. // 将叶子节点添加进集合
  301. if(!selectedNodes[i].isParenta){
  302. nodesArrForTable.push({id: selectedNodes[i].id, name: selectedNodes[i].name});
  303. nodesArrForRemove.push(selectedNodes[i]);
  304. }
  305. }
  306. if(nodesArrForTable.length == 0){
  307. toastr.error("当前没有选择任何用户", "错误提示");
  308. return false;
  309. }
  310. // 删除节点
  311. for (var i=0; i < nodesArrForRemove.length; i++) {
  312. ztreeObj.removeNode(nodesArrForRemove[i]);
  313. }
  314. // 表格中插入节点
  315. $addVirtualGroupModalUsersTable.bootstrapTable('prepend', nodesArrForTable);
  316. })
  317. // 点击删除按钮
  318. $("#add-virtual-group-modal-users-delete-button").click(function(){
  319. var checkedUsers = $addVirtualGroupModalUsersTable.bootstrapTable('getSelections');
  320. var checkedUsersUidArr = [];
  321. if($addVirtualGroupModalUsersTable.bootstrapTable('getData').length == 0){
  322. toastr.error("当前没有要移除的用户", "错误提示");
  323. return false;
  324. }
  325. if(checkedUsers.length == 0){
  326. toastr.error("请选择要移除的用户", "错误提示");
  327. return false;
  328. }
  329. for(var i = 0; i < checkedUsers.length; i++){
  330. checkedUsersUidArr.push(checkedUsers[i].id);
  331. }
  332. $addVirtualGroupModalUsersTable.bootstrapTable('remove', {
  333. field: 'id', values: checkedUsersUidArr
  334. });
  335. var userNodesForAdd = [];
  336. for(var i = 0; i < checkedUsersUidArr.length; i++){
  337. for(var j = 0; j < allNodes.length; j++){
  338. if(checkedUsersUidArr[i] == allNodes[j].id){
  339. userNodesForAdd.push(allNodes[j]);
  340. }
  341. }
  342. }
  343. console.log("allNodes");
  344. console.log(allNodes);
  345. var ztreeObj = $.fn.zTree.getZTreeObj("add-virtual-group-modal-users-tree");
  346. nowNodes = ztreeObj.transformToArray(ztreeObj.getNodes());
  347. for(var i = 0; i<nowNodes.length; i++){
  348. if('children' in nowNodes[i]){
  349. delete nowNodes[i].children;
  350. }
  351. }
  352. $.fn.zTree.init($("#add-virtual-group-modal-users-tree"), settingForAddVirtualGroupUsers, nowNodes.concat(userNodesForAdd));
  353. })
  354. </script>
  355. </body>
  356. </html>

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号