ry-ui.js 69 KB

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