ry-ui.js 40 KB

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