ry-ui.js 69 KB

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