ry-ui.js 65 KB

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