ry-ui.js 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  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. $.table.initEvent();
  544. $.bttTable = $('#' + options.id).bootstrapTreeTable({
  545. code: options.code, // 用于设置父子关系
  546. parentCode: options.parentCode, // 用于设置父子关系
  547. type: 'post', // 请求方式(*)
  548. url: options.url, // 请求后台的URL(*)
  549. data: options.data, // 无url时用于渲染的数据
  550. ajaxParams: options.ajaxParams, // 请求数据的ajax的data属性
  551. rootIdValue: options.rootIdValue, // 设置指定根节点id值
  552. height: options.height, // 表格树的高度
  553. expandColumn: options.expandColumn, // 在哪一列上面显示展开按钮
  554. striped: options.striped, // 是否显示行间隔色
  555. bordered: false, // 是否显示边框
  556. toolbar: '#' + options.toolbar, // 指定工作栏
  557. showSearch: options.showSearch, // 是否显示检索信息
  558. showRefresh: options.showRefresh, // 是否显示刷新按钮
  559. showColumns: options.showColumns, // 是否显示隐藏某列下拉框
  560. expandAll: options.expandAll, // 是否全部展开
  561. expandFirst: options.expandFirst, // 是否默认第一级展开--expandAll为false时生效
  562. columns: options.columns, // 显示列信息(*)
  563. responseHandler: $.treeTable.responseHandler, // 在加载服务器发送来的数据之前处理函数
  564. onLoadSuccess: $.table.onLoadSuccess // 当所有数据被加载时触发处理函数
  565. });
  566. },
  567. // 条件查询
  568. search: function(formId) {
  569. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  570. var params = $.common.formToJSON(currentId);
  571. $.bttTable.bootstrapTreeTable('refresh', params);
  572. },
  573. // 刷新
  574. refresh: function() {
  575. $.bttTable.bootstrapTreeTable('refresh');
  576. },
  577. // 查询表格树指定列值
  578. selectColumns: function(column) {
  579. var rows = $.map($.bttTable.bootstrapTreeTable('getSelections'), function (row) {
  580. return $.common.getItemField(row, column);
  581. });
  582. return $.common.uniqueFn(rows);
  583. },
  584. // 请求获取数据后处理回调函数,校验异常状态提醒
  585. responseHandler: function(res) {
  586. if (typeof table.options.responseHandler == "function") {
  587. table.options.responseHandler(res);
  588. }
  589. if (res.code != undefined && res.code != 0) {
  590. $.modal.alertWarning(res.msg);
  591. return [];
  592. } else {
  593. return res;
  594. }
  595. },
  596. },
  597. // 表单封装处理
  598. form: {
  599. // 表单重置
  600. reset: function(formId, tableId) {
  601. table.set(tableId);
  602. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  603. $("#" + currentId)[0].reset();
  604. if (table.options.type == table_type.bootstrapTable) {
  605. if($.common.isEmpty(tableId)){
  606. $("#" + table.options.id).bootstrapTable('refresh');
  607. } else{
  608. $("#" + tableId).bootstrapTable('refresh');
  609. }
  610. } else if (table.options.type == table_type.bootstrapTreeTable) {
  611. if($.common.isEmpty(tableId)){
  612. $("#" + table.options.id).bootstrapTreeTable('refresh', []);
  613. } else{
  614. $("#" + tableId).bootstrapTreeTable('refresh', []);
  615. }
  616. }
  617. },
  618. // 获取选中复选框项
  619. selectCheckeds: function(name) {
  620. var checkeds = "";
  621. $('input:checkbox[name="' + name + '"]:checked').each(function(i) {
  622. if (0 == i) {
  623. checkeds = $(this).val();
  624. } else {
  625. checkeds += ("," + $(this).val());
  626. }
  627. });
  628. return checkeds;
  629. },
  630. // 获取选中下拉框项
  631. selectSelects: function(name) {
  632. var selects = "";
  633. $('#' + name + ' option:selected').each(function (i) {
  634. if (0 == i) {
  635. selects = $(this).val();
  636. } else {
  637. selects += ("," + $(this).val());
  638. }
  639. });
  640. return selects;
  641. }
  642. },
  643. // 弹出层封装处理
  644. modal: {
  645. // 显示图标
  646. icon: function(type) {
  647. var icon = "";
  648. if (type == modal_status.WARNING) {
  649. icon = 0;
  650. } else if (type == modal_status.SUCCESS) {
  651. icon = 1;
  652. } else if (type == modal_status.FAIL) {
  653. icon = 2;
  654. } else {
  655. icon = 3;
  656. }
  657. return icon;
  658. },
  659. // 消息提示
  660. msg: function(content, type) {
  661. if (type != undefined) {
  662. layer.msg(content, { icon: $.modal.icon(type), time: 1000, shift: 5 });
  663. } else {
  664. layer.msg(content);
  665. }
  666. },
  667. // 错误消息
  668. msgError: function(content) {
  669. $.modal.msg(content, modal_status.FAIL);
  670. },
  671. // 成功消息
  672. msgSuccess: function(content) {
  673. $.modal.msg(content, modal_status.SUCCESS);
  674. },
  675. // 警告消息
  676. msgWarning: function(content) {
  677. $.modal.msg(content, modal_status.WARNING);
  678. },
  679. // 弹出提示
  680. alert: function(content, type) {
  681. layer.alert(content, {
  682. icon: $.modal.icon(type),
  683. title: "系统提示",
  684. btn: ['确认'],
  685. btnclass: ['btn btn-primary'],
  686. });
  687. },
  688. // 消息提示并刷新父窗体
  689. msgReload: function(msg, type) {
  690. layer.msg(msg, {
  691. icon: $.modal.icon(type),
  692. time: 500,
  693. shade: [0.1, '#8F8F8F']
  694. },
  695. function() {
  696. $.modal.reload();
  697. });
  698. },
  699. // 错误提示
  700. alertError: function(content) {
  701. $.modal.alert(content, modal_status.FAIL);
  702. },
  703. // 成功提示
  704. alertSuccess: function(content) {
  705. $.modal.alert(content, modal_status.SUCCESS);
  706. },
  707. // 警告提示
  708. alertWarning: function(content) {
  709. $.modal.alert(content, modal_status.WARNING);
  710. },
  711. // 关闭窗体
  712. close: function () {
  713. var index = parent.layer.getFrameIndex(window.name);
  714. parent.layer.close(index);
  715. },
  716. // 关闭全部窗体
  717. closeAll: function () {
  718. layer.closeAll();
  719. },
  720. // 确认窗体
  721. confirm: function (content, callBack) {
  722. layer.confirm(content, {
  723. icon: 3,
  724. title: "系统提示",
  725. btn: ['确认', '取消']
  726. }, function (index) {
  727. layer.close(index);
  728. callBack(true);
  729. });
  730. },
  731. // 弹出层指定宽度
  732. open: function (title, url, width, height, callback) {
  733. //如果是移动端,就使用自适应大小弹窗
  734. if ($.common.isMobile()) {
  735. width = 'auto';
  736. height = 'auto';
  737. }
  738. if ($.common.isEmpty(title)) {
  739. title = false;
  740. }
  741. if ($.common.isEmpty(url)) {
  742. url = "/404.html";
  743. }
  744. if ($.common.isEmpty(width)) {
  745. width = 800;
  746. }
  747. if ($.common.isEmpty(height)) {
  748. height = ($(window).height() - 50);
  749. }
  750. if ($.common.isEmpty(callback)) {
  751. callback = function(index, layero) {
  752. var iframeWin = layero.find('iframe')[0];
  753. iframeWin.contentWindow.submitHandler(index, layero);
  754. }
  755. }
  756. layer.open({
  757. type: 2,
  758. area: [width + 'px', height + 'px'],
  759. fix: false,
  760. //不固定
  761. maxmin: true,
  762. shade: 0.3,
  763. title: title,
  764. content: url,
  765. btn: ['确定', '关闭'],
  766. // 弹层外区域关闭
  767. shadeClose: true,
  768. yes: callback,
  769. cancel: function(index) {
  770. return true;
  771. }
  772. });
  773. },
  774. // 弹出层指定参数选项
  775. openOptions: function (options) {
  776. var _url = $.common.isEmpty(options.url) ? "/404.html" : options.url;
  777. var _title = $.common.isEmpty(options.title) ? "系统窗口" : options.title;
  778. var _width = $.common.isEmpty(options.width) ? "800" : options.width;
  779. var _height = $.common.isEmpty(options.height) ? ($(window).height() - 50) : options.height;
  780. var _btn = ['<i class="fa fa-check"></i> 确认', '<i class="fa fa-close"></i> 关闭'];
  781. if ($.common.isEmpty(options.yes)) {
  782. options.yes = function(index, layero) {
  783. options.callBack(index, layero);
  784. }
  785. }
  786. layer.open({
  787. type: 2,
  788. maxmin: true,
  789. shade: 0.3,
  790. title: _title,
  791. fix: false,
  792. area: [_width + 'px', _height + 'px'],
  793. content: _url,
  794. shadeClose: $.common.isEmpty(options.shadeClose) ? true : options.shadeClose,
  795. skin: options.skin,
  796. btn: $.common.isEmpty(options.btn) ? _btn : options.btn,
  797. yes: options.yes,
  798. cancel: function () {
  799. return true;
  800. }
  801. });
  802. },
  803. // 弹出层全屏
  804. openFull: function (title, url, width, height) {
  805. //如果是移动端,就使用自适应大小弹窗
  806. if ($.common.isMobile()) {
  807. width = 'auto';
  808. height = 'auto';
  809. }
  810. if ($.common.isEmpty(title)) {
  811. title = false;
  812. }
  813. if ($.common.isEmpty(url)) {
  814. url = "/404.html";
  815. }
  816. if ($.common.isEmpty(width)) {
  817. width = 800;
  818. }
  819. if ($.common.isEmpty(height)) {
  820. height = ($(window).height() - 50);
  821. }
  822. var index = layer.open({
  823. type: 2,
  824. area: [width + 'px', height + 'px'],
  825. fix: false,
  826. //不固定
  827. maxmin: true,
  828. shade: 0.3,
  829. title: title,
  830. content: url,
  831. btn: ['确定', '关闭'],
  832. // 弹层外区域关闭
  833. shadeClose: true,
  834. yes: function(index, layero) {
  835. var iframeWin = layero.find('iframe')[0];
  836. iframeWin.contentWindow.submitHandler(index, layero);
  837. },
  838. cancel: function(index) {
  839. return true;
  840. }
  841. });
  842. layer.full(index);
  843. },
  844. // 选卡页方式打开
  845. openTab: function (title, url) {
  846. createMenuItem(url, title);
  847. },
  848. // 选卡页同一页签打开
  849. parentTab: function (title, url) {
  850. var dataId = window.frameElement.getAttribute('data-id');
  851. createMenuItem(url, title);
  852. closeItem(dataId);
  853. },
  854. // 关闭选项卡
  855. closeTab: function (dataId) {
  856. closeItem(dataId);
  857. },
  858. // 禁用按钮
  859. disable: function() {
  860. var doc = window.top == window.parent ? window.document : window.parent.document;
  861. $("a[class*=layui-layer-btn]", doc).addClass("layer-disabled");
  862. },
  863. // 启用按钮
  864. enable: function() {
  865. var doc = window.top == window.parent ? window.document : window.parent.document;
  866. $("a[class*=layui-layer-btn]", doc).removeClass("layer-disabled");
  867. },
  868. // 打开遮罩层
  869. loading: function (message) {
  870. $.blockUI({ message: '<div class="loaderbox"><div class="loading-activity"></div> ' + message + '</div>' });
  871. },
  872. // 关闭遮罩层
  873. closeLoading: function () {
  874. setTimeout(function(){
  875. $.unblockUI();
  876. }, 50);
  877. },
  878. // 重新加载
  879. reload: function () {
  880. parent.location.reload();
  881. }
  882. },
  883. // 操作封装处理
  884. operate: {
  885. // 提交数据
  886. submit: function(url, type, dataType, data, callback) {
  887. var config = {
  888. url: url,
  889. type: type,
  890. dataType: dataType,
  891. data: data,
  892. beforeSend: function () {
  893. $.modal.loading("正在处理中,请稍后...");
  894. },
  895. success: function(result) {
  896. if (typeof callback == "function") {
  897. callback(result);
  898. }
  899. $.operate.ajaxSuccess(result);
  900. }
  901. };
  902. $.ajax(config)
  903. },
  904. // post请求传输
  905. post: function(url, data, callback) {
  906. $.operate.submit(url, "post", "json", data, callback);
  907. },
  908. // get请求传输
  909. get: function(url, callback) {
  910. $.operate.submit(url, "get", "json", "", callback);
  911. },
  912. // 详细信息
  913. detail: function(id, width, height) {
  914. table.set();
  915. var _url = $.operate.detailUrl(id);
  916. var _width = $.common.isEmpty(width) ? "800" : width;
  917. var _height = $.common.isEmpty(height) ? ($(window).height() - 50) : height;
  918. //如果是移动端,就使用自适应大小弹窗
  919. if ($.common.isMobile()) {
  920. _width = 'auto';
  921. _height = 'auto';
  922. }
  923. var options = {
  924. title: table.options.modalName + "详细",
  925. width: _width,
  926. height: _height,
  927. url: _url,
  928. skin: 'layui-layer-gray',
  929. btn: ['关闭'],
  930. yes: function (index, layero) {
  931. layer.close(index);
  932. }
  933. };
  934. $.modal.openOptions(options);
  935. },
  936. // 详细访问地址
  937. detailUrl: function(id) {
  938. var url = "/404.html";
  939. if ($.common.isNotEmpty(id)) {
  940. url = table.options.detailUrl.replace("{id}", id);
  941. } else {
  942. var id = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  943. if (id.length == 0) {
  944. $.modal.alertWarning("请至少选择一条记录");
  945. return;
  946. }
  947. url = table.options.detailUrl.replace("{id}", id);
  948. }
  949. return url;
  950. },
  951. // 删除信息
  952. remove: function(id) {
  953. table.set();
  954. $.modal.confirm("确定删除该条" + table.options.modalName + "信息吗?", function() {
  955. var url = $.common.isEmpty(id) ? table.options.removeUrl : table.options.removeUrl.replace("{id}", id);
  956. if(table.options.type == table_type.bootstrapTreeTable) {
  957. $.operate.get(url);
  958. } else {
  959. var data = { "ids": id };
  960. $.operate.submit(url, "post", "json", data);
  961. }
  962. });
  963. },
  964. // 批量删除信息
  965. removeAll: function() {
  966. table.set();
  967. var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  968. if (rows.length == 0) {
  969. $.modal.alertWarning("请至少选择一条记录");
  970. return;
  971. }
  972. $.modal.confirm("确认要删除选中的" + rows.length + "条数据吗?", function() {
  973. var url = table.options.removeUrl;
  974. var data = { "ids": rows.join() };
  975. $.operate.submit(url, "post", "json", data);
  976. });
  977. },
  978. // 清空信息
  979. clean: function() {
  980. table.set();
  981. $.modal.confirm("确定清空所有" + table.options.modalName + "吗?", function() {
  982. var url = table.options.cleanUrl;
  983. $.operate.submit(url, "post", "json", "");
  984. });
  985. },
  986. // 添加信息
  987. add: function(id) {
  988. table.set();
  989. $.modal.open("添加" + table.options.modalName, $.operate.addUrl(id));
  990. },
  991. // 添加信息,以tab页展现
  992. addTab: function (id) {
  993. table.set();
  994. $.modal.openTab("添加" + table.options.modalName, $.operate.addUrl(id));
  995. },
  996. // 添加信息 全屏
  997. addFull: function(id) {
  998. table.set();
  999. var url = $.common.isEmpty(id) ? table.options.createUrl : table.options.createUrl.replace("{id}", id);
  1000. $.modal.openFull("添加" + table.options.modalName, url);
  1001. },
  1002. // 添加访问地址
  1003. addUrl: function(id) {
  1004. var url = $.common.isEmpty(id) ? table.options.createUrl.replace("{id}", "") : table.options.createUrl.replace("{id}", id);
  1005. return url;
  1006. },
  1007. // 修改信息
  1008. edit: function(id) {
  1009. table.set();
  1010. if($.common.isEmpty(id) && table.options.type == table_type.bootstrapTreeTable) {
  1011. var row = $("#" + table.options.id).bootstrapTreeTable('getSelections')[0];
  1012. if ($.common.isEmpty(row)) {
  1013. $.modal.alertWarning("请至少选择一条记录");
  1014. return;
  1015. }
  1016. var url = table.options.updateUrl.replace("{id}", row[table.options.uniqueId]);
  1017. $.modal.open("修改" + table.options.modalName, url);
  1018. } else {
  1019. $.modal.open("修改" + table.options.modalName, $.operate.editUrl(id));
  1020. }
  1021. },
  1022. // 修改信息,以tab页展现
  1023. editTab: function(id) {
  1024. table.set();
  1025. $.modal.openTab("修改" + table.options.modalName, $.operate.editUrl(id));
  1026. },
  1027. // 修改信息 全屏
  1028. editFull: function(id) {
  1029. table.set();
  1030. var url = "/404.html";
  1031. if ($.common.isNotEmpty(id)) {
  1032. url = table.options.updateUrl.replace("{id}", id);
  1033. } else {
  1034. if(table.options.type == table_type.bootstrapTreeTable) {
  1035. var row = $("#" + table.options.id).bootstrapTreeTable('getSelections')[0];
  1036. if ($.common.isEmpty(row)) {
  1037. $.modal.alertWarning("请至少选择一条记录");
  1038. return;
  1039. }
  1040. url = table.options.updateUrl.replace("{id}", row[table.options.uniqueId]);
  1041. } else {
  1042. var row = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  1043. url = table.options.updateUrl.replace("{id}", row);
  1044. }
  1045. }
  1046. $.modal.openFull("修改" + table.options.modalName, url);
  1047. },
  1048. // 修改访问地址
  1049. editUrl: function(id) {
  1050. var url = "/404.html";
  1051. if ($.common.isNotEmpty(id)) {
  1052. url = table.options.updateUrl.replace("{id}", id);
  1053. } else {
  1054. var id = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  1055. if (id.length == 0) {
  1056. $.modal.alertWarning("请至少选择一条记录");
  1057. return;
  1058. }
  1059. url = table.options.updateUrl.replace("{id}", id);
  1060. }
  1061. return url;
  1062. },
  1063. // 保存信息 刷新表格
  1064. save: function(url, data, callback) {
  1065. var config = {
  1066. url: url,
  1067. type: "post",
  1068. dataType: "json",
  1069. data: data,
  1070. beforeSend: function () {
  1071. $.modal.loading("正在处理中,请稍后...");
  1072. $.modal.disable();
  1073. },
  1074. success: function(result) {
  1075. if (typeof callback == "function") {
  1076. callback(result);
  1077. }
  1078. $.operate.successCallback(result);
  1079. }
  1080. };
  1081. $.ajax(config)
  1082. },
  1083. // 保存信息 弹出提示框
  1084. saveModal: function(url, data, callback) {
  1085. var config = {
  1086. url: url,
  1087. type: "post",
  1088. dataType: "json",
  1089. data: data,
  1090. beforeSend: function () {
  1091. $.modal.loading("正在处理中,请稍后...");
  1092. },
  1093. success: function(result) {
  1094. if (typeof callback == "function") {
  1095. callback(result);
  1096. }
  1097. if (result.code == web_status.SUCCESS) {
  1098. $.modal.alertSuccess(result.msg)
  1099. } else if (result.code == web_status.WARNING) {
  1100. $.modal.alertWarning(result.msg)
  1101. } else {
  1102. $.modal.alertError(result.msg);
  1103. }
  1104. $.modal.closeLoading();
  1105. }
  1106. };
  1107. $.ajax(config)
  1108. },
  1109. // 保存选项卡信息
  1110. saveTab: function(url, data, callback) {
  1111. var config = {
  1112. url: url,
  1113. type: "post",
  1114. dataType: "json",
  1115. data: data,
  1116. beforeSend: function () {
  1117. $.modal.loading("正在处理中,请稍后...");
  1118. },
  1119. success: function(result) {
  1120. if (typeof callback == "function") {
  1121. callback(result);
  1122. }
  1123. $.operate.successTabCallback(result);
  1124. }
  1125. };
  1126. $.ajax(config)
  1127. },
  1128. // 保存结果弹出msg刷新table表格
  1129. ajaxSuccess: function (result) {
  1130. if (result.code == web_status.SUCCESS && table.options.type == table_type.bootstrapTable) {
  1131. $.modal.msgSuccess(result.msg);
  1132. $.table.refresh();
  1133. } else if (result.code == web_status.SUCCESS && table.options.type == table_type.bootstrapTreeTable) {
  1134. $.modal.msgSuccess(result.msg);
  1135. $.treeTable.refresh();
  1136. } else if (result.code == web_status.SUCCESS && table.option.type == undefined) {
  1137. $.modal.msgSuccess(result.msg)
  1138. } else if (result.code == web_status.WARNING) {
  1139. $.modal.alertWarning(result.msg)
  1140. } else {
  1141. $.modal.alertError(result.msg);
  1142. }
  1143. $.modal.closeLoading();
  1144. },
  1145. // 成功结果提示msg(父窗体全局更新)
  1146. saveSuccess: function (result) {
  1147. if (result.code == web_status.SUCCESS) {
  1148. $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
  1149. } else if (result.code == web_status.WARNING) {
  1150. $.modal.alertWarning(result.msg)
  1151. } else {
  1152. $.modal.alertError(result.msg);
  1153. }
  1154. $.modal.closeLoading();
  1155. },
  1156. // 成功回调执行事件(父窗体静默更新)
  1157. successCallback: function(result) {
  1158. if (result.code == web_status.SUCCESS) {
  1159. var parent = window.parent;
  1160. if (parent.table.options.type == table_type.bootstrapTable) {
  1161. $.modal.close();
  1162. parent.$.modal.msgSuccess(result.msg);
  1163. parent.$.table.refresh();
  1164. } else if (parent.table.options.type == table_type.bootstrapTreeTable) {
  1165. $.modal.close();
  1166. parent.$.modal.msgSuccess(result.msg);
  1167. parent.$.treeTable.refresh();
  1168. } else {
  1169. $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
  1170. }
  1171. } else if (result.code == web_status.WARNING) {
  1172. $.modal.alertWarning(result.msg)
  1173. } else {
  1174. $.modal.alertError(result.msg);
  1175. }
  1176. $.modal.closeLoading();
  1177. $.modal.enable();
  1178. },
  1179. // 选项卡成功回调执行事件(父窗体静默更新)
  1180. successTabCallback: function(result) {
  1181. if (result.code == web_status.SUCCESS) {
  1182. var topWindow = $(window.parent.document);
  1183. var currentId = $('.page-tabs-content', topWindow).find('.active').attr('data-panel');
  1184. var $contentWindow = $('.RuoYi_iframe[data-id="' + currentId + '"]', topWindow)[0].contentWindow;
  1185. $.modal.close();
  1186. $contentWindow.$.modal.msgSuccess(result.msg);
  1187. $contentWindow.$(".layui-layer-padding").removeAttr("style");
  1188. if ($contentWindow.table.options.type == table_type.bootstrapTable) {
  1189. $contentWindow.$.table.refresh();
  1190. } else if ($contentWindow.table.options.type == table_type.bootstrapTreeTable) {
  1191. $contentWindow.$.treeTable.refresh();
  1192. }
  1193. $.modal.closeTab();
  1194. } else if (result.code == web_status.WARNING) {
  1195. $.modal.alertWarning(result.msg)
  1196. } else {
  1197. $.modal.alertError(result.msg);
  1198. }
  1199. $.modal.closeLoading();
  1200. }
  1201. },
  1202. // 校验封装处理
  1203. validate: {
  1204. // 判断返回标识是否唯一 false 不存在 true 存在
  1205. unique: function (value) {
  1206. if (value == "0") {
  1207. return true;
  1208. }
  1209. return false;
  1210. },
  1211. // 表单验证
  1212. form: function (formId) {
  1213. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  1214. return $("#" + currentId).validate().form();
  1215. },
  1216. // 重置表单验证(清除提示信息)
  1217. reset: function (formId) {
  1218. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  1219. return $("#" + currentId).validate().resetForm();
  1220. }
  1221. },
  1222. // 树插件封装处理
  1223. tree: {
  1224. _option: {},
  1225. _lastValue: {},
  1226. // 初始化树结构
  1227. init: function(options) {
  1228. var defaults = {
  1229. id: "tree", // 属性ID
  1230. expandLevel: 0, // 展开等级节点
  1231. view: {
  1232. selectedMulti: false, // 设置是否允许同时选中多个节点
  1233. nameIsHTML: true // 设置 name 属性是否支持 HTML 脚本
  1234. },
  1235. check: {
  1236. enable: false, // 置 zTree 的节点上是否显示 checkbox / radio
  1237. nocheckInherit: true, // 设置子节点是否自动继承
  1238. },
  1239. data: {
  1240. key: {
  1241. title: "title" // 节点数据保存节点提示信息的属性名称
  1242. },
  1243. simpleData: {
  1244. enable: true // true / false 分别表示 使用 / 不使用 简单数据模式
  1245. }
  1246. },
  1247. };
  1248. var options = $.extend(defaults, options);
  1249. $.tree._option = options;
  1250. // 树结构初始化加载
  1251. var setting = {
  1252. callback: {
  1253. onClick: options.onClick, // 用于捕获节点被点击的事件回调函数
  1254. onCheck: options.onCheck, // 用于捕获 checkbox / radio 被勾选 或 取消勾选的事件回调函数
  1255. onDblClick: options.onDblClick // 用于捕获鼠标双击之后的事件回调函数
  1256. },
  1257. check: options.check,
  1258. view: options.view,
  1259. data: options.data
  1260. };
  1261. $.get(options.url, function(data) {
  1262. var treeId = $("#treeId").val();
  1263. tree = $.fn.zTree.init($("#" + options.id), setting, data);
  1264. $._tree = tree;
  1265. for (var i = 0; i < options.expandLevel; i++) {
  1266. var nodes = tree.getNodesByParam("level", i);
  1267. for (var j = 0; j < nodes.length; j++) {
  1268. tree.expandNode(nodes[j], true, false, false);
  1269. }
  1270. }
  1271. var node = tree.getNodesByParam("id", treeId, null)[0];
  1272. $.tree.selectByIdName(treeId, node);
  1273. });
  1274. },
  1275. // 搜索节点
  1276. searchNode: function() {
  1277. // 取得输入的关键字的值
  1278. var value = $.common.trim($("#keyword").val());
  1279. if ($.tree._lastValue == value) {
  1280. return;
  1281. }
  1282. // 保存最后一次搜索名称
  1283. $.tree._lastValue = value;
  1284. var nodes = $._tree.getNodes();
  1285. // 如果要查空字串,就退出不查了。
  1286. if (value == "") {
  1287. $.tree.showAllNode(nodes);
  1288. return;
  1289. }
  1290. $.tree.hideAllNode(nodes);
  1291. // 根据搜索值模糊匹配
  1292. $.tree.updateNodes($._tree.getNodesByParamFuzzy("name", value));
  1293. },
  1294. // 根据Id和Name选中指定节点
  1295. selectByIdName: function(treeId, node) {
  1296. if ($.common.isNotEmpty(treeId) && treeId == node.id) {
  1297. $._tree.selectNode(node, true);
  1298. }
  1299. },
  1300. // 显示所有节点
  1301. showAllNode: function(nodes) {
  1302. nodes = $._tree.transformToArray(nodes);
  1303. for (var i = nodes.length - 1; i >= 0; i--) {
  1304. if (nodes[i].getParentNode() != null) {
  1305. $._tree.expandNode(nodes[i], true, false, false, false);
  1306. } else {
  1307. $._tree.expandNode(nodes[i], true, true, false, false);
  1308. }
  1309. $._tree.showNode(nodes[i]);
  1310. $.tree.showAllNode(nodes[i].children);
  1311. }
  1312. },
  1313. // 隐藏所有节点
  1314. hideAllNode: function(nodes) {
  1315. var tree = $.fn.zTree.getZTreeObj("tree");
  1316. var nodes = $._tree.transformToArray(nodes);
  1317. for (var i = nodes.length - 1; i >= 0; i--) {
  1318. $._tree.hideNode(nodes[i]);
  1319. }
  1320. },
  1321. // 显示所有父节点
  1322. showParent: function(treeNode) {
  1323. var parentNode;
  1324. while ((parentNode = treeNode.getParentNode()) != null) {
  1325. $._tree.showNode(parentNode);
  1326. $._tree.expandNode(parentNode, true, false, false);
  1327. treeNode = parentNode;
  1328. }
  1329. },
  1330. // 显示所有孩子节点
  1331. showChildren: function(treeNode) {
  1332. if (treeNode.isParent) {
  1333. for (var idx in treeNode.children) {
  1334. var node = treeNode.children[idx];
  1335. $._tree.showNode(node);
  1336. $.tree.showChildren(node);
  1337. }
  1338. }
  1339. },
  1340. // 更新节点状态
  1341. updateNodes: function(nodeList) {
  1342. $._tree.showNodes(nodeList);
  1343. for (var i = 0, l = nodeList.length; i < l; i++) {
  1344. var treeNode = nodeList[i];
  1345. $.tree.showChildren(treeNode);
  1346. $.tree.showParent(treeNode)
  1347. }
  1348. },
  1349. // 获取当前被勾选集合
  1350. getCheckedNodes: function(column) {
  1351. var _column = $.common.isEmpty(column) ? "id" : column;
  1352. var nodes = $._tree.getCheckedNodes(true);
  1353. return $.map(nodes, function (row) {
  1354. return row[_column];
  1355. }).join();
  1356. },
  1357. // 不允许根父节点选择
  1358. notAllowParents: function(_tree) {
  1359. var nodes = _tree.getSelectedNodes();
  1360. if(nodes.length == 0){
  1361. $.modal.msgError("请选择节点后提交");
  1362. return false;
  1363. }
  1364. for (var i = 0; i < nodes.length; i++) {
  1365. if (nodes[i].level == 0) {
  1366. $.modal.msgError("不能选择根节点(" + nodes[i].name + ")");
  1367. return false;
  1368. }
  1369. if (nodes[i].isParent) {
  1370. $.modal.msgError("不能选择父节点(" + nodes[i].name + ")");
  1371. return false;
  1372. }
  1373. }
  1374. return true;
  1375. },
  1376. // 不允许最后层级节点选择
  1377. notAllowLastLevel: function(_tree) {
  1378. var nodes = _tree.getSelectedNodes();
  1379. for (var i = 0; i < nodes.length; i++) {
  1380. if (!nodes[i].isParent) {
  1381. $.modal.msgError("不能选择最后层级节点(" + nodes[i].name + ")");
  1382. return false;
  1383. }
  1384. }
  1385. return true;
  1386. },
  1387. // 隐藏/显示搜索栏
  1388. toggleSearch: function() {
  1389. $('#search').slideToggle(200);
  1390. $('#btnShow').toggle();
  1391. $('#btnHide').toggle();
  1392. $('#keyword').focus();
  1393. },
  1394. // 折叠
  1395. collapse: function() {
  1396. $._tree.expandAll(false);
  1397. },
  1398. // 展开
  1399. expand: function() {
  1400. $._tree.expandAll(true);
  1401. }
  1402. },
  1403. // 通用方法封装处理
  1404. common: {
  1405. // 判断字符串是否为空
  1406. isEmpty: function (value) {
  1407. if (value == null || this.trim(value) == "") {
  1408. return true;
  1409. }
  1410. return false;
  1411. },
  1412. // 判断一个字符串是否为非空串
  1413. isNotEmpty: function (value) {
  1414. return !$.common.isEmpty(value);
  1415. },
  1416. // 空对象转字符串
  1417. nullToStr: function(value) {
  1418. if ($.common.isEmpty(value)) {
  1419. return "-";
  1420. }
  1421. return value;
  1422. },
  1423. // 是否显示数据 为空默认为显示
  1424. visible: function (value) {
  1425. if ($.common.isEmpty(value) || value == true) {
  1426. return true;
  1427. }
  1428. return false;
  1429. },
  1430. // 空格截取
  1431. trim: function (value) {
  1432. if (value == null) {
  1433. return "";
  1434. }
  1435. return value.toString().replace(/(^\s*)|(\s*$)|\r|\n/g, "");
  1436. },
  1437. // 比较两个字符串(大小写敏感)
  1438. equals: function (str, that) {
  1439. return str == that;
  1440. },
  1441. // 比较两个字符串(大小写不敏感)
  1442. equalsIgnoreCase: function (str, that) {
  1443. return String(str).toUpperCase() === String(that).toUpperCase();
  1444. },
  1445. // 将字符串按指定字符分割
  1446. split: function (str, sep, maxLen) {
  1447. if ($.common.isEmpty(str)) {
  1448. return null;
  1449. }
  1450. var value = String(str).split(sep);
  1451. return maxLen ? value.slice(0, maxLen - 1) : value;
  1452. },
  1453. // 字符串格式化(%s )
  1454. sprintf: function (str) {
  1455. var args = arguments, flag = true, i = 1;
  1456. str = str.replace(/%s/g, function () {
  1457. var arg = args[i++];
  1458. if (typeof arg === 'undefined') {
  1459. flag = false;
  1460. return '';
  1461. }
  1462. return arg;
  1463. });
  1464. return flag ? str : '';
  1465. },
  1466. // 获取节点数据,支持多层级访问
  1467. getItemField: function (item, field) {
  1468. var value = item;
  1469. if (typeof field !== 'string' || item.hasOwnProperty(field)) {
  1470. return item[field];
  1471. }
  1472. var props = field.split('.');
  1473. for (var p in props) {
  1474. value = value && value[props[p]];
  1475. }
  1476. return value;
  1477. },
  1478. // 指定随机数返回
  1479. random: function (min, max) {
  1480. return Math.floor((Math.random() * max) + min);
  1481. },
  1482. // 判断字符串是否是以start开头
  1483. startWith: function(value, start) {
  1484. var reg = new RegExp("^" + start);
  1485. return reg.test(value)
  1486. },
  1487. // 判断字符串是否是以end结尾
  1488. endWith: function(value, end) {
  1489. var reg = new RegExp(end + "$");
  1490. return reg.test(value)
  1491. },
  1492. // 数组去重
  1493. uniqueFn: function(array) {
  1494. var result = [];
  1495. var hashObj = {};
  1496. for (var i = 0; i < array.length; i++) {
  1497. if (!hashObj[array[i]]) {
  1498. hashObj[array[i]] = true;
  1499. result.push(array[i]);
  1500. }
  1501. }
  1502. return result;
  1503. },
  1504. // 数组中的所有元素放入一个字符串
  1505. join: function(array, separator) {
  1506. if ($.common.isEmpty(array)) {
  1507. return null;
  1508. }
  1509. return array.join(separator);
  1510. },
  1511. // 获取form下所有的字段并转换为json对象
  1512. formToJSON: function(formId) {
  1513. var json = {};
  1514. $.each($("#" + formId).serializeArray(), function(i, field) {
  1515. if(json[field.name]) {
  1516. json[field.name] += ("," + field.value);
  1517. } else {
  1518. json[field.name] = field.value;
  1519. }
  1520. });
  1521. return json;
  1522. },
  1523. // 获取obj对象长度
  1524. getLength: function(obj) {
  1525. var count = 0;  
  1526. for (var i in obj) {
  1527. if (obj.hasOwnProperty(i)) {
  1528. count++;
  1529. }  
  1530. }
  1531. return count;
  1532. },
  1533. // 判断移动端
  1534. isMobile: function () {
  1535. return navigator.userAgent.match(/(Android|iPhone|SymbianOS|Windows Phone|iPad|iPod)/i);
  1536. },
  1537. }
  1538. });
  1539. })(jQuery);
  1540. /** 表格类型 */
  1541. table_type = {
  1542. bootstrapTable: 0,
  1543. bootstrapTreeTable: 1
  1544. };
  1545. /** 消息状态码 */
  1546. web_status = {
  1547. SUCCESS: 0,
  1548. FAIL: 500,
  1549. WARNING: 301
  1550. };
  1551. /** 弹窗状态码 */
  1552. modal_status = {
  1553. SUCCESS: "success",
  1554. FAIL: "error",
  1555. WARNING: "warning"
  1556. };