ry-ui.js 40 KB

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