ry-ui.js 67 KB

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