ry-ui.js 66 KB

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