_groupTree.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. layui.define(['jquery', 'layer', 'uParas', 'form', 'admin', 'zTree', 'formSelects', 'iconPicker'], function (exports) {
  2. var $ = layui.$, uParas = layui.uParas, iconPicker = layui.iconPicker, form = layui.form;
  3. admin = layui.admin, formSelectsFormSelectsV4 = layui.formSelects;
  4. var _g = {
  5. init: function (insTb) {
  6. var setting = {
  7. check: {
  8. enable: false,
  9. chkboxType: {"Y": "", "N": ""}
  10. },
  11. view: {
  12. dblClickExpand: false
  13. },
  14. data: {
  15. simpleData: {
  16. enable: true,
  17. idKey: "gId",
  18. pIdKey: "groupParent",
  19. rootPId: null
  20. },
  21. key: {
  22. name: "groupName"
  23. }
  24. },
  25. callback: {
  26. onClick: onClick
  27. }
  28. };
  29. /**
  30. * ztree 文本下拉
  31. */
  32. admin.req(uParas.baseUrl + '/admin/common/getGroupLists', {}, function (res) {
  33. if (res.code == 1) {
  34. $.fn.zTree.init($('#groupTree'), setting, res.data);
  35. $.fn.zTree.getZTreeObj("groupTree").expandAll(true);
  36. layer.closeAll('loading');
  37. hideMenu();
  38. }
  39. }, 'get');
  40. function onClick(event, treeId, treeNode, clickFlag) {
  41. var zTree = $.fn.zTree.getZTreeObj("groupTree"),
  42. nodes = zTree.getSelectedNodes(),
  43. v = "", idlist = "";
  44. for (var i = 0, l = nodes.length; i < l; i++) {
  45. v += nodes[i].groupName + ",";
  46. idlist += nodes[i].gId + ",";
  47. }
  48. if (v.length > 0) {
  49. v = v.substring(0, v.length - 1);
  50. idlist = idlist.substring(0, idlist.length - 1);
  51. }
  52. var cityObj = $("#groupName");
  53. if (cityObj.val() == v) {
  54. cityObj.attr("value", "点击选择");
  55. $("#groupId").attr("value", "");
  56. } else {
  57. cityObj.attr("value", v);
  58. $("#groupId").attr("value", idlist);
  59. }
  60. hideMenu();
  61. var groupId = idlist;
  62. if (groupId != "-1") {
  63. var Obj = new Object();
  64. Obj.gId = groupId;
  65. if (insTb != null && insTb != undefined) {
  66. insTb.reload({where: Obj}, 'data');
  67. }
  68. }
  69. }
  70. function hideMenu() {
  71. $("#menuContent").fadeOut("fast");
  72. $("body").unbind("mousedown", onBodyDown);
  73. }
  74. $("#groupName").click(function () {
  75. $("#menuContent").slideDown("fast");
  76. $("body").bind("mousedown", onBodyDown);
  77. });
  78. function onBodyDown(event) {
  79. if (!(event.target.id === "groupName" || event.target.id === "menuContent" || $(event.target).parents("#menuContent").length > 0)) {
  80. hideMenu();
  81. }
  82. }
  83. },
  84. initgroupTreeAndUsers: function (groupUrl, groupTree, groupName, groupId, menuContent,divName, formFilter, valStr) {
  85. var setting = {
  86. check: {
  87. enable: false,
  88. chkboxType: {"Y": "", "N": ""}
  89. },
  90. view: {
  91. dblClickExpand: false
  92. },
  93. data: {
  94. simpleData: {
  95. enable: true,
  96. idKey: "gId",
  97. pIdKey: "groupParent",
  98. rootPId: null
  99. },
  100. key: {
  101. name: "groupName"
  102. }
  103. },
  104. callback: {
  105. onClick: onClick
  106. }
  107. };
  108. /**
  109. * ztree 文本下拉
  110. */
  111. $.getJSON(uParas.baseUrl + groupUrl, {}, function (res) {
  112. if (res.code == 1) {
  113. $.fn.zTree.init($('#' + groupTree), setting, res.data);
  114. $.fn.zTree.getZTreeObj(groupTree).expandAll(true);
  115. //初始化已选中的值
  116. if (valStr != undefined && valStr != ''){
  117. initChecked();
  118. }
  119. layer.closeAll('loading');
  120. hideMenu();
  121. }
  122. });
  123. function onClick(event, treeId, treeNode, clickFlag) {
  124. var zTree = $.fn.zTree.getZTreeObj(groupTree),
  125. nodes = zTree.getSelectedNodes(),
  126. v = "", idlist = "";
  127. for (var i = 0, l = nodes.length; i < l; i++) {
  128. v += nodes[i].groupName + ",";
  129. idlist += nodes[i].gId + ",";
  130. }
  131. if (v.length > 0) {
  132. v = v.substring(0, v.length - 1);
  133. idlist = idlist.substring(0, idlist.length - 1);
  134. }
  135. var cityObj = $("#" + groupName);
  136. if (cityObj.val() == v) {
  137. cityObj.attr("value", "");
  138. $("#" + groupId).attr("value", "");
  139. } else {
  140. cityObj.attr("value", v);
  141. $("#" + groupId).attr("value", idlist);
  142. }
  143. hideMenu();
  144. var groupId_ = idlist;
  145. if (groupId_ != "-1") {
  146. var Obj = new Object();
  147. Obj.gId = groupId_;
  148. //初始化人员
  149. initUsers(groupId_);
  150. }
  151. }
  152. function initChecked() {
  153. debugger
  154. var groupName_ = $("#" + groupName).val();
  155. var groupId_ = $("#" + groupId).val();
  156. var zTree = $.fn.zTree.getZTreeObj(groupTree);
  157. var checkeds = zTree.getNodes();
  158. //遍历所有节点
  159. $(checkeds).each(function (i) {
  160. //判断得到  中国  这个节点 
  161. if (zTree.getNodes()[i].groupName == groupName_ || zTree.getNodes()[i].gId == groupId_) {
  162. //    1. 勾选这个节点
  163. zTree.checkNode(zTree.getNodes()[i], true, true);
  164. //      2.设置文本域
  165. //设置执行区域显示的文本
  166. $("#" + groupName).val(zTree.getNodes()[i].groupName);
  167. //设置执行区域实际获得的值
  168. $("#" + groupId).val(zTree.getNodes()[i].gId);
  169. }
  170. });
  171. //初始化已选中的人员
  172. initUsers(groupId_);
  173. }
  174. function initUsers(groupId_) {
  175. admin.req(uParas.baseUrl + '/admin/common/getUsersByGroupId?gId=' + groupId_, {}, function (res) {
  176. if (res.code == 1) {
  177. // $("#" + divName + "").attr("lay-verify", 'required');
  178. var html = '<option value="">==请选择==</option>';
  179. $.each(res.data, function (i, v) {
  180. if (valStr == v["aId"]) {
  181. html += '<option value="' + v["aId"] + '" selected>' + v["accountRealName"] + '</option>';
  182. } else {
  183. html += '<option value="' + v["aId"] + '">' + v["accountRealName"] + '</option>';
  184. }
  185. });
  186. $("#" + divName + "").html(html);
  187. form.render('select', formFilter);
  188. }
  189. }, 'get');
  190. }
  191. function hideMenu() {
  192. $("#" + menuContent).fadeOut("fast");
  193. $("body").unbind("mousedown", onBodyDown);
  194. }
  195. $("#" + groupName).click(function () {
  196. $("#" + menuContent).slideDown("fast");
  197. $("body").bind("mousedown", onBodyDown);
  198. });
  199. function onBodyDown(event) {
  200. if (!(event.target.id === groupName || event.target.id === menuContent || $(event.target).parents("#" + menuContent).length > 0)) {
  201. hideMenu();
  202. }
  203. }
  204. },
  205. /**初始化部门过滤*/
  206. initGroupColation: function (insTb, groupUrl, groupTree, groupName, groupId, menuContent) {
  207. /**
  208. * ztree 文本下拉
  209. */
  210. admin.req(uParas.baseUrl + groupUrl, {}, function (res) {
  211. if (res.code == 1) {
  212. $.fn.zTree.init($('#' + groupTree), setting, res.data);
  213. $.fn.zTree.getZTreeObj(groupTree).expandAll(true);
  214. //初始化已选中的值
  215. initChecked();
  216. layer.closeAll('loading');
  217. hideMenu();
  218. }
  219. }, 'get');
  220. var setting = {
  221. check: {
  222. enable: false,
  223. chkboxType: {"Y": "", "N": ""}
  224. },
  225. view: {
  226. dblClickExpand: false
  227. },
  228. data: {
  229. simpleData: {
  230. enable: true,
  231. idKey: "gId",
  232. pIdKey: "groupParent",
  233. rootPId: null
  234. },
  235. key: {
  236. name: "groupName"
  237. }
  238. },
  239. callback: {
  240. onClick: onClick
  241. }
  242. };
  243. function onClick(event, treeId, treeNode, clickFlag) {
  244. var zTree = $.fn.zTree.getZTreeObj(groupTree),
  245. nodes = zTree.getSelectedNodes(),
  246. v = "", idlist = "";
  247. for (var i = 0, l = nodes.length; i < l; i++) {
  248. v += nodes[i].groupName + ",";
  249. idlist += nodes[i].gId + ",";
  250. }
  251. if (v.length > 0) {
  252. v = v.substring(0, v.length - 1);
  253. idlist = idlist.substring(0, idlist.length - 1);
  254. }
  255. var cityObj = $("#" + groupName);
  256. if (cityObj.val() == v) {
  257. cityObj.attr("value", "");
  258. $("#" + groupId).attr("value", "");
  259. } else {
  260. cityObj.attr("value", v);
  261. $("#" + groupId).attr("value", idlist);
  262. }
  263. hideMenu();
  264. var groupId_ = idlist;
  265. if (groupId_ != "-1") {
  266. var Obj = new Object();
  267. Obj.gId = groupId_;
  268. }
  269. if (insTb != null && insTb != undefined && insTb != '') {
  270. insTb.reload({where: Obj}, 'data');
  271. }
  272. }
  273. function initChecked() {
  274. var groupName_ = $("#" + groupName).val();
  275. var groupId_ = $("#" + groupId).val();
  276. var zTree = $.fn.zTree.getZTreeObj(groupTree);
  277. var checkeds = zTree.getNodes();
  278. //遍历所有节点
  279. $(checkeds).each(function (i) {
  280. //判断得到  中国  这个节点 
  281. if (zTree.getNodes()[i].groupName == groupName_ || zTree.getNodes()[i].groupId == groupId_) {
  282. //    1. 勾选这个节点
  283. zTree.checkNode(zTree.getNodes()[i], true, true);
  284. //      2.设置文本域
  285. //设置执行区域显示的文本
  286. $("#" + groupName).val(zTree.getNodes()[i].groupName);
  287. //设置执行区域实际获得的值
  288. $("#" + groupId).val(zTree.getNodes()[i].gId);
  289. }
  290. });
  291. }
  292. function hideMenu() {
  293. $("#" + menuContent).fadeOut("fast");
  294. $("body").unbind("mousedown", onBodyDown);
  295. }
  296. $("#" + groupName).click(function () {
  297. $("#" + menuContent).slideDown("fast");
  298. $("body").bind("mousedown", onBodyDown);
  299. });
  300. function onBodyDown(event) {
  301. if (!(event.target.id === groupName || event.target.id === menuContent || $(event.target).parents("#" + menuContent).length > 0)) {
  302. hideMenu();
  303. }
  304. }
  305. }
  306. }
  307. exports("_groupTree", _g);
  308. });