ry-ui.js 70 KB

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