ry-ui.js 41 KB

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