ry-ui.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /**
  2. * 通用js方法封装处理
  3. * Copyright (c) 2018 ruoyi
  4. */
  5. (function ($) {
  6. $.extend({
  7. _treeTable: {},
  8. _tree: {},
  9. // 表格封装处理
  10. table: {
  11. _option: {},
  12. _params: {},
  13. // 初始化表格参数
  14. init: function(options) {
  15. $.table._option = options;
  16. $.table._params = $.common.isEmpty(options.queryParams) ? $.table.queryParams : options.queryParams;
  17. _sortOrder = $.common.isEmpty(options.sortOrder) ? "asc" : options.sortOrder;
  18. _sortName = $.common.isEmpty(options.sortName) ? "" : options.sortName;
  19. _striped = $.common.isEmpty(options.striped) ? false : options.striped;
  20. _escape = $.common.isEmpty(options.escape) ? false : options.escape;
  21. $('#bootstrap-table').bootstrapTable({
  22. url: options.url, // 请求后台的URL(*)
  23. contentType: "application/x-www-form-urlencoded", // 编码类型
  24. method: 'post', // 请求方式(*)
  25. cache: false, // 是否使用缓存
  26. striped: _striped, // 是否显示行间隔色
  27. sortable: true, // 是否启用排序
  28. sortStable: true, // 设置为 true 将获得稳定的排序
  29. sortName: _sortName, // 排序列名称
  30. sortOrder: _sortOrder, // 排序方式 asc 或者 desc
  31. pagination: $.common.visible(options.pagination), // 是否显示分页(*)
  32. pageNumber: 1, // 初始化加载第一页,默认第一页
  33. pageSize: 10, // 每页的记录行数(*)
  34. pageList: [10, 25, 50], // 可供选择的每页的行数(*)
  35. escape: _escape, // 转义HTML字符串
  36. iconSize: 'outline', // 图标大小:undefined默认的按钮尺寸 xs超小按钮sm小按钮lg大按钮
  37. toolbar: '#toolbar', // 指定工作栏
  38. sidePagination: "server", // 启用服务端分页
  39. search: $.common.visible(options.search), // 是否显示搜索框功能
  40. showSearch: $.common.visible(options.showSearch), // 是否显示检索信息
  41. showRefresh: $.common.visible(options.showRefresh), // 是否显示刷新按钮
  42. showColumns: $.common.visible(options.showColumns), // 是否显示隐藏某列下拉框
  43. showToggle: $.common.visible(options.showToggle), // 是否显示详细视图和列表视图的切换按钮
  44. showExport: $.common.visible(options.showExport), // 是否支持导出文件
  45. queryParams: $.table._params, // 传递参数(*)
  46. columns: options.columns, // 显示列信息(*)
  47. responseHandler: $.table.responseHandler // 回调函数
  48. });
  49. },
  50. // 查询条件
  51. queryParams: function(params) {
  52. return {
  53. // 传递参数查询参数
  54. pageSize: params.limit,
  55. pageNum: params.offset / params.limit + 1,
  56. searchValue: params.search,
  57. orderByColumn: params.sort,
  58. isAsc: params.order
  59. };
  60. },
  61. // 请求获取数据后处理回调函数
  62. responseHandler: function(res) {
  63. if (res.code == 0) {
  64. return { rows: res.rows, total: res.total };
  65. } else {
  66. $.modal.alertWarning(res.msg);
  67. return { rows: [], total: 0 };
  68. }
  69. },
  70. // 序列号生成
  71. serialNumber: function (index) {
  72. var table = $('#bootstrap-table').bootstrapTable('getOptions');
  73. var pageSize = table.pageSize;
  74. var pageNumber = table.pageNumber;
  75. return pageSize * (pageNumber - 1) + index + 1;
  76. },
  77. // 搜索-默认第一个form
  78. search: function(formId) {
  79. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  80. var params = $("#bootstrap-table").bootstrapTable('getOptions');
  81. params.queryParams = function(params) {
  82. var search = {};
  83. $.each($("#" + currentId).serializeArray(), function(i, field) {
  84. search[field.name] = field.value;
  85. });
  86. search.pageSize = params.limit;
  87. search.pageNum = params.offset / params.limit + 1;
  88. search.searchValue = params.search;
  89. search.orderByColumn = params.sort;
  90. search.isAsc = params.order;
  91. return search;
  92. }
  93. $("#bootstrap-table").bootstrapTable('refresh', params);
  94. },
  95. // 下载-默认第一个form
  96. exportExcel: function(formId) {
  97. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  98. $.modal.loading("正在导出数据,请稍后...");
  99. $.post($.table._option.exportUrl, $("#" + currentId).serializeArray(), function(result) {
  100. if (result.code == web_status.SUCCESS) {
  101. window.location.href = ctx + "common/download?fileName=" + result.msg + "&delete=" + true;
  102. } else {
  103. $.modal.alertError(result.msg);
  104. }
  105. $.modal.closeLoading();
  106. });
  107. },
  108. // 刷新表格
  109. refresh: function() {
  110. $("#bootstrap-table").bootstrapTable('refresh', {
  111. silent: true
  112. });
  113. },
  114. // 查询表格指定列值
  115. selectColumns: function(column) {
  116. return $.map($('#bootstrap-table').bootstrapTable('getSelections'), function (row) {
  117. return row[column];
  118. });
  119. },
  120. // 查询表格首列值
  121. selectFirstColumns: function() {
  122. return $.map($('#bootstrap-table').bootstrapTable('getSelections'), function (row) {
  123. return row[$.table._option.columns[1].field];
  124. });
  125. },
  126. // 回显数据字典
  127. selectDictLabel: function(datas, value) {
  128. var actions = [];
  129. $.each(datas, function(index, dict) {
  130. if (dict.dictValue == value) {
  131. actions.push("<span class='badge badge-" + dict.listClass + "'>" + dict.dictLabel + "</span>");
  132. return false;
  133. }
  134. });
  135. return actions.join('');
  136. },
  137. // 显示表格指定列
  138. showColumn: function(column) {
  139. $("#bootstrap-table").bootstrapTable('showColumn', column);
  140. },
  141. // 隐藏表格指定列
  142. hideColumn: function(column) {
  143. $("#bootstrap-table").bootstrapTable('hideColumn', column);
  144. }
  145. },
  146. // 表格树封装处理
  147. treeTable: {
  148. _option: {},
  149. // 初始化表格
  150. init: function(options) {
  151. $.table._option = options;
  152. _striped = $.common.isEmpty(options.striped) ? false : options.striped;
  153. _expandColumn = $.common.isEmpty(options.expandColumn) ? '1' : options.expandColumn;
  154. var treeTable = $('#bootstrap-tree-table').bootstrapTreeTable({
  155. code: options.code, // 用于设置父子关系
  156. parentCode: options.parentCode, // 用于设置父子关系
  157. type: 'get', // 请求方式(*)
  158. url: options.url, // 请求后台的URL(*)
  159. ajaxParams: {}, // 请求数据的ajax的data属性
  160. expandColumn: _expandColumn, // 在哪一列上面显示展开按钮
  161. striped: _striped, // 是否显示行间隔色
  162. bordered: true, // 是否显示边框
  163. toolbar: '#toolbar', // 指定工作栏
  164. showRefresh: $.common.visible(options.showRefresh), // 是否显示刷新按钮
  165. showColumns: $.common.visible(options.showColumns), // 是否显示隐藏某列下拉框
  166. expandAll: $.common.visible(options.expandAll), // 是否全部展开
  167. expandFirst: $.common.visible(options.expandFirst), // 是否默认第一级展开--expandAll为false时生效
  168. columns: options.columns
  169. });
  170. $._treeTable = treeTable;
  171. },
  172. // 条件查询
  173. search: function(formId) {
  174. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  175. var params = {};
  176. $.each($("#" + currentId).serializeArray(), function(i, field) {
  177. params[field.name] = field.value;
  178. });
  179. $._treeTable.bootstrapTreeTable('refresh', params);
  180. },
  181. // 刷新
  182. refresh: function() {
  183. $._treeTable.bootstrapTreeTable('refresh');
  184. },
  185. },
  186. // 表单封装处理
  187. form: {
  188. // 表单重置
  189. reset: function(formId) {
  190. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  191. $("#" + currentId)[0].reset();
  192. },
  193. // 获取选中复选框项
  194. selectCheckeds: function(name) {
  195. var checkeds = "";
  196. $('input:checkbox[name="' + name + '"]:checked').each(function(i) {
  197. if (0 == i) {
  198. checkeds = $(this).val();
  199. } else {
  200. checkeds += ("," + $(this).val());
  201. }
  202. });
  203. return checkeds;
  204. },
  205. // 获取选中下拉框项
  206. selectSelects: function(name) {
  207. var selects = "";
  208. $('#' + name + ' option:selected').each(function (i) {
  209. if (0 == i) {
  210. selects = $(this).val();
  211. } else {
  212. selects += ("," + $(this).val());
  213. }
  214. });
  215. return selects;
  216. }
  217. },
  218. // 弹出层封装处理
  219. modal: {
  220. // 显示图标
  221. icon: function(type) {
  222. var icon = "";
  223. if (type == modal_status.WARNING) {
  224. icon = 0;
  225. } else if (type == modal_status.SUCCESS) {
  226. icon = 1;
  227. } else if (type == modal_status.FAIL) {
  228. icon = 2;
  229. } else {
  230. icon = 3;
  231. }
  232. return icon;
  233. },
  234. // 消息提示
  235. msg: function(content, type) {
  236. if (type != undefined) {
  237. layer.msg(content, { icon: $.modal.icon(type), time: 1000, shift: 5 });
  238. } else {
  239. layer.msg(content);
  240. }
  241. },
  242. // 错误消息
  243. msgError: function(content) {
  244. $.modal.msg(content, modal_status.FAIL);
  245. },
  246. // 成功消息
  247. msgSuccess: function(content) {
  248. $.modal.msg(content, modal_status.SUCCESS);
  249. },
  250. // 警告消息
  251. msgWarning: function(content) {
  252. $.modal.msg(content, modal_status.WARNING);
  253. },
  254. // 弹出提示
  255. alert: function(content, type) {
  256. layer.alert(content, {
  257. icon: $.modal.icon(type),
  258. title: "系统提示",
  259. btn: ['确认'],
  260. btnclass: ['btn btn-primary'],
  261. });
  262. },
  263. // 消息提示并刷新父窗体
  264. msgReload: function(msg, type) {
  265. layer.msg(msg, {
  266. icon: $.modal.icon(type),
  267. time: 500,
  268. shade: [0.1, '#8F8F8F']
  269. },
  270. function() {
  271. $.modal.reload();
  272. });
  273. },
  274. // 错误提示
  275. alertError: function(content) {
  276. $.modal.alert(content, modal_status.FAIL);
  277. },
  278. // 成功提示
  279. alertSuccess: function(content) {
  280. $.modal.alert(content, modal_status.SUCCESS);
  281. },
  282. // 警告提示
  283. alertWarning: function(content) {
  284. $.modal.alert(content, modal_status.WARNING);
  285. },
  286. // 关闭窗体
  287. close: function () {
  288. var index = parent.layer.getFrameIndex(window.name);
  289. parent.layer.close(index);
  290. },
  291. // 确认窗体
  292. confirm: function (content, callBack) {
  293. layer.confirm(content, {
  294. icon: 3,
  295. title: "系统提示",
  296. btn: ['确认', '取消'],
  297. btnclass: ['btn btn-primary', 'btn btn-danger'],
  298. }, function (index) {
  299. layer.close(index);
  300. callBack(true);
  301. });
  302. },
  303. // 弹出层指定宽度
  304. open: function (title, url, width, height) {
  305. //如果是移动端,就使用自适应大小弹窗
  306. if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
  307. width = 'auto';
  308. height = 'auto';
  309. }
  310. if ($.common.isEmpty(title)) {
  311. title = false;
  312. };
  313. if ($.common.isEmpty(url)) {
  314. url = "/404.html";
  315. };
  316. if ($.common.isEmpty(width)) {
  317. width = 800;
  318. };
  319. if ($.common.isEmpty(height)) {
  320. height = ($(window).height() - 50);
  321. };
  322. layer.open({
  323. type: 2,
  324. area: [width + 'px', height + 'px'],
  325. fix: false,
  326. //不固定
  327. maxmin: true,
  328. shade: 0.3,
  329. title: title,
  330. content: url,
  331. btn: ['确定', '关闭'],
  332. // 弹层外区域关闭
  333. shadeClose: true,
  334. yes: function(index, layero) {
  335. var iframeWin = layero.find('iframe')[0];
  336. iframeWin.contentWindow.submitHandler();
  337. },
  338. cancel: function(index) {
  339. return true;
  340. }
  341. });
  342. },
  343. // 弹出层指定参数选项
  344. openOptions: function (options) {
  345. var _url = $.common.isEmpty(options.url) ? "/404.html" : options.url;
  346. var _title = $.common.isEmpty(options.title) ? "系统窗口" : options.title;
  347. var _width = $.common.isEmpty(options.width) ? "800" : options.width;
  348. var _height = $.common.isEmpty(options.height) ? ($(window).height() - 50) : options.height;
  349. layer.open({
  350. type: 2,
  351. maxmin: true,
  352. shade: 0.3,
  353. title: _title,
  354. fix: false,
  355. area: [_width + 'px', _height + 'px'],
  356. content: _url,
  357. shadeClose: true,
  358. btn: ['<i class="fa fa-check"></i> 确认', '<i class="fa fa-close"></i> 关闭'],
  359. yes: function (index, layero) {
  360. options.callBack(index, layero)
  361. }, cancel: function () {
  362. return true;
  363. }
  364. });
  365. },
  366. // 弹出层全屏
  367. openFull: function (title, url, width, height) {
  368. //如果是移动端,就使用自适应大小弹窗
  369. if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
  370. width = 'auto';
  371. height = 'auto';
  372. }
  373. if ($.common.isEmpty(title)) {
  374. title = false;
  375. };
  376. if ($.common.isEmpty(url)) {
  377. url = "/404.html";
  378. };
  379. if ($.common.isEmpty(width)) {
  380. width = 800;
  381. };
  382. if ($.common.isEmpty(height)) {
  383. height = ($(window).height() - 50);
  384. };
  385. var index = layer.open({
  386. type: 2,
  387. area: [width + 'px', height + 'px'],
  388. fix: false,
  389. //不固定
  390. maxmin: true,
  391. shade: 0.3,
  392. title: title,
  393. content: url,
  394. btn: ['确定', '关闭'],
  395. // 弹层外区域关闭
  396. shadeClose: true,
  397. yes: function(index, layero) {
  398. var iframeWin = layero.find('iframe')[0];
  399. iframeWin.contentWindow.submitHandler();
  400. },
  401. cancel: function(index) {
  402. return true;
  403. }
  404. });
  405. layer.full(index);
  406. },
  407. // 打开遮罩层
  408. loading: function (message) {
  409. $.blockUI({ message: '<div class="loaderbox"><div class="loading-activity"></div> ' + message + '</div>' });
  410. },
  411. // 关闭遮罩层
  412. closeLoading: function () {
  413. setTimeout(function(){
  414. $.unblockUI();
  415. }, 50);
  416. },
  417. // 重新加载
  418. reload: function () {
  419. parent.location.reload();
  420. }
  421. },
  422. // 操作封装处理
  423. operate: {
  424. // 提交数据
  425. submit: function(url, type, dataType, data) {
  426. $.modal.loading("正在处理中,请稍后...");
  427. var config = {
  428. url: url,
  429. type: type,
  430. dataType: dataType,
  431. data: data,
  432. success: function(result) {
  433. $.operate.ajaxSuccess(result);
  434. }
  435. };
  436. $.ajax(config)
  437. },
  438. // post请求传输
  439. post: function(url, data) {
  440. $.operate.submit(url, "post", "json", data);
  441. },
  442. // get请求传输
  443. get: function(url) {
  444. $.operate.submit(url, "get", "json", "");
  445. },
  446. // 详细信息
  447. detail: function(id, width, height) {
  448. var _url = $.common.isEmpty(id) ? $.table._option.detailUrl : $.table._option.detailUrl.replace("{id}", id);
  449. var _width = $.common.isEmpty(width) ? "800" : width;
  450. var _height = $.common.isEmpty(height) ? ($(window).height() - 50) : height;
  451. //如果是移动端,就使用自适应大小弹窗
  452. if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
  453. _width = 'auto';
  454. _height = 'auto';
  455. }
  456. layer.open({
  457. type: 2,
  458. area: [_width + 'px', _height + 'px'],
  459. fix: false,
  460. //不固定
  461. maxmin: true,
  462. shade: 0.3,
  463. title: $.table._option.modalName + "详细",
  464. content: _url,
  465. btn: '关闭',
  466. // 弹层外区域关闭
  467. shadeClose: true,
  468. success: function(layer) {
  469. layer[0].childNodes[3].childNodes[0].attributes[0].value='layui-layer-btn1';
  470. },
  471. btn1: function(index) {
  472. layer.close(index);
  473. }
  474. });
  475. },
  476. // 删除信息
  477. remove: function(id) {
  478. $.modal.confirm("确定删除该条" + $.table._option.modalName + "信息吗?", function() {
  479. var url = $.common.isEmpty(id) ? $.table._option.removeUrl : $.table._option.removeUrl.replace("{id}", id);
  480. var data = { "ids": id };
  481. $.operate.submit(url, "post", "json", data);
  482. });
  483. },
  484. // 批量删除信息
  485. removeAll: function() {
  486. var rows = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);
  487. if (rows.length == 0) {
  488. $.modal.alertWarning("请至少选择一条记录");
  489. return;
  490. }
  491. $.modal.confirm("确认要删除选中的" + rows.length + "条数据吗?", function() {
  492. var url = $.table._option.removeUrl;
  493. var data = { "ids": rows.join() };
  494. $.operate.submit(url, "post", "json", data);
  495. });
  496. },
  497. // 清空信息
  498. clean: function() {
  499. $.modal.confirm("确定清空所有" + $.table._option.modalName + "吗?", function() {
  500. var url = $.table._option.cleanUrl;
  501. $.operate.submit(url, "post", "json", "");
  502. });
  503. },
  504. // 添加信息
  505. add: function(id) {
  506. var url = $.common.isEmpty(id) ? $.table._option.createUrl : $.table._option.createUrl.replace("{id}", id);
  507. $.modal.open("添加" + $.table._option.modalName, url);
  508. },
  509. // 修改信息
  510. edit: function(id) {
  511. var url = "/404.html";
  512. if ($.common.isNotEmpty(id)) {
  513. url = $.table._option.updateUrl.replace("{id}", id);
  514. } else {
  515. var id = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);
  516. if (id.length == 0) {
  517. $.modal.alertWarning("请至少选择一条记录");
  518. return;
  519. }
  520. url = $.table._option.updateUrl.replace("{id}", id);
  521. }
  522. $.modal.open("修改" + $.table._option.modalName, url);
  523. },
  524. // 工具栏表格树修改
  525. editTree: function() {
  526. var row = $('#bootstrap-tree-table').bootstrapTreeTable('getSelections')[0];
  527. if ($.common.isEmpty(row)) {
  528. $.modal.alertWarning("请至少选择一条记录");
  529. return;
  530. }
  531. var url = $.table._option.updateUrl.replace("{id}", row[$.table._option.uniqueId]);
  532. $.modal.open("修改" + $.table._option.modalName, url);
  533. },
  534. // 添加信息 全屏
  535. addFull: function(id) {
  536. var url = $.common.isEmpty(id) ? $.table._option.createUrl : $.table._option.createUrl.replace("{id}", id);
  537. $.modal.openFull("添加" + $.table._option.modalName, url);
  538. },
  539. // 修改信息 全屏
  540. editFull: function(id) {
  541. var url = "/404.html";
  542. if ($.common.isNotEmpty(id)) {
  543. url = $.table._option.updateUrl.replace("{id}", id);
  544. } else {
  545. var row = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);
  546. url = $.table._option.updateUrl.replace("{id}", row);
  547. }
  548. $.modal.openFull("修改" + $.table._option.modalName, url);
  549. },
  550. // 保存信息
  551. save: function(url, data) {
  552. $.modal.loading("正在处理中,请稍后...");
  553. var config = {
  554. url: url,
  555. type: "post",
  556. dataType: "json",
  557. data: data,
  558. success: function(result) {
  559. $.operate.successCallback(result);
  560. }
  561. };
  562. $.ajax(config)
  563. },
  564. // 保存结果弹出msg刷新table表格
  565. ajaxSuccess: function (result) {
  566. if (result.code == web_status.SUCCESS) {
  567. $.modal.msgSuccess(result.msg);
  568. $.table.refresh();
  569. } else {
  570. $.modal.alertError(result.msg);
  571. }
  572. $.modal.closeLoading();
  573. },
  574. // 成功结果提示msg(父窗体全局更新)
  575. saveSuccess: function (result) {
  576. if (result.code == web_status.SUCCESS) {
  577. $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
  578. } else {
  579. $.modal.alertError(result.msg);
  580. }
  581. $.modal.closeLoading();
  582. },
  583. // 成功回调执行事件(父窗体静默更新)
  584. successCallback: function(result) {
  585. if (result.code == web_status.SUCCESS) {
  586. if (window.parent.$("#bootstrap-table").length > 0) {
  587. $.modal.close();
  588. window.parent.$.modal.msgSuccess(result.msg);
  589. window.parent.$.table.refresh();
  590. } else if (window.parent.$("#bootstrap-tree-table").length > 0) {
  591. $.modal.close();
  592. window.parent.$.modal.msgSuccess(result.msg);
  593. window.parent.$.treeTable.refresh();
  594. } else {
  595. $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
  596. }
  597. } else {
  598. $.modal.alertError(result.msg);
  599. }
  600. $.modal.closeLoading();
  601. }
  602. },
  603. // 校验封装处理
  604. validate: {
  605. // 判断返回标识是否唯一 false 不存在 true 存在
  606. unique: function (value) {
  607. if (value == "0") {
  608. return true;
  609. }
  610. return false;
  611. },
  612. // 表单验证
  613. form: function (formId) {
  614. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  615. return $("#" + currentId).validate().form();
  616. }
  617. },
  618. // 树插件封装处理
  619. tree: {
  620. _option: {},
  621. _lastValue: {},
  622. // 初始化树结构
  623. init: function(options) {
  624. $.tree._option = options;
  625. // 属性ID
  626. var _id = $.common.isEmpty(options.id) ? "tree" : options.id;
  627. // 展开等级节点
  628. var _expandLevel = $.common.isEmpty(options.expandLevel) ? 0 : options.expandLevel;
  629. // 树结构初始化加载
  630. var setting = {
  631. check: options.check,
  632. view: { selectedMulti: false, nameIsHTML: true },
  633. data: { key: { title: "title" }, simpleData: { enable: true } },
  634. callback: { onClick: options.onClick }
  635. };
  636. $.get(options.url, function(data) {
  637. var treeName = $("#treeName").val();
  638. var treeId = $("#treeId").val();
  639. tree = $.fn.zTree.init($("#" + _id), setting, data);
  640. $._tree = tree;
  641. // 展开第一级节点
  642. var nodes = tree.getNodesByParam("level", 0);
  643. for (var i = 0; i < nodes.length; i++) {
  644. if(_expandLevel > 0) {
  645. tree.expandNode(nodes[i], true, false, false);
  646. }
  647. $.tree.selectByIdName(treeId, treeName, nodes[i]);
  648. }
  649. // 展开第二级节点
  650. nodes = tree.getNodesByParam("level", 1);
  651. for (var i = 0; i < nodes.length; i++) {
  652. if(_expandLevel > 1) {
  653. tree.expandNode(nodes[i], true, false, false);
  654. }
  655. $.tree.selectByIdName(treeId, treeName, nodes[i]);
  656. }
  657. // 展开第三级节点
  658. nodes = tree.getNodesByParam("level", 2);
  659. for (var i = 0; i < nodes.length; i++) {
  660. if(_expandLevel > 2) {
  661. tree.expandNode(nodes[i], true, false, false);
  662. }
  663. $.tree.selectByIdName(treeId, treeName, nodes[i]);
  664. }
  665. }, null, null, "正在加载,请稍后...");
  666. },
  667. // 搜索节点
  668. searchNode: function() {
  669. // 取得输入的关键字的值
  670. var value = $.common.trim($("#keyword").val());
  671. if ($.tree._lastValue === value) {
  672. return;
  673. }
  674. // 保存最后一次搜索名称
  675. $.tree._lastValue = value;
  676. var nodes = $._tree.getNodes();
  677. // 如果要查空字串,就退出不查了。
  678. if (value == "") {
  679. $.tree.showAllNode(nodes);
  680. return;
  681. }
  682. $.tree.hideAllNode(nodes);
  683. // 根据搜索值模糊匹配
  684. $.tree.updateNodes($._tree.getNodesByParamFuzzy("name", value));
  685. },
  686. // 根据Id和Name选中指定节点
  687. selectByIdName: function(treeId, treeName, node) {
  688. if ($.common.isNotEmpty(treeName) && $.common.isNotEmpty(treeId)) {
  689. if (treeId == node.id && treeName == node.name) {
  690. $._tree.selectNode(node, true);
  691. }
  692. }
  693. },
  694. // 显示所有节点
  695. showAllNode: function(nodes) {
  696. nodes = $._tree.transformToArray(nodes);
  697. for (var i = nodes.length - 1; i >= 0; i--) {
  698. if (nodes[i].getParentNode() != null) {
  699. $._tree.expandNode(nodes[i], true, false, false, false);
  700. } else {
  701. $._tree.expandNode(nodes[i], true, true, false, false);
  702. }
  703. $._tree.showNode(nodes[i]);
  704. $.tree.showAllNode(nodes[i].children);
  705. }
  706. },
  707. // 隐藏所有节点
  708. hideAllNode: function(nodes) {
  709. var tree = $.fn.zTree.getZTreeObj("tree");
  710. var nodes = $._tree.transformToArray(nodes);
  711. for (var i = nodes.length - 1; i >= 0; i--) {
  712. $._tree.hideNode(nodes[i]);
  713. }
  714. },
  715. // 显示所有父节点
  716. showParent: function(treeNode) {
  717. var parentNode;
  718. while ((parentNode = treeNode.getParentNode()) != null) {
  719. $._tree.showNode(parentNode);
  720. $._tree.expandNode(parentNode, true, false, false);
  721. treeNode = parentNode;
  722. }
  723. },
  724. // 显示所有孩子节点
  725. showChildren: function(treeNode) {
  726. if (treeNode.isParent) {
  727. for (var idx in treeNode.children) {
  728. var node = treeNode.children[idx];
  729. $._tree.showNode(node);
  730. $.tree.showChildren(node);
  731. }
  732. }
  733. },
  734. // 更新节点状态
  735. updateNodes: function(nodeList) {
  736. $._tree.showNodes(nodeList);
  737. for (var i = 0, l = nodeList.length; i < l; i++) {
  738. var treeNode = nodeList[i];
  739. $.tree.showChildren(treeNode);
  740. $.tree.showParent(treeNode)
  741. }
  742. },
  743. // 获取当前被勾选集合
  744. getCheckedNodes: function(column) {
  745. var _column = $.common.isEmpty(column) ? "id" : column;
  746. var nodes = $._tree.getCheckedNodes(true);
  747. return $.map(nodes, function (row) {
  748. return row[_column];
  749. }).join();
  750. },
  751. // 不允许根父节点选择
  752. notAllowParents: function(_tree) {
  753. var nodes = _tree.getSelectedNodes();
  754. for (var i = 0; i < nodes.length; i++) {
  755. if (nodes[i].level == 0) {
  756. $.modal.msgError("不能选择根节点(" + nodes[i].name + ")");
  757. return false;
  758. }
  759. if (nodes[i].isParent) {
  760. $.modal.msgError("不能选择父节点(" + nodes[i].name + ")");
  761. return false;
  762. }
  763. }
  764. return true;
  765. },
  766. // 不允许最后层级节点选择
  767. notAllowLastLevel: function(_tree) {
  768. var nodes = _tree.getSelectedNodes();
  769. for (var i = 0; i < nodes.length; i++) {
  770. if (nodes[i].level == nodes.length + 1) {
  771. $.modal.msgError("不能选择最后层级节点(" + nodes[i].name + ")");
  772. return false;
  773. }
  774. }
  775. return true;
  776. },
  777. // 隐藏/显示搜索栏
  778. toggleSearch: function() {
  779. $('#search').slideToggle(200);
  780. $('#btnShow').toggle();
  781. $('#btnHide').toggle();
  782. $('#keyword').focus();
  783. },
  784. // 折叠
  785. collapse: function() {
  786. $._tree.expandAll(false);
  787. },
  788. // 展开
  789. expand: function() {
  790. $._tree.expandAll(true);
  791. }
  792. },
  793. // 通用方法封装处理
  794. common: {
  795. // 判断字符串是否为空
  796. isEmpty: function (value) {
  797. if (value == null || this.trim(value) == "") {
  798. return true;
  799. }
  800. return false;
  801. },
  802. // 判断一个字符串是否为非空串
  803. isNotEmpty: function (value) {
  804. return !$.common.isEmpty(value);
  805. },
  806. // 是否显示数据 为空默认为显示
  807. visible: function (value) {
  808. if ($.common.isEmpty(value) || value == true) {
  809. return true;
  810. }
  811. return false;
  812. },
  813. // 空格截取
  814. trim: function (value) {
  815. if (value == null) {
  816. return "";
  817. }
  818. return value.toString().replace(/(^\s*)|(\s*$)|\r|\n/g, "");
  819. },
  820. // 指定随机数返回
  821. random: function (min, max) {
  822. return Math.floor((Math.random() * max) + min);
  823. },
  824. startWith: function(value, start) {
  825. var reg = new RegExp("^" + start);
  826. return reg.test(value)
  827. },
  828. endWith: function(value, end) {
  829. var reg = new RegExp(end + "$");
  830. return reg.test(value)
  831. }
  832. }
  833. });
  834. })(jQuery);
  835. /** 消息状态码 */
  836. web_status = {
  837. SUCCESS: 0,
  838. FAIL: 500
  839. };
  840. /** 弹窗状态码 */
  841. modal_status = {
  842. SUCCESS: "success",
  843. FAIL: "error",
  844. WARNING: "warning"
  845. };