ry-ui.js 66 KB

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