ry-ui.js 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  1. /**
  2. * 通用js方法封装处理
  3. * Copyright (c) 2019 ruoyi
  4. */
  5. // 当前table相关信息
  6. var table = {
  7. config: {},
  8. // 当前实例配置
  9. options: {},
  10. // 设置实例配置
  11. set: function(id) {
  12. if($.common.getLength(table.config) > 1) {
  13. var tableId = $.common.isEmpty(id) ? $(event.currentTarget).parents(".bootstrap-table").find(".table").attr("id") : id;
  14. if ($.common.isNotEmpty(tableId)) {
  15. table.options = table.get(tableId);
  16. }
  17. }
  18. },
  19. // 获取实例配置
  20. get: function(id) {
  21. return table.config[id];
  22. },
  23. // 记住选择实例组
  24. rememberSelecteds: {},
  25. // 记住选择ID组
  26. rememberSelectedIds: {}
  27. };
  28. (function ($) {
  29. $.extend({
  30. _tree: {},
  31. bttTable: {},
  32. // 表格封装处理
  33. table: {
  34. // 初始化表格参数
  35. init: function(options) {
  36. var defaults = {
  37. id: "bootstrap-table",
  38. type: 0, // 0 代表bootstrapTable 1代表bootstrapTreeTable
  39. height: undefined,
  40. sidePagination: "server",
  41. sortName: "",
  42. sortOrder: "asc",
  43. pagination: true,
  44. pageSize: 10,
  45. pageList: [10, 25, 50],
  46. toolbar: "toolbar",
  47. striped: false,
  48. escape: false,
  49. firstLoad: true,
  50. showFooter: false,
  51. search: false,
  52. showSearch: true,
  53. showPageGo: false,
  54. showRefresh: true,
  55. showColumns: true,
  56. showToggle: true,
  57. showExport: false,
  58. clickToSelect: false,
  59. singleSelect: false,
  60. mobileResponsive: true,
  61. rememberSelected: false,
  62. fixedColumns: false,
  63. fixedNumber: 0,
  64. rightFixedColumns: false,
  65. rightFixedNumber: 0,
  66. queryParams: $.table.queryParams,
  67. rowStyle: {},
  68. };
  69. var options = $.extend(defaults, options);
  70. table.options = options;
  71. table.config[options.id] = options;
  72. $.table.initEvent();
  73. $('#' + options.id).bootstrapTable({
  74. id: options.id,
  75. url: options.url, // 请求后台的URL(*)
  76. contentType: "application/x-www-form-urlencoded", // 编码类型
  77. method: 'post', // 请求方式(*)
  78. cache: false, // 是否使用缓存
  79. height: options.height, // 表格的高度
  80. striped: options.striped, // 是否显示行间隔色
  81. sortable: true, // 是否启用排序
  82. sortStable: true, // 设置为 true 将获得稳定的排序
  83. sortName: options.sortName, // 排序列名称
  84. sortOrder: options.sortOrder, // 排序方式 asc 或者 desc
  85. pagination: options.pagination, // 是否显示分页(*)
  86. pageNumber: 1, // 初始化加载第一页,默认第一页
  87. pageSize: options.pageSize, // 每页的记录行数(*)
  88. pageList: options.pageList, // 可供选择的每页的行数(*)
  89. firstLoad: options.firstLoad, // 是否首次请求加载数据,对于数据较大可以配置false
  90. escape: options.escape, // 转义HTML字符串
  91. showFooter: options.showFooter, // 是否显示表尾
  92. iconSize: 'outline', // 图标大小:undefined默认的按钮尺寸 xs超小按钮sm小按钮lg大按钮
  93. toolbar: '#' + options.toolbar, // 指定工作栏
  94. sidePagination: options.sidePagination, // server启用服务端分页client客户端分页
  95. search: options.search, // 是否显示搜索框功能
  96. searchText: options.searchText, // 搜索框初始显示的内容,默认为空
  97. showSearch: options.showSearch, // 是否显示检索信息
  98. showPageGo: options.showPageGo, // 是否显示跳转页
  99. showRefresh: options.showRefresh, // 是否显示刷新按钮
  100. showColumns: options.showColumns, // 是否显示隐藏某列下拉框
  101. showToggle: options.showToggle, // 是否显示详细视图和列表视图的切换按钮
  102. showExport: options.showExport, // 是否支持导出文件
  103. uniqueId: options.uniqueId, // 唯 一的标识符
  104. clickToSelect: options.clickToSelect, // 是否启用点击选中行
  105. singleSelect: options.singleSelect, // 是否单选checkbox
  106. mobileResponsive: options.mobileResponsive, // 是否支持移动端适配
  107. detailView: options.detailView, // 是否启用显示细节视图
  108. onClickRow: options.onClickRow, // 点击某行触发的事件
  109. onDblClickRow: options.onDblClickRow, // 双击某行触发的事件
  110. onClickCell: options.onClickCell, // 单击某格触发的事件
  111. onDblClickCell: options.onDblClickCell, // 双击某格触发的事件
  112. onEditableSave: options.onEditableSave, // 行内编辑保存的事件
  113. onExpandRow: options.onExpandRow, // 点击详细视图的事件
  114. rememberSelected: options.rememberSelected, // 启用翻页记住前面的选择
  115. fixedColumns: options.fixedColumns, // 是否启用冻结列(左侧)
  116. fixedNumber: options.fixedNumber, // 列冻结的个数(左侧)
  117. rightFixedColumns: options.rightFixedColumns, // 是否启用冻结列(右侧)
  118. rightFixedNumber: options.rightFixedNumber, // 列冻结的个数(右侧)
  119. onReorderRow: options.onReorderRow, // 当拖拽结束后处理函数
  120. queryParams: options.queryParams, // 传递参数(*)
  121. rowStyle: options.rowStyle, // 通过自定义函数设置行样式
  122. columns: options.columns, // 显示列信息(*)
  123. responseHandler: $.table.responseHandler, // 在加载服务器发送来的数据之前处理函数
  124. onLoadSuccess: $.table.onLoadSuccess, // 当所有数据被加载时触发处理函数
  125. exportOptions: options.exportOptions, // 前端导出忽略列索引
  126. detailFormatter: options.detailFormatter, // 在行下面展示其他数据列表
  127. });
  128. },
  129. // 获取实例ID,如存在多个返回#id1,#id2 delimeter分隔符
  130. getOptionsIds: function(separator) {
  131. var _separator = $.common.isEmpty(separator) ? "," : separator;
  132. var optionsIds = "";
  133. $.each(table.config, function(key, value){
  134. optionsIds += "#" + key + _separator;
  135. });
  136. return optionsIds.substring(0, optionsIds.length - 1);
  137. },
  138. // 查询条件
  139. queryParams: function(params) {
  140. var curParams = {
  141. // 传递参数查询参数
  142. pageSize: params.limit,
  143. pageNum: params.offset / params.limit + 1,
  144. searchValue: params.search,
  145. orderByColumn: params.sort,
  146. isAsc: params.order
  147. };
  148. var currentId = $.common.isEmpty(table.options.formId) ? $('form').attr('id') : table.options.formId;
  149. return $.extend(curParams, $.common.formToJSON(currentId));
  150. },
  151. // 请求获取数据后处理回调函数
  152. responseHandler: function(res) {
  153. if (typeof table.options.responseHandler == "function") {
  154. table.options.responseHandler(res);
  155. }
  156. if (res.code == 0) {
  157. if ($.common.isNotEmpty(table.options.sidePagination) && table.options.sidePagination == 'client') {
  158. return res.rows;
  159. } else {
  160. if ($.common.isNotEmpty(table.options.rememberSelected) && table.options.rememberSelected) {
  161. var column = $.common.isEmpty(table.options.uniqueId) ? table.options.columns[1].field : table.options.uniqueId;
  162. $.each(res.rows, function(i, row) {
  163. row.state = $.inArray(row[column], table.rememberSelectedIds[table.options.id]) !== -1;
  164. })
  165. }
  166. return { rows: res.rows, total: res.total };
  167. }
  168. } else {
  169. $.modal.alertWarning(res.msg);
  170. return { rows: [], total: 0 };
  171. }
  172. },
  173. // 初始化事件
  174. initEvent: function() {
  175. // 实例ID信息
  176. var optionsIds = $.table.getOptionsIds();
  177. // 监听事件处理
  178. $(optionsIds).on(TABLE_EVENTS, function () {
  179. table.set($(this).attr("id"));
  180. });
  181. // 选中、取消、全部选中、全部取消(事件)
  182. $(optionsIds).on("check.bs.table check-all.bs.table uncheck.bs.table uncheck-all.bs.table", function (e, rows) {
  183. // 复选框分页保留保存选中数组
  184. var rowIds = $.table.affectedRowIds(rows);
  185. if ($.common.isNotEmpty(table.options.rememberSelected) && table.options.rememberSelected) {
  186. func = $.inArray(e.type, ['check', 'check-all']) > -1 ? 'union' : 'difference';
  187. var selectedIds = table.rememberSelectedIds[table.options.id];
  188. if($.common.isNotEmpty(selectedIds)) {
  189. table.rememberSelectedIds[table.options.id] = _[func](selectedIds, rowIds);
  190. } else {
  191. table.rememberSelectedIds[table.options.id] = _[func]([], rowIds);
  192. }
  193. var selectedRows = table.rememberSelecteds[table.options.id];
  194. if($.common.isNotEmpty(selectedRows)) {
  195. table.rememberSelecteds[table.options.id] = _[func](selectedRows, rows);
  196. } else {
  197. table.rememberSelecteds[table.options.id] = _[func]([], rows);
  198. }
  199. }
  200. });
  201. // 加载成功、选中、取消、全部选中、全部取消(事件)
  202. $(optionsIds).on("check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table load-success.bs.table", function () {
  203. var toolbar = table.options.toolbar;
  204. var uniqueId = table.options.uniqueId;
  205. // 工具栏按钮控制
  206. var rows = $.common.isEmpty(uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(uniqueId);
  207. // 非多个禁用
  208. $('#' + toolbar + ' .multiple').toggleClass('disabled', !rows.length);
  209. // 非单个禁用
  210. $('#' + toolbar + ' .single').toggleClass('disabled', rows.length!=1);
  211. });
  212. // 图片预览事件
  213. $(optionsIds).off("click").on("click", '.img-circle', function() {
  214. var src = $(this).attr('src');
  215. var target = $(this).data('target');
  216. var height = $(this).data('height');
  217. var width = $(this).data('width');
  218. if($.common.equals("self", target)) {
  219. layer.open({
  220. title: false,
  221. type: 1,
  222. closeBtn: true,
  223. shadeClose: true,
  224. area: ['auto', 'auto'],
  225. content: "<img src='" + src + "' height='" + height + "' width='" + width + "'/>"
  226. });
  227. } else if ($.common.equals("blank", target)) {
  228. window.open(src);
  229. }
  230. });
  231. // 单击tooltip事件
  232. $(optionsIds).on("click", '.tooltip-show', function() {
  233. var target = $(this).data('target');
  234. var input = $(this).prev();
  235. if ($.common.equals("copy", target)) {
  236. input.select();
  237. document.execCommand("copy");
  238. } else if ($.common.equals("open", target)) {
  239. parent.layer.alert(input.val(), {
  240. title: "信息内容",
  241. shadeClose: true,
  242. btn: ['确认'],
  243. btnclass: ['btn btn-primary'],
  244. });
  245. }
  246. });
  247. },
  248. // 当所有数据被加载时触发
  249. onLoadSuccess: function(data) {
  250. if (typeof table.options.onLoadSuccess == "function") {
  251. table.options.onLoadSuccess(data);
  252. }
  253. // 浮动提示框特效
  254. $(".table [data-toggle='tooltip']").tooltip();
  255. // 气泡弹出框特效
  256. $('.table [data-toggle="popover"]').each(function() {
  257. $(this).popover({ trigger: "manual", html: true, animation: false, container: "body", placement: "left"
  258. }).on("mouseenter",
  259. function() {
  260. var _this = this;
  261. $(this).popover("show");
  262. $(".popover").on("mouseleave", function() {
  263. $(_this).popover('hide');
  264. });
  265. }).on("mouseleave",
  266. function() {
  267. var _this = this;
  268. setTimeout(function() {
  269. if (!$(".popover:hover").length)
  270. $(_this).popover("hide");
  271. }, 100);
  272. });
  273. });
  274. },
  275. // 表格销毁
  276. destroy: function (tableId) {
  277. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  278. $("#" + currentId).bootstrapTable('destroy');
  279. },
  280. // 序列号生成
  281. serialNumber: function (index, tableId) {
  282. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  283. var tableParams = $("#" + currentId).bootstrapTable('getOptions');
  284. var pageSize = tableParams.pageSize;
  285. var pageNumber = tableParams.pageNumber;
  286. return pageSize * (pageNumber - 1) + index + 1;
  287. },
  288. // 列超出指定长度浮动提示 target(copy单击复制文本 open弹窗打开文本)
  289. tooltip: function (value, length, target) {
  290. var _length = $.common.isEmpty(length) ? 20 : length;
  291. var _text = "";
  292. var _value = $.common.nullToStr(value);
  293. var _target = $.common.isEmpty(target) ? 'copy' : target;
  294. if (_value.length > _length) {
  295. _text = _value.substr(0, _length) + "...";
  296. _value = _value.replace(/\'/g,"&apos;");
  297. _value = _value.replace(/\"/g,"&quot;");
  298. var actions = [];
  299. actions.push($.common.sprintf('<input id="tooltip-show" style="opacity: 0;position: absolute;z-index:-1" type="text" value="%s"/>', _value));
  300. actions.push($.common.sprintf('<a href="###" class="tooltip-show" data-toggle="tooltip" data-target="%s" title="%s">%s</a>', _target, _value, _text));
  301. return actions.join('');
  302. } else {
  303. _text = _value;
  304. return _text;
  305. }
  306. },
  307. // 下拉按钮切换
  308. dropdownToggle: function (value) {
  309. var actions = [];
  310. actions.push('<div class="btn-group">');
  311. actions.push('<button type="button" class="btn btn-xs dropdown-toggle" data-toggle="dropdown" aria-expanded="false">');
  312. actions.push('<i class="fa fa-cog"></i>&nbsp;<span class="fa fa-chevron-down"></span></button>');
  313. actions.push('<ul class="dropdown-menu">');
  314. actions.push(value.replace(/<a/g,"<li><a").replace(/<\/a>/g,"</a></li>"));
  315. actions.push('</ul>');
  316. actions.push('</div>');
  317. return actions.join('');
  318. },
  319. // 图片预览
  320. imageView: function (value, height, width, target) {
  321. if ($.common.isEmpty(width)) {
  322. width = 'auto';
  323. }
  324. if ($.common.isEmpty(height)) {
  325. height = 'auto';
  326. }
  327. // blank or self
  328. var _target = $.common.isEmpty(target) ? 'self' : target;
  329. if ($.common.isNotEmpty(value)) {
  330. return $.common.sprintf("<img class='img-circle img-xs' data-height='%s' data-width='%s' data-target='%s' src='%s'/>", height, width, _target, value);
  331. } else {
  332. return $.common.nullToStr(value);
  333. }
  334. },
  335. // 搜索-默认第一个form
  336. search: function(formId, tableId, data) {
  337. table.set(tableId);
  338. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  339. var params = $.common.isEmpty(tableId) ? $("#" + table.options.id).bootstrapTable('getOptions') : $("#" + tableId).bootstrapTable('getOptions');
  340. params.queryParams = function(params) {
  341. var search = $.common.formToJSON(currentId);
  342. if($.common.isNotEmpty(data)){
  343. $.each(data, function(key) {
  344. search[key] = data[key];
  345. });
  346. }
  347. search.pageSize = params.limit;
  348. search.pageNum = params.offset / params.limit + 1;
  349. search.searchValue = params.search;
  350. search.orderByColumn = params.sort;
  351. search.isAsc = params.order;
  352. return search;
  353. }
  354. if($.common.isNotEmpty(tableId)){
  355. $("#" + tableId).bootstrapTable('refresh', params);
  356. } else{
  357. $("#" + table.options.id).bootstrapTable('refresh', params);
  358. }
  359. data = {};
  360. },
  361. // 导出数据
  362. exportExcel: function(formId) {
  363. table.set();
  364. $.modal.confirm("确定导出所有" + table.options.modalName + "吗?", function() {
  365. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  366. var params = $("#" + table.options.id).bootstrapTable('getOptions');
  367. var dataParam = $("#" + currentId).serializeArray();
  368. dataParam.push({ "name": "orderByColumn", "value": params.sortName });
  369. dataParam.push({ "name": "isAsc", "value": params.sortOrder });
  370. $.modal.loading("正在导出数据,请稍后...");
  371. $.post(table.options.exportUrl, dataParam, function(result) {
  372. if (result.code == web_status.SUCCESS) {
  373. window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
  374. } else if (result.code == web_status.WARNING) {
  375. $.modal.alertWarning(result.msg)
  376. } else {
  377. $.modal.alertError(result.msg);
  378. }
  379. $.modal.closeLoading();
  380. });
  381. });
  382. },
  383. // 下载模板
  384. importTemplate: function() {
  385. table.set();
  386. $.get(table.options.importTemplateUrl, function(result) {
  387. if (result.code == web_status.SUCCESS) {
  388. window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
  389. } else if (result.code == web_status.WARNING) {
  390. $.modal.alertWarning(result.msg)
  391. } else {
  392. $.modal.alertError(result.msg);
  393. }
  394. });
  395. },
  396. // 导入数据
  397. importExcel: function(formId) {
  398. table.set();
  399. var currentId = $.common.isEmpty(formId) ? 'importTpl' : formId;
  400. layer.open({
  401. type: 1,
  402. area: ['400px', '230px'],
  403. fix: false,
  404. //不固定
  405. maxmin: true,
  406. shade: 0.3,
  407. title: '导入' + table.options.modalName + '数据',
  408. content: $('#' + currentId).html(),
  409. btn: ['<i class="fa fa-check"></i> 导入', '<i class="fa fa-remove"></i> 取消'],
  410. // 弹层外区域关闭
  411. shadeClose: true,
  412. btn1: function(index, layero){
  413. var file = layero.find('#file').val();
  414. if (file == '' || (!$.common.endWith(file, '.xls') && !$.common.endWith(file, '.xlsx'))){
  415. $.modal.msgWarning("请选择后缀为 “xls”或“xlsx”的文件。");
  416. return false;
  417. }
  418. var index = layer.load(2, {shade: false});
  419. $.modal.disable();
  420. var formData = new FormData(layero.find('form')[0]);
  421. $.ajax({
  422. url: table.options.importUrl,
  423. data: formData,
  424. cache: false,
  425. contentType: false,
  426. processData: false,
  427. type: 'POST',
  428. success: function (result) {
  429. if (result.code == web_status.SUCCESS) {
  430. $.modal.closeAll();
  431. $.modal.alertSuccess(result.msg);
  432. $.table.refresh();
  433. } else if (result.code == web_status.WARNING) {
  434. layer.close(index);
  435. $.modal.enable();
  436. $.modal.alertWarning(result.msg)
  437. } else {
  438. layer.close(index);
  439. $.modal.enable();
  440. $.modal.alertError(result.msg);
  441. }
  442. }
  443. });
  444. }
  445. });
  446. },
  447. // 刷新表格
  448. refresh: function(tableId) {
  449. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  450. $("#" + currentId).bootstrapTable('refresh', {
  451. silent: true
  452. });
  453. },
  454. // 查询表格指定列值
  455. selectColumns: function(column) {
  456. var rows = $.map($("#" + table.options.id).bootstrapTable('getSelections'), function (row) {
  457. return row[column];
  458. });
  459. if ($.common.isNotEmpty(table.options.rememberSelected) && table.options.rememberSelected) {
  460. var selectedRows = table.rememberSelecteds[table.options.id];
  461. if($.common.isNotEmpty(selectedRows)) {
  462. rows = $.map(table.rememberSelecteds[table.options.id], function (row) {
  463. return row[column];
  464. });
  465. }
  466. }
  467. return $.common.uniqueFn(rows);
  468. },
  469. // 获取当前页选中或者取消的行ID
  470. affectedRowIds: function(rows) {
  471. var column = $.common.isEmpty(table.options.uniqueId) ? table.options.columns[1].field : table.options.uniqueId;
  472. var rowIds;
  473. if ($.isArray(rows)) {
  474. rowIds = $.map(rows, function(row) {
  475. return row[column];
  476. });
  477. } else {
  478. rowIds = [rows[column]];
  479. }
  480. return rowIds;
  481. },
  482. // 查询表格首列值
  483. selectFirstColumns: function() {
  484. var rows = $.map($("#" + table.options.id).bootstrapTable('getSelections'), function (row) {
  485. return row[table.options.columns[1].field];
  486. });
  487. if ($.common.isNotEmpty(table.options.rememberSelected) && table.options.rememberSelected) {
  488. var selectedRows = table.rememberSelecteds[table.options.id];
  489. if($.common.isNotEmpty(selectedRows)) {
  490. rows = $.map(selectedRows, function (row) {
  491. return row[table.options.columns[1].field];
  492. });
  493. }
  494. }
  495. return $.common.uniqueFn(rows);
  496. },
  497. // 回显数据字典
  498. selectDictLabel: function(datas, value) {
  499. var actions = [];
  500. $.each(datas, function(index, dict) {
  501. if (dict.dictValue == ('' + value)) {
  502. var listClass = $.common.equals("default", dict.listClass) || $.common.isEmpty(dict.listClass) ? "" : "badge badge-" + dict.listClass;
  503. actions.push($.common.sprintf("<span class='%s'>%s</span>", listClass, dict.dictLabel));
  504. return false;
  505. }
  506. });
  507. return actions.join('');
  508. },
  509. // 显示表格指定列
  510. showColumn: function(column, tableId) {
  511. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  512. $("#" + currentId).bootstrapTable('showColumn', column);
  513. },
  514. // 隐藏表格指定列
  515. hideColumn: function(column, tableId) {
  516. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  517. $("#" + currentId).bootstrapTable('hideColumn', column);
  518. }
  519. },
  520. // 表格树封装处理
  521. treeTable: {
  522. // 初始化表格
  523. init: function(options) {
  524. var defaults = {
  525. id: "bootstrap-tree-table",
  526. type: 1, // 0 代表bootstrapTable 1代表bootstrapTreeTable
  527. height: 0,
  528. rootIdValue: null,
  529. ajaxParams: {},
  530. toolbar: "toolbar",
  531. striped: false,
  532. expandColumn: 1,
  533. showSearch: true,
  534. showRefresh: true,
  535. showColumns: true,
  536. expandAll: true,
  537. expandFirst: true
  538. };
  539. var options = $.extend(defaults, options);
  540. table.options = options;
  541. table.config[options.id] = options;
  542. $.bttTable = $('#' + options.id).bootstrapTreeTable({
  543. code: options.code, // 用于设置父子关系
  544. parentCode: options.parentCode, // 用于设置父子关系
  545. type: 'post', // 请求方式(*)
  546. url: options.url, // 请求后台的URL(*)
  547. data: options.data, // 无url时用于渲染的数据
  548. ajaxParams: options.ajaxParams, // 请求数据的ajax的data属性
  549. rootIdValue: options.rootIdValue, // 设置指定根节点id值
  550. height: options.height, // 表格树的高度
  551. expandColumn: options.expandColumn, // 在哪一列上面显示展开按钮
  552. striped: options.striped, // 是否显示行间隔色
  553. bordered: false, // 是否显示边框
  554. toolbar: '#' + options.toolbar, // 指定工作栏
  555. showSearch: options.showSearch, // 是否显示检索信息
  556. showRefresh: options.showRefresh, // 是否显示刷新按钮
  557. showColumns: options.showColumns, // 是否显示隐藏某列下拉框
  558. expandAll: options.expandAll, // 是否全部展开
  559. expandFirst: options.expandFirst, // 是否默认第一级展开--expandAll为false时生效
  560. columns: options.columns, // 显示列信息(*)
  561. responseHandler: $.treeTable.responseHandler // 当所有数据被加载时触发处理函数
  562. });
  563. },
  564. // 条件查询
  565. search: function(formId) {
  566. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  567. var params = $.common.formToJSON(currentId);
  568. $.bttTable.bootstrapTreeTable('refresh', params);
  569. },
  570. // 刷新
  571. refresh: function() {
  572. $.bttTable.bootstrapTreeTable('refresh');
  573. },
  574. // 查询表格树指定列值
  575. selectColumns: function(column) {
  576. var rows = $.map($.bttTable.bootstrapTreeTable('getSelections'), function (row) {
  577. return row[column];
  578. });
  579. return $.common.uniqueFn(rows);
  580. },
  581. // 请求获取数据后处理回调函数,校验异常状态提醒
  582. responseHandler: function(res) {
  583. if (typeof table.options.responseHandler == "function") {
  584. table.options.responseHandler(res);
  585. }
  586. if (res.code != undefined && res.code != 0) {
  587. $.modal.alertWarning(res.msg);
  588. return [];
  589. } else {
  590. return res;
  591. }
  592. },
  593. },
  594. // 表单封装处理
  595. form: {
  596. // 表单重置
  597. reset: function(formId, tableId) {
  598. table.set(tableId);
  599. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  600. $("#" + currentId)[0].reset();
  601. if (table.options.type == table_type.bootstrapTable) {
  602. if($.common.isEmpty(tableId)){
  603. $("#" + table.options.id).bootstrapTable('refresh');
  604. } else{
  605. $("#" + tableId).bootstrapTable('refresh');
  606. }
  607. } else if (table.options.type == table_type.bootstrapTreeTable) {
  608. if($.common.isEmpty(tableId)){
  609. $("#" + table.options.id).bootstrapTreeTable('refresh', []);
  610. } else{
  611. $("#" + tableId).bootstrapTreeTable('refresh', []);
  612. }
  613. }
  614. },
  615. // 获取选中复选框项
  616. selectCheckeds: function(name) {
  617. var checkeds = "";
  618. $('input:checkbox[name="' + name + '"]:checked').each(function(i) {
  619. if (0 == i) {
  620. checkeds = $(this).val();
  621. } else {
  622. checkeds += ("," + $(this).val());
  623. }
  624. });
  625. return checkeds;
  626. },
  627. // 获取选中下拉框项
  628. selectSelects: function(name) {
  629. var selects = "";
  630. $('#' + name + ' option:selected').each(function (i) {
  631. if (0 == i) {
  632. selects = $(this).val();
  633. } else {
  634. selects += ("," + $(this).val());
  635. }
  636. });
  637. return selects;
  638. }
  639. },
  640. // 弹出层封装处理
  641. modal: {
  642. // 显示图标
  643. icon: function(type) {
  644. var icon = "";
  645. if (type == modal_status.WARNING) {
  646. icon = 0;
  647. } else if (type == modal_status.SUCCESS) {
  648. icon = 1;
  649. } else if (type == modal_status.FAIL) {
  650. icon = 2;
  651. } else {
  652. icon = 3;
  653. }
  654. return icon;
  655. },
  656. // 消息提示
  657. msg: function(content, type) {
  658. if (type != undefined) {
  659. layer.msg(content, { icon: $.modal.icon(type), time: 1000, shift: 5 });
  660. } else {
  661. layer.msg(content);
  662. }
  663. },
  664. // 错误消息
  665. msgError: function(content) {
  666. $.modal.msg(content, modal_status.FAIL);
  667. },
  668. // 成功消息
  669. msgSuccess: function(content) {
  670. $.modal.msg(content, modal_status.SUCCESS);
  671. },
  672. // 警告消息
  673. msgWarning: function(content) {
  674. $.modal.msg(content, modal_status.WARNING);
  675. },
  676. // 弹出提示
  677. alert: function(content, type) {
  678. layer.alert(content, {
  679. icon: $.modal.icon(type),
  680. title: "系统提示",
  681. btn: ['确认'],
  682. btnclass: ['btn btn-primary'],
  683. });
  684. },
  685. // 消息提示并刷新父窗体
  686. msgReload: function(msg, type) {
  687. layer.msg(msg, {
  688. icon: $.modal.icon(type),
  689. time: 500,
  690. shade: [0.1, '#8F8F8F']
  691. },
  692. function() {
  693. $.modal.reload();
  694. });
  695. },
  696. // 错误提示
  697. alertError: function(content) {
  698. $.modal.alert(content, modal_status.FAIL);
  699. },
  700. // 成功提示
  701. alertSuccess: function(content) {
  702. $.modal.alert(content, modal_status.SUCCESS);
  703. },
  704. // 警告提示
  705. alertWarning: function(content) {
  706. $.modal.alert(content, modal_status.WARNING);
  707. },
  708. // 关闭窗体
  709. close: function () {
  710. var index = parent.layer.getFrameIndex(window.name);
  711. parent.layer.close(index);
  712. },
  713. // 关闭全部窗体
  714. closeAll: function () {
  715. layer.closeAll();
  716. },
  717. // 确认窗体
  718. confirm: function (content, callBack) {
  719. layer.confirm(content, {
  720. icon: 3,
  721. title: "系统提示",
  722. btn: ['确认', '取消']
  723. }, function (index) {
  724. layer.close(index);
  725. callBack(true);
  726. });
  727. },
  728. // 弹出层指定宽度
  729. open: function (title, url, width, height, callback) {
  730. //如果是移动端,就使用自适应大小弹窗
  731. if ($.common.isMobile()) {
  732. width = 'auto';
  733. height = 'auto';
  734. }
  735. if ($.common.isEmpty(title)) {
  736. title = false;
  737. }
  738. if ($.common.isEmpty(url)) {
  739. url = "/404.html";
  740. }
  741. if ($.common.isEmpty(width)) {
  742. width = 800;
  743. }
  744. if ($.common.isEmpty(height)) {
  745. height = ($(window).height() - 50);
  746. }
  747. if ($.common.isEmpty(callback)) {
  748. callback = function(index, layero) {
  749. var iframeWin = layero.find('iframe')[0];
  750. iframeWin.contentWindow.submitHandler(index, layero);
  751. }
  752. }
  753. layer.open({
  754. type: 2,
  755. area: [width + 'px', height + 'px'],
  756. fix: false,
  757. //不固定
  758. maxmin: true,
  759. shade: 0.3,
  760. title: title,
  761. content: url,
  762. btn: ['确定', '关闭'],
  763. // 弹层外区域关闭
  764. shadeClose: true,
  765. yes: callback,
  766. cancel: function(index) {
  767. return true;
  768. }
  769. });
  770. },
  771. // 弹出层指定参数选项
  772. openOptions: function (options) {
  773. var _url = $.common.isEmpty(options.url) ? "/404.html" : options.url;
  774. var _title = $.common.isEmpty(options.title) ? "系统窗口" : options.title;
  775. var _width = $.common.isEmpty(options.width) ? "800" : options.width;
  776. var _height = $.common.isEmpty(options.height) ? ($(window).height() - 50) : options.height;
  777. var _btn = ['<i class="fa fa-check"></i> 确认', '<i class="fa fa-close"></i> 关闭'];
  778. if ($.common.isEmpty(options.yes)) {
  779. options.yes = function(index, layero) {
  780. options.callBack(index, layero);
  781. }
  782. }
  783. layer.open({
  784. type: 2,
  785. maxmin: true,
  786. shade: 0.3,
  787. title: _title,
  788. fix: false,
  789. area: [_width + 'px', _height + 'px'],
  790. content: _url,
  791. shadeClose: $.common.isEmpty(options.shadeClose) ? true : options.shadeClose,
  792. skin: options.skin,
  793. btn: $.common.isEmpty(options.btn) ? _btn : options.btn,
  794. yes: options.yes,
  795. cancel: function () {
  796. return true;
  797. }
  798. });
  799. },
  800. // 弹出层全屏
  801. openFull: function (title, url, width, height) {
  802. //如果是移动端,就使用自适应大小弹窗
  803. if ($.common.isMobile()) {
  804. width = 'auto';
  805. height = 'auto';
  806. }
  807. if ($.common.isEmpty(title)) {
  808. title = false;
  809. }
  810. if ($.common.isEmpty(url)) {
  811. url = "/404.html";
  812. }
  813. if ($.common.isEmpty(width)) {
  814. width = 800;
  815. }
  816. if ($.common.isEmpty(height)) {
  817. height = ($(window).height() - 50);
  818. }
  819. var index = layer.open({
  820. type: 2,
  821. area: [width + 'px', height + 'px'],
  822. fix: false,
  823. //不固定
  824. maxmin: true,
  825. shade: 0.3,
  826. title: title,
  827. content: url,
  828. btn: ['确定', '关闭'],
  829. // 弹层外区域关闭
  830. shadeClose: true,
  831. yes: function(index, layero) {
  832. var iframeWin = layero.find('iframe')[0];
  833. iframeWin.contentWindow.submitHandler(index, layero);
  834. },
  835. cancel: function(index) {
  836. return true;
  837. }
  838. });
  839. layer.full(index);
  840. },
  841. // 选卡页方式打开
  842. openTab: function (title, url) {
  843. createMenuItem(url, title);
  844. },
  845. // 选卡页同一页签打开
  846. parentTab: function (title, url) {
  847. var dataId = window.frameElement.getAttribute('data-id');
  848. createMenuItem(url, title);
  849. closeItem(dataId);
  850. },
  851. // 关闭选项卡
  852. closeTab: function (dataId) {
  853. closeItem(dataId);
  854. },
  855. // 禁用按钮
  856. disable: function() {
  857. var doc = window.top == window.parent ? window.document : window.parent.document;
  858. $("a[class*=layui-layer-btn]", doc).addClass("layer-disabled");
  859. },
  860. // 启用按钮
  861. enable: function() {
  862. var doc = window.top == window.parent ? window.document : window.parent.document;
  863. $("a[class*=layui-layer-btn]", doc).removeClass("layer-disabled");
  864. },
  865. // 打开遮罩层
  866. loading: function (message) {
  867. $.blockUI({ message: '<div class="loaderbox"><div class="loading-activity"></div> ' + message + '</div>' });
  868. },
  869. // 关闭遮罩层
  870. closeLoading: function () {
  871. setTimeout(function(){
  872. $.unblockUI();
  873. }, 50);
  874. },
  875. // 重新加载
  876. reload: function () {
  877. parent.location.reload();
  878. }
  879. },
  880. // 操作封装处理
  881. operate: {
  882. // 提交数据
  883. submit: function(url, type, dataType, data, callback) {
  884. var config = {
  885. url: url,
  886. type: type,
  887. dataType: dataType,
  888. data: data,
  889. beforeSend: function () {
  890. $.modal.loading("正在处理中,请稍后...");
  891. },
  892. success: function(result) {
  893. if (typeof callback == "function") {
  894. callback(result);
  895. }
  896. $.operate.ajaxSuccess(result);
  897. }
  898. };
  899. $.ajax(config)
  900. },
  901. // post请求传输
  902. post: function(url, data, callback) {
  903. $.operate.submit(url, "post", "json", data, callback);
  904. },
  905. // get请求传输
  906. get: function(url, callback) {
  907. $.operate.submit(url, "get", "json", "", callback);
  908. },
  909. // 详细信息
  910. detail: function(id, width, height) {
  911. table.set();
  912. var _url = $.operate.detailUrl(id);
  913. var _width = $.common.isEmpty(width) ? "800" : width;
  914. var _height = $.common.isEmpty(height) ? ($(window).height() - 50) : height;
  915. //如果是移动端,就使用自适应大小弹窗
  916. if ($.common.isMobile()) {
  917. _width = 'auto';
  918. _height = 'auto';
  919. }
  920. var options = {
  921. title: table.options.modalName + "详细",
  922. width: _width,
  923. height: _height,
  924. url: _url,
  925. skin: 'layui-layer-gray',
  926. btn: ['关闭'],
  927. yes: function (index, layero) {
  928. layer.close(index);
  929. }
  930. };
  931. $.modal.openOptions(options);
  932. },
  933. // 详细访问地址
  934. detailUrl: function(id) {
  935. var url = "/404.html";
  936. if ($.common.isNotEmpty(id)) {
  937. url = table.options.detailUrl.replace("{id}", id);
  938. } else {
  939. var id = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  940. if (id.length == 0) {
  941. $.modal.alertWarning("请至少选择一条记录");
  942. return;
  943. }
  944. url = table.options.detailUrl.replace("{id}", id);
  945. }
  946. return url;
  947. },
  948. // 删除信息
  949. remove: function(id) {
  950. table.set();
  951. $.modal.confirm("确定删除该条" + table.options.modalName + "信息吗?", function() {
  952. var url = $.common.isEmpty(id) ? table.options.removeUrl : table.options.removeUrl.replace("{id}", id);
  953. if(table.options.type == table_type.bootstrapTreeTable) {
  954. $.operate.get(url);
  955. } else {
  956. var data = { "ids": id };
  957. $.operate.submit(url, "post", "json", data);
  958. }
  959. });
  960. },
  961. // 批量删除信息
  962. removeAll: function() {
  963. table.set();
  964. var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  965. if (rows.length == 0) {
  966. $.modal.alertWarning("请至少选择一条记录");
  967. return;
  968. }
  969. $.modal.confirm("确认要删除选中的" + rows.length + "条数据吗?", function() {
  970. var url = table.options.removeUrl;
  971. var data = { "ids": rows.join() };
  972. $.operate.submit(url, "post", "json", data);
  973. });
  974. },
  975. // 清空信息
  976. clean: function() {
  977. table.set();
  978. $.modal.confirm("确定清空所有" + table.options.modalName + "吗?", function() {
  979. var url = table.options.cleanUrl;
  980. $.operate.submit(url, "post", "json", "");
  981. });
  982. },
  983. // 添加信息
  984. add: function(id) {
  985. table.set();
  986. $.modal.open("添加" + table.options.modalName, $.operate.addUrl(id));
  987. },
  988. // 添加信息,以tab页展现
  989. addTab: function (id) {
  990. table.set();
  991. $.modal.openTab("添加" + table.options.modalName, $.operate.addUrl(id));
  992. },
  993. // 添加信息 全屏
  994. addFull: function(id) {
  995. table.set();
  996. var url = $.common.isEmpty(id) ? table.options.createUrl : table.options.createUrl.replace("{id}", id);
  997. $.modal.openFull("添加" + table.options.modalName, url);
  998. },
  999. // 添加访问地址
  1000. addUrl: function(id) {
  1001. var url = $.common.isEmpty(id) ? table.options.createUrl.replace("{id}", "") : table.options.createUrl.replace("{id}", id);
  1002. return url;
  1003. },
  1004. // 修改信息
  1005. edit: function(id) {
  1006. table.set();
  1007. if($.common.isEmpty(id) && table.options.type == table_type.bootstrapTreeTable) {
  1008. var row = $("#" + table.options.id).bootstrapTreeTable('getSelections')[0];
  1009. if ($.common.isEmpty(row)) {
  1010. $.modal.alertWarning("请至少选择一条记录");
  1011. return;
  1012. }
  1013. var url = table.options.updateUrl.replace("{id}", row[table.options.uniqueId]);
  1014. $.modal.open("修改" + table.options.modalName, url);
  1015. } else {
  1016. $.modal.open("修改" + table.options.modalName, $.operate.editUrl(id));
  1017. }
  1018. },
  1019. // 修改信息,以tab页展现
  1020. editTab: function(id) {
  1021. table.set();
  1022. $.modal.openTab("修改" + table.options.modalName, $.operate.editUrl(id));
  1023. },
  1024. // 修改信息 全屏
  1025. editFull: function(id) {
  1026. table.set();
  1027. var url = "/404.html";
  1028. if ($.common.isNotEmpty(id)) {
  1029. url = table.options.updateUrl.replace("{id}", id);
  1030. } else {
  1031. var row = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  1032. url = table.options.updateUrl.replace("{id}", row);
  1033. }
  1034. $.modal.openFull("修改" + table.options.modalName, url);
  1035. },
  1036. // 修改访问地址
  1037. editUrl: function(id) {
  1038. var url = "/404.html";
  1039. if ($.common.isNotEmpty(id)) {
  1040. url = table.options.updateUrl.replace("{id}", id);
  1041. } else {
  1042. var id = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  1043. if (id.length == 0) {
  1044. $.modal.alertWarning("请至少选择一条记录");
  1045. return;
  1046. }
  1047. url = table.options.updateUrl.replace("{id}", id);
  1048. }
  1049. return url;
  1050. },
  1051. // 保存信息 刷新表格
  1052. save: function(url, data, callback) {
  1053. var config = {
  1054. url: url,
  1055. type: "post",
  1056. dataType: "json",
  1057. data: data,
  1058. beforeSend: function () {
  1059. $.modal.loading("正在处理中,请稍后...");
  1060. $.modal.disable();
  1061. },
  1062. success: function(result) {
  1063. if (typeof callback == "function") {
  1064. callback(result);
  1065. }
  1066. $.operate.successCallback(result);
  1067. }
  1068. };
  1069. $.ajax(config)
  1070. },
  1071. // 保存信息 弹出提示框
  1072. saveModal: function(url, data, callback) {
  1073. var config = {
  1074. url: url,
  1075. type: "post",
  1076. dataType: "json",
  1077. data: data,
  1078. beforeSend: function () {
  1079. $.modal.loading("正在处理中,请稍后...");
  1080. },
  1081. success: function(result) {
  1082. if (typeof callback == "function") {
  1083. callback(result);
  1084. }
  1085. if (result.code == web_status.SUCCESS) {
  1086. $.modal.alertSuccess(result.msg)
  1087. } else if (result.code == web_status.WARNING) {
  1088. $.modal.alertWarning(result.msg)
  1089. } else {
  1090. $.modal.alertError(result.msg);
  1091. }
  1092. $.modal.closeLoading();
  1093. }
  1094. };
  1095. $.ajax(config)
  1096. },
  1097. // 保存选项卡信息
  1098. saveTab: function(url, data, callback) {
  1099. var config = {
  1100. url: url,
  1101. type: "post",
  1102. dataType: "json",
  1103. data: data,
  1104. beforeSend: function () {
  1105. $.modal.loading("正在处理中,请稍后...");
  1106. },
  1107. success: function(result) {
  1108. if (typeof callback == "function") {
  1109. callback(result);
  1110. }
  1111. $.operate.successTabCallback(result);
  1112. }
  1113. };
  1114. $.ajax(config)
  1115. },
  1116. // 保存结果弹出msg刷新table表格
  1117. ajaxSuccess: function (result) {
  1118. if (result.code == web_status.SUCCESS && table.options.type == table_type.bootstrapTable) {
  1119. $.modal.msgSuccess(result.msg);
  1120. $.table.refresh();
  1121. } else if (result.code == web_status.SUCCESS && table.options.type == table_type.bootstrapTreeTable) {
  1122. $.modal.msgSuccess(result.msg);
  1123. $.treeTable.refresh();
  1124. } else if (result.code == web_status.SUCCESS && table.option.type == undefined) {
  1125. $.modal.msgSuccess(result.msg)
  1126. } else if (result.code == web_status.WARNING) {
  1127. $.modal.alertWarning(result.msg)
  1128. } else {
  1129. $.modal.alertError(result.msg);
  1130. }
  1131. $.modal.closeLoading();
  1132. },
  1133. // 成功结果提示msg(父窗体全局更新)
  1134. saveSuccess: function (result) {
  1135. if (result.code == web_status.SUCCESS) {
  1136. $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
  1137. } else if (result.code == web_status.WARNING) {
  1138. $.modal.alertWarning(result.msg)
  1139. } else {
  1140. $.modal.alertError(result.msg);
  1141. }
  1142. $.modal.closeLoading();
  1143. },
  1144. // 成功回调执行事件(父窗体静默更新)
  1145. successCallback: function(result) {
  1146. if (result.code == web_status.SUCCESS) {
  1147. var parent = window.parent;
  1148. if (parent.table.options.type == table_type.bootstrapTable) {
  1149. $.modal.close();
  1150. parent.$.modal.msgSuccess(result.msg);
  1151. parent.$.table.refresh();
  1152. } else if (parent.table.options.type == table_type.bootstrapTreeTable) {
  1153. $.modal.close();
  1154. parent.$.modal.msgSuccess(result.msg);
  1155. parent.$.treeTable.refresh();
  1156. } else {
  1157. $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
  1158. }
  1159. } else if (result.code == web_status.WARNING) {
  1160. $.modal.alertWarning(result.msg)
  1161. } else {
  1162. $.modal.alertError(result.msg);
  1163. }
  1164. $.modal.closeLoading();
  1165. $.modal.enable();
  1166. },
  1167. // 选项卡成功回调执行事件(父窗体静默更新)
  1168. successTabCallback: function(result) {
  1169. if (result.code == web_status.SUCCESS) {
  1170. var topWindow = $(window.parent.document);
  1171. var currentId = $('.page-tabs-content', topWindow).find('.active').attr('data-panel');
  1172. var $contentWindow = $('.RuoYi_iframe[data-id="' + currentId + '"]', topWindow)[0].contentWindow;
  1173. $.modal.close();
  1174. $contentWindow.$.modal.msgSuccess(result.msg);
  1175. $contentWindow.$(".layui-layer-padding").removeAttr("style");
  1176. if ($contentWindow.table.options.type == table_type.bootstrapTable) {
  1177. $contentWindow.$.table.refresh();
  1178. } else if ($contentWindow.table.options.type == table_type.bootstrapTreeTable) {
  1179. $contentWindow.$.treeTable.refresh();
  1180. }
  1181. $.modal.closeTab();
  1182. } else if (result.code == web_status.WARNING) {
  1183. $.modal.alertWarning(result.msg)
  1184. } else {
  1185. $.modal.alertError(result.msg);
  1186. }
  1187. $.modal.closeLoading();
  1188. }
  1189. },
  1190. // 校验封装处理
  1191. validate: {
  1192. // 判断返回标识是否唯一 false 不存在 true 存在
  1193. unique: function (value) {
  1194. if (value == "0") {
  1195. return true;
  1196. }
  1197. return false;
  1198. },
  1199. // 表单验证
  1200. form: function (formId) {
  1201. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  1202. return $("#" + currentId).validate().form();
  1203. },
  1204. // 重置表单验证(清除提示信息)
  1205. reset: function (formId) {
  1206. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  1207. return $("#" + currentId).validate().resetForm();
  1208. }
  1209. },
  1210. // 树插件封装处理
  1211. tree: {
  1212. _option: {},
  1213. _lastValue: {},
  1214. // 初始化树结构
  1215. init: function(options) {
  1216. var defaults = {
  1217. id: "tree", // 属性ID
  1218. expandLevel: 0, // 展开等级节点
  1219. view: {
  1220. selectedMulti: false, // 设置是否允许同时选中多个节点
  1221. nameIsHTML: true // 设置 name 属性是否支持 HTML 脚本
  1222. },
  1223. check: {
  1224. enable: false, // 置 zTree 的节点上是否显示 checkbox / radio
  1225. nocheckInherit: true, // 设置子节点是否自动继承
  1226. },
  1227. data: {
  1228. key: {
  1229. title: "title" // 节点数据保存节点提示信息的属性名称
  1230. },
  1231. simpleData: {
  1232. enable: true // true / false 分别表示 使用 / 不使用 简单数据模式
  1233. }
  1234. },
  1235. };
  1236. var options = $.extend(defaults, options);
  1237. $.tree._option = options;
  1238. // 树结构初始化加载
  1239. var setting = {
  1240. callback: {
  1241. onClick: options.onClick, // 用于捕获节点被点击的事件回调函数
  1242. onCheck: options.onCheck, // 用于捕获 checkbox / radio 被勾选 或 取消勾选的事件回调函数
  1243. onDblClick: options.onDblClick // 用于捕获鼠标双击之后的事件回调函数
  1244. },
  1245. check: options.check,
  1246. view: options.view,
  1247. data: options.data
  1248. };
  1249. $.get(options.url, function(data) {
  1250. var treeId = $("#treeId").val();
  1251. tree = $.fn.zTree.init($("#" + options.id), setting, data);
  1252. $._tree = tree;
  1253. var nodes = tree.getNodesByParam("level", options.expandLevel - 1);
  1254. for (var i = 0; i < nodes.length; i++) {
  1255. tree.expandNode(nodes[i], true, false, false);
  1256. }
  1257. var node = tree.getNodesByParam("id", treeId, null)[0];
  1258. $.tree.selectByIdName(treeId, node);
  1259. });
  1260. },
  1261. // 搜索节点
  1262. searchNode: function() {
  1263. // 取得输入的关键字的值
  1264. var value = $.common.trim($("#keyword").val());
  1265. if ($.tree._lastValue == value) {
  1266. return;
  1267. }
  1268. // 保存最后一次搜索名称
  1269. $.tree._lastValue = value;
  1270. var nodes = $._tree.getNodes();
  1271. // 如果要查空字串,就退出不查了。
  1272. if (value == "") {
  1273. $.tree.showAllNode(nodes);
  1274. return;
  1275. }
  1276. $.tree.hideAllNode(nodes);
  1277. // 根据搜索值模糊匹配
  1278. $.tree.updateNodes($._tree.getNodesByParamFuzzy("name", value));
  1279. },
  1280. // 根据Id和Name选中指定节点
  1281. selectByIdName: function(treeId, node) {
  1282. if ($.common.isNotEmpty(treeId) && treeId == node.id) {
  1283. $._tree.selectNode(node, true);
  1284. }
  1285. },
  1286. // 显示所有节点
  1287. showAllNode: function(nodes) {
  1288. nodes = $._tree.transformToArray(nodes);
  1289. for (var i = nodes.length - 1; i >= 0; i--) {
  1290. if (nodes[i].getParentNode() != null) {
  1291. $._tree.expandNode(nodes[i], true, false, false, false);
  1292. } else {
  1293. $._tree.expandNode(nodes[i], true, true, false, false);
  1294. }
  1295. $._tree.showNode(nodes[i]);
  1296. $.tree.showAllNode(nodes[i].children);
  1297. }
  1298. },
  1299. // 隐藏所有节点
  1300. hideAllNode: function(nodes) {
  1301. var tree = $.fn.zTree.getZTreeObj("tree");
  1302. var nodes = $._tree.transformToArray(nodes);
  1303. for (var i = nodes.length - 1; i >= 0; i--) {
  1304. $._tree.hideNode(nodes[i]);
  1305. }
  1306. },
  1307. // 显示所有父节点
  1308. showParent: function(treeNode) {
  1309. var parentNode;
  1310. while ((parentNode = treeNode.getParentNode()) != null) {
  1311. $._tree.showNode(parentNode);
  1312. $._tree.expandNode(parentNode, true, false, false);
  1313. treeNode = parentNode;
  1314. }
  1315. },
  1316. // 显示所有孩子节点
  1317. showChildren: function(treeNode) {
  1318. if (treeNode.isParent) {
  1319. for (var idx in treeNode.children) {
  1320. var node = treeNode.children[idx];
  1321. $._tree.showNode(node);
  1322. $.tree.showChildren(node);
  1323. }
  1324. }
  1325. },
  1326. // 更新节点状态
  1327. updateNodes: function(nodeList) {
  1328. $._tree.showNodes(nodeList);
  1329. for (var i = 0, l = nodeList.length; i < l; i++) {
  1330. var treeNode = nodeList[i];
  1331. $.tree.showChildren(treeNode);
  1332. $.tree.showParent(treeNode)
  1333. }
  1334. },
  1335. // 获取当前被勾选集合
  1336. getCheckedNodes: function(column) {
  1337. var _column = $.common.isEmpty(column) ? "id" : column;
  1338. var nodes = $._tree.getCheckedNodes(true);
  1339. return $.map(nodes, function (row) {
  1340. return row[_column];
  1341. }).join();
  1342. },
  1343. // 不允许根父节点选择
  1344. notAllowParents: function(_tree) {
  1345. var nodes = _tree.getSelectedNodes();
  1346. if(nodes.length == 0){
  1347. $.modal.msgError("请选择节点后提交");
  1348. return false;
  1349. }
  1350. for (var i = 0; i < nodes.length; i++) {
  1351. if (nodes[i].level == 0) {
  1352. $.modal.msgError("不能选择根节点(" + nodes[i].name + ")");
  1353. return false;
  1354. }
  1355. if (nodes[i].isParent) {
  1356. $.modal.msgError("不能选择父节点(" + nodes[i].name + ")");
  1357. return false;
  1358. }
  1359. }
  1360. return true;
  1361. },
  1362. // 不允许最后层级节点选择
  1363. notAllowLastLevel: function(_tree) {
  1364. var nodes = _tree.getSelectedNodes();
  1365. for (var i = 0; i < nodes.length; i++) {
  1366. if (!nodes[i].isParent) {
  1367. $.modal.msgError("不能选择最后层级节点(" + nodes[i].name + ")");
  1368. return false;
  1369. }
  1370. }
  1371. return true;
  1372. },
  1373. // 隐藏/显示搜索栏
  1374. toggleSearch: function() {
  1375. $('#search').slideToggle(200);
  1376. $('#btnShow').toggle();
  1377. $('#btnHide').toggle();
  1378. $('#keyword').focus();
  1379. },
  1380. // 折叠
  1381. collapse: function() {
  1382. $._tree.expandAll(false);
  1383. },
  1384. // 展开
  1385. expand: function() {
  1386. $._tree.expandAll(true);
  1387. }
  1388. },
  1389. // 通用方法封装处理
  1390. common: {
  1391. // 判断字符串是否为空
  1392. isEmpty: function (value) {
  1393. if (value == null || this.trim(value) == "") {
  1394. return true;
  1395. }
  1396. return false;
  1397. },
  1398. // 判断一个字符串是否为非空串
  1399. isNotEmpty: function (value) {
  1400. return !$.common.isEmpty(value);
  1401. },
  1402. // 空对象转字符串
  1403. nullToStr: function(value) {
  1404. if ($.common.isEmpty(value)) {
  1405. return "-";
  1406. }
  1407. return value;
  1408. },
  1409. // 是否显示数据 为空默认为显示
  1410. visible: function (value) {
  1411. if ($.common.isEmpty(value) || value == true) {
  1412. return true;
  1413. }
  1414. return false;
  1415. },
  1416. // 空格截取
  1417. trim: function (value) {
  1418. if (value == null) {
  1419. return "";
  1420. }
  1421. return value.toString().replace(/(^\s*)|(\s*$)|\r|\n/g, "");
  1422. },
  1423. // 比较两个字符串(大小写敏感)
  1424. equals: function (str, that) {
  1425. return str == that;
  1426. },
  1427. // 比较两个字符串(大小写不敏感)
  1428. equalsIgnoreCase: function (str, that) {
  1429. return String(str).toUpperCase() === String(that).toUpperCase();
  1430. },
  1431. // 将字符串按指定字符分割
  1432. split: function (str, sep, maxLen) {
  1433. if ($.common.isEmpty(str)) {
  1434. return null;
  1435. }
  1436. var value = String(str).split(sep);
  1437. return maxLen ? value.slice(0, maxLen - 1) : value;
  1438. },
  1439. // 字符串格式化(%s )
  1440. sprintf: function (str) {
  1441. var args = arguments, flag = true, i = 1;
  1442. str = str.replace(/%s/g, function () {
  1443. var arg = args[i++];
  1444. if (typeof arg === 'undefined') {
  1445. flag = false;
  1446. return '';
  1447. }
  1448. return arg;
  1449. });
  1450. return flag ? str : '';
  1451. },
  1452. // 指定随机数返回
  1453. random: function (min, max) {
  1454. return Math.floor((Math.random() * max) + min);
  1455. },
  1456. // 判断字符串是否是以start开头
  1457. startWith: function(value, start) {
  1458. var reg = new RegExp("^" + start);
  1459. return reg.test(value)
  1460. },
  1461. // 判断字符串是否是以end结尾
  1462. endWith: function(value, end) {
  1463. var reg = new RegExp(end + "$");
  1464. return reg.test(value)
  1465. },
  1466. // 数组去重
  1467. uniqueFn: function(array) {
  1468. var result = [];
  1469. var hashObj = {};
  1470. for (var i = 0; i < array.length; i++) {
  1471. if (!hashObj[array[i]]) {
  1472. hashObj[array[i]] = true;
  1473. result.push(array[i]);
  1474. }
  1475. }
  1476. return result;
  1477. },
  1478. // 数组中的所有元素放入一个字符串
  1479. join: function(array, separator) {
  1480. if ($.common.isEmpty(array)) {
  1481. return null;
  1482. }
  1483. return array.join(separator);
  1484. },
  1485. // 获取form下所有的字段并转换为json对象
  1486. formToJSON: function(formId) {
  1487. var json = {};
  1488. $.each($("#" + formId).serializeArray(), function(i, field) {
  1489. if(json[field.name]) {
  1490. json[field.name] += ("," + field.value);
  1491. } else {
  1492. json[field.name] = field.value;
  1493. }
  1494. });
  1495. return json;
  1496. },
  1497. // 获取obj对象长度
  1498. getLength: function(obj) {
  1499. var count = 0;  
  1500. for (var i in obj) {
  1501. if (obj.hasOwnProperty(i)) {
  1502. count++;
  1503. }  
  1504. }
  1505. return count;
  1506. },
  1507. // 判断移动端
  1508. isMobile: function () {
  1509. return navigator.userAgent.match(/(Android|iPhone|SymbianOS|Windows Phone|iPad|iPod)/i);
  1510. },
  1511. }
  1512. });
  1513. })(jQuery);
  1514. /** 表格类型 */
  1515. table_type = {
  1516. bootstrapTable: 0,
  1517. bootstrapTreeTable: 1
  1518. };
  1519. /** 消息状态码 */
  1520. web_status = {
  1521. SUCCESS: 0,
  1522. FAIL: 500,
  1523. WARNING: 301
  1524. };
  1525. /** 弹窗状态码 */
  1526. modal_status = {
  1527. SUCCESS: "success",
  1528. FAIL: "error",
  1529. WARNING: "warning"
  1530. };