ry-ui.js 67 KB

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