ry-ui.js 60 KB

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