dept.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. $(function () {
  2. initialPage();
  3. getGrid("../sys/dept/list?name=");
  4. });
  5. function initialPage() {
  6. $(window).resize(function () {
  7. TreeGrid.table.resetHeight({height: $(window).height() - 100});
  8. });
  9. }
  10. const deptData = {}
  11. function getGrid(url) {
  12. var colunms = TreeGrid.initColumn();
  13. var table = new TreeTable(TreeGrid.id, url, colunms);
  14. table.setExpandColumn(2);
  15. table.setIdField("deptId");
  16. table.setCodeField("deptId");
  17. table.setParentCodeField("parentId");
  18. table.setExpandAll(true);
  19. table.setHeight($(window).height() - 100);
  20. table.init();
  21. TreeGrid.table = table;
  22. }
  23. var TreeGrid = {
  24. id: "deptTable",
  25. table: null,
  26. layerIndex: -1
  27. };
  28. /**
  29. * 初始化表格的列
  30. */
  31. TreeGrid.initColumn = function () {
  32. var columns = [
  33. {field: 'selectItem', radio: true},
  34. {title: '公司ID', field: 'deptId', visible: false, align: 'center', valign: 'middle', width: '80px'},
  35. {title: '公司名称', field: 'name', align: 'center', valign: 'middle', sortable: true, width: '180px'},
  36. {title: '上级公司', field: 'parentName', align: 'center', valign: 'middle', sortable: true, width: '100px'},
  37. {title: '排序号', field: 'orderNum', align: 'center', valign: 'middle', sortable: true, width: '100px'}]
  38. return columns;
  39. };
  40. var setting = {
  41. data: {
  42. simpleData: {
  43. enable: true,
  44. idKey: "deptId",
  45. pIdKey: "parentId",
  46. rootPId: -1
  47. },
  48. key: {
  49. url: "nourl"
  50. }
  51. }
  52. };
  53. var ztree;
  54. var vm = new Vue({
  55. el: '#rrapp',
  56. data: {
  57. showList: true,
  58. title: null,
  59. dept: {
  60. parentName: null,
  61. parentId: 0,
  62. orderNum: 0
  63. },
  64. q: {
  65. name: ''
  66. },
  67. ruleValidate: {
  68. name: [
  69. {required: true, message: '公司名称不能为空', trigger: 'blur'}
  70. ]
  71. }
  72. },
  73. methods: {
  74. getDept: function () {
  75. //加载公司树
  76. Ajax.request({
  77. url: "../sys/dept/select",
  78. async: true,
  79. successCallback: function (r) {
  80. ztree = $.fn.zTree.init($("#deptTree"), setting, r.deptList);
  81. var node = ztree.getNodeByParam("deptId", vm.dept.parentId);
  82. if (node) {
  83. ztree.selectNode(node);
  84. vm.dept.parentName = node.name;
  85. } else {
  86. node = ztree.getNodeByParam("deptId", 0);
  87. ztree.selectNode(node);
  88. vm.dept.parentName = node.name;
  89. }
  90. }
  91. });
  92. },
  93. validatorDept:function (id,cb) {
  94. Ajax.request({
  95. url: "../sys/dept/countDeptLevel?deptId=" + id,
  96. async: true,
  97. successCallback: function (r) {
  98. cb(r)
  99. }
  100. });
  101. },
  102. add: function () {
  103. var deptId = TreeGrid.table.getSelectedRow();
  104. var parentId = 0;
  105. if (deptId.length != 0) {
  106. parentId = deptId[0].id;
  107. }
  108. if (parentId === 0) {
  109. vm.showList = false;
  110. vm.title = "新增";
  111. vm.dept = {parentName: null, parentId: parentId, orderNum: 0};
  112. vm.getDept();
  113. return
  114. }
  115. this.validatorDept(parentId || deptId,function (r) {
  116. if (r.num<5) {
  117. vm.showList = false;
  118. vm.title = "新增";
  119. vm.dept = {parentName: null, parentId: parentId, orderNum: 0};
  120. vm.getDept();
  121. }
  122. else{
  123. return alert('部门层级已经达到5层了,无法再创建子部门');
  124. }
  125. })
  126. },
  127. update: function () {
  128. var deptId = getDeptId();
  129. if (!deptId) {
  130. return;
  131. }
  132. Ajax.request({
  133. url: "../sys/dept/info/" + deptId,
  134. async: true,
  135. successCallback: function (r) {
  136. vm.showList = false;
  137. vm.title = "修改";
  138. vm.dept = r.dept;
  139. vm.getDept();
  140. }
  141. });
  142. },
  143. del: function () {
  144. var deptId = getDeptId();
  145. if (!deptId) {
  146. return;
  147. }
  148. confirm('确定要删除选中的记录?', function () {
  149. Ajax.request({
  150. type: "POST",
  151. url: "../sys/dept/delete",
  152. params: {"deptId": deptId},
  153. successCallback: function () {
  154. alert('操作成功', function (index) {
  155. vm.reload();
  156. });
  157. }
  158. });
  159. });
  160. },
  161. saveOrUpdate: function (event) {
  162. var url = vm.dept.deptId == null ? "../sys/dept/save" : "../sys/dept/update";
  163. Ajax.request({
  164. url: url,
  165. contentType: "application/json",
  166. params: JSON.stringify(vm.dept),
  167. type: 'POST',
  168. successCallback: function () {
  169. alert('操作成功', function (index) {
  170. vm.reload();
  171. });
  172. }
  173. });
  174. },
  175. query:function () {
  176. getGrid('../sys/dept/list?name='+vm.q.name);
  177. },
  178. deptTree: function () {
  179. openWindow({
  180. title: "选择公司",
  181. area: ['300px', '450px'],
  182. content: jQuery("#deptLayer"),
  183. btn: ['确定', '取消'],
  184. btn1: function (index) {
  185. var node = ztree.getSelectedNodes();
  186. //选择上级公司
  187. vm.validatorDept(node[0].deptId,function (r) {
  188. if (r.num<5) {
  189. vm.dept.parentId = node[0].deptId;
  190. vm.dept.parentName = node[0].name;
  191. layer.close(index);
  192. }
  193. else{
  194. return alert('部门层级已经达到5层了,无法再创建子部门');
  195. }
  196. })
  197. }
  198. });
  199. },
  200. reload: function () {
  201. vm.showList = true;
  202. TreeGrid.table.refresh();
  203. },
  204. handleSubmit: function (name) {
  205. handleSubmitValidate(this, name, function () {
  206. vm.saveOrUpdate()
  207. });
  208. },
  209. handleReset: function (name) {
  210. handleResetForm(this, name);
  211. }
  212. }
  213. });
  214. function getDeptId() {
  215. var selected = $('#deptTable').bootstrapTreeTable('getSelections');
  216. if (selected.length == 0) {
  217. alert("请选择一条记录");
  218. return false;
  219. } else {
  220. return selected[0].id;
  221. }
  222. }