ry-ui.js 72 KB

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