ry-ui.js 69 KB

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