ry-ui.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /**
  2. * 通用js方法封装处理
  3. * Copyright (c) 2018 ruoyi
  4. */
  5. (function ($) {
  6. $.extend({
  7. _treeTable: {},
  8. _tree: {},
  9. // 表格封装处理
  10. table: {
  11. _option: {},
  12. _params: {},
  13. // 初始化表格
  14. init: function(options) {
  15. $.table._option = options;
  16. $.table._params = $.common.isEmpty(options.queryParams) ? $.table.queryParams : options.queryParams;
  17. _sortOrder = $.common.isEmpty(options.sortOrder) ? "asc" : options.sortOrder;
  18. _sortName = $.common.isEmpty(options.sortName) ? "" : options.sortName;
  19. _striped = $.common.isEmpty(options.striped) ? false : options.striped;
  20. $('#bootstrap-table').bootstrapTable({
  21. url: options.url, // 请求后台的URL(*)
  22. contentType: "application/x-www-form-urlencoded", // 编码类型
  23. method: 'post', // 请求方式(*)
  24. cache: false, // 是否使用缓存
  25. striped: _striped, // 是否显示行间隔色
  26. sortable: true, // 是否启用排序
  27. sortStable: true, // 设置为 true 将获得稳定的排序
  28. sortName: _sortName, // 排序列名称
  29. sortOrder: _sortOrder, // 排序方式 asc 或者 desc
  30. pagination: $.common.visible(options.pagination), // 是否显示分页(*)
  31. pageNumber: 1, // 初始化加载第一页,默认第一页
  32. pageSize: 10, // 每页的记录行数(*)
  33. pageList: [10, 25, 50], // 可供选择的每页的行数(*)
  34. iconSize: 'outline', // 图标大小:undefined默认的按钮尺寸 xs超小按钮sm小按钮lg大按钮
  35. toolbar: '#toolbar', // 指定工作栏
  36. sidePagination: "server", // 启用服务端分页
  37. search: $.common.visible(options.search), // 是否显示搜索框功能
  38. showSearch: $.common.visible(options.showSearch), // 是否显示检索信息
  39. showRefresh: $.common.visible(options.showRefresh), // 是否显示刷新按钮
  40. showColumns: $.common.visible(options.showColumns), // 是否显示隐藏某列下拉框
  41. showToggle: $.common.visible(options.showToggle), // 是否显示详细视图和列表视图的切换按钮
  42. showExport: $.common.visible(options.showExport), // 是否支持导出文件
  43. queryParams: $.table._params, // 传递参数(*)
  44. columns: options.columns, // 显示列信息(*)
  45. responseHandler: $.table.responseHandler // 回调函数
  46. });
  47. },
  48. // 查询条件
  49. queryParams: function(params) {
  50. return {
  51. // 传递参数查询参数
  52. pageSize: params.limit,
  53. pageNum: params.offset / params.limit + 1,
  54. searchValue: params.search,
  55. orderByColumn: params.sort,
  56. isAsc: params.order
  57. };
  58. },
  59. // 请求获取数据后处理回调函数
  60. responseHandler: function(res) {
  61. if (res.code == 0) {
  62. return { rows: res.rows, total: res.total };
  63. } else {
  64. $.modal.alertWarning(res.msg);
  65. return { rows: [], total: 0 };
  66. }
  67. },
  68. // 搜索
  69. search: function(formId) {
  70. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  71. var params = $("#bootstrap-table").bootstrapTable('getOptions');
  72. params.queryParams = function(params) {
  73. var search = {};
  74. $.each($("#" + currentId).serializeArray(), function(i, field) {
  75. search[field.name] = field.value;
  76. });
  77. search.pageSize = params.limit;
  78. search.pageNum = params.offset / params.limit + 1;
  79. search.searchValue = params.search;
  80. search.orderByColumn = params.sort;
  81. search.isAsc = params.order;
  82. return search;
  83. }
  84. $("#bootstrap-table").bootstrapTable('refresh', params);
  85. },
  86. // 下载
  87. exportExcel: function(formId) {
  88. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  89. $.modal.loading("正在导出数据,请稍后...");
  90. $.post($.table._option.exportUrl, $("#" + currentId).serializeArray(), function(result) {
  91. if (result.code == web_status.SUCCESS) {
  92. window.location.href = ctx + "common/download?fileName=" + result.msg + "&delete=" + true;
  93. } else {
  94. $.modal.alertError(result.msg);
  95. }
  96. $.modal.closeLoading();
  97. });
  98. },
  99. // 刷新
  100. refresh: function() {
  101. $("#bootstrap-table").bootstrapTable('refresh', {
  102. url: $.table._option.url,
  103. silent: true
  104. });
  105. },
  106. // 查询选中列值
  107. selectColumns: function(column) {
  108. return $.map($('#bootstrap-table').bootstrapTable('getSelections'), function (row) {
  109. return row[column];
  110. });
  111. },
  112. // 查询选中首列值
  113. selectFirstColumns: function() {
  114. return $.map($('#bootstrap-table').bootstrapTable('getSelections'), function (row) {
  115. return row[$.table._option.columns[1].field];
  116. });
  117. },
  118. // 回显数据字典
  119. selectDictLabel: function(datas, value) {
  120. var actions = [];
  121. $.each(datas, function(index, dict) {
  122. if (dict.dictValue == value) {
  123. actions.push("<span class='badge badge-" + dict.listClass + "'>" + dict.dictLabel + "</span>");
  124. return false;
  125. }
  126. });
  127. return actions.join('');
  128. }
  129. },
  130. // 表格树封装处理
  131. treeTable: {
  132. _option: {},
  133. // 初始化表格
  134. init: function(options) {
  135. $.table._option = options;
  136. _striped = $.common.isEmpty(options.striped) ? false : options.striped;
  137. _expandColumn = $.common.isEmpty(options.expandColumn) ? '1' : options.expandColumn;
  138. var treeTable = $('#bootstrap-table').bootstrapTreeTable({
  139. code: options.code, // 用于设置父子关系
  140. parentCode: options.parentCode, // 用于设置父子关系
  141. type: 'get', // 请求方式(*)
  142. url: options.url, // 请求后台的URL(*)
  143. ajaxParams: {}, // 请求数据的ajax的data属性
  144. expandColumn: _expandColumn, // 在哪一列上面显示展开按钮
  145. striped: _striped, // 是否显示行间隔色
  146. bordered: true, // 是否显示边框
  147. toolbar: '#toolbar', // 指定工作栏
  148. showRefresh: $.common.visible(options.showRefresh), // 是否显示刷新按钮
  149. showColumns: $.common.visible(options.showColumns), // 是否显示隐藏某列下拉框
  150. expandAll: $.common.visible(options.expandAll), // 是否全部展开
  151. expandFirst: $.common.visible(options.expandFirst), // 是否默认第一级展开--expandAll为false时生效
  152. columns: options.columns
  153. });
  154. $._treeTable = treeTable;
  155. },
  156. // 条件查询
  157. search: function(formId) {
  158. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  159. var params = {};
  160. $.each($("#" + currentId).serializeArray(), function(i, field) {
  161. params[field.name] = field.value;
  162. });
  163. $._treeTable.bootstrapTreeTable('refresh', params);
  164. },
  165. // 刷新
  166. refresh: function() {
  167. $._treeTable.bootstrapTreeTable('refresh');
  168. },
  169. },
  170. // 表单封装处理
  171. form: {
  172. // 表单重置
  173. reset: function(formId) {
  174. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  175. $("#" + currentId)[0].reset();
  176. },
  177. // 获取选中复选框项
  178. selectCheckeds: function(name) {
  179. var checkeds = "";
  180. $('input:checkbox[name="' + name + '"]:checked').each(function(i) {
  181. if (0 == i) {
  182. checkeds = $(this).val();
  183. } else {
  184. checkeds += ("," + $(this).val());
  185. }
  186. });
  187. return checkeds;
  188. },
  189. // 获取选中下拉框项
  190. selectSelects: function(name) {
  191. var selects = "";
  192. $('#' + name + ' option:selected').each(function (i) {
  193. if (0 == i) {
  194. selects = $(this).val();
  195. } else {
  196. selects += ("," + $(this).val());
  197. }
  198. });
  199. return selects;
  200. }
  201. },
  202. // 弹出层封装处理
  203. modal: {
  204. // 显示图标
  205. icon: function(type) {
  206. var icon = "";
  207. if (type == modal_status.WARNING) {
  208. icon = 0;
  209. } else if (type == modal_status.SUCCESS) {
  210. icon = 1;
  211. } else if (type == modal_status.FAIL) {
  212. icon = 2;
  213. } else {
  214. icon = 3;
  215. }
  216. return icon;
  217. },
  218. // 消息提示
  219. msg: function(content, type) {
  220. if (type != undefined) {
  221. layer.msg(content, { icon: $.modal.icon(type), time: 1000, shift: 5 });
  222. } else {
  223. layer.msg(content);
  224. }
  225. },
  226. // 错误消息
  227. msgError: function(content) {
  228. $.modal.msg(content, modal_status.FAIL);
  229. },
  230. // 成功消息
  231. msgSuccess: function(content) {
  232. $.modal.msg(content, modal_status.SUCCESS);
  233. },
  234. // 警告消息
  235. msgWarning: function(content) {
  236. $.modal.msg(content, modal_status.WARNING);
  237. },
  238. // 弹出提示
  239. alert: function(content, type) {
  240. layer.alert(content, {
  241. icon: $.modal.icon(type),
  242. title: "系统提示",
  243. btn: ['确认'],
  244. btnclass: ['btn btn-primary'],
  245. });
  246. },
  247. // 消息提示并刷新父窗体
  248. msgReload: function(msg, type) {
  249. layer.msg(msg, {
  250. icon: $.modal.icon(type),
  251. time: 500,
  252. shade: [0.1, '#8F8F8F']
  253. },
  254. function() {
  255. $.modal.reload();
  256. });
  257. },
  258. // 错误提示
  259. alertError: function(content) {
  260. $.modal.alert(content, modal_status.FAIL);
  261. },
  262. // 成功提示
  263. alertSuccess: function(content) {
  264. $.modal.alert(content, modal_status.SUCCESS);
  265. },
  266. // 警告提示
  267. alertWarning: function(content) {
  268. $.modal.alert(content, modal_status.WARNING);
  269. },
  270. // 关闭窗体
  271. close: function () {
  272. var index = parent.layer.getFrameIndex(window.name);
  273. parent.layer.close(index);
  274. },
  275. // 确认窗体
  276. confirm: function (content, callBack) {
  277. layer.confirm(content, {
  278. icon: 3,
  279. title: "系统提示",
  280. btn: ['确认', '取消'],
  281. btnclass: ['btn btn-primary', 'btn btn-danger'],
  282. }, function (index) {
  283. layer.close(index);
  284. callBack(true);
  285. });
  286. },
  287. // 弹出层指定宽度
  288. open: function (title, url, width, height) {
  289. //如果是移动端,就使用自适应大小弹窗
  290. if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
  291. width = 'auto';
  292. height = 'auto';
  293. }
  294. if ($.common.isEmpty(title)) {
  295. title = false;
  296. };
  297. if ($.common.isEmpty(url)) {
  298. url = "/404.html";
  299. };
  300. if ($.common.isEmpty(width)) {
  301. width = 800;
  302. };
  303. if ($.common.isEmpty(height)) {
  304. height = ($(window).height() - 50);
  305. };
  306. layer.open({
  307. type: 2,
  308. area: [width + 'px', height + 'px'],
  309. fix: false,
  310. //不固定
  311. maxmin: true,
  312. shade: 0.3,
  313. title: title,
  314. content: url,
  315. btn: ['确定', '关闭'],
  316. // 弹层外区域关闭
  317. shadeClose: true,
  318. yes: function(index, layero) {
  319. var iframeWin = layero.find('iframe')[0];
  320. iframeWin.contentWindow.submitHandler();
  321. },
  322. cancel: function(index) {
  323. return true;
  324. }
  325. });
  326. },
  327. // 弹出层指定参数选项
  328. openOptions: function (options) {
  329. var _url = $.common.isEmpty(options.url) ? "/404.html" : options.url;
  330. var _title = $.common.isEmpty(options.title) ? "系统窗口" : options.title;
  331. var _width = $.common.isEmpty(options.width) ? "800" : options.width;
  332. var _height = $.common.isEmpty(options.height) ? ($(window).height() - 50) : options.height;
  333. layer.open({
  334. type: 2,
  335. maxmin: true,
  336. shade: 0.3,
  337. title: _title,
  338. fix: false,
  339. area: [_width + 'px', _height + 'px'],
  340. content: _url,
  341. shadeClose: true,
  342. btn: ['<i class="fa fa-check"></i> 确认', '<i class="fa fa-close"></i> 关闭'],
  343. yes: function (index, layero) {
  344. options.callBack(index, layero)
  345. }, cancel: function () {
  346. return true;
  347. }
  348. });
  349. },
  350. // 弹出层全屏
  351. openFull: function (title, url, width, height) {
  352. //如果是移动端,就使用自适应大小弹窗
  353. if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
  354. width = 'auto';
  355. height = 'auto';
  356. }
  357. if ($.common.isEmpty(title)) {
  358. title = false;
  359. };
  360. if ($.common.isEmpty(url)) {
  361. url = "/404.html";
  362. };
  363. if ($.common.isEmpty(width)) {
  364. width = 800;
  365. };
  366. if ($.common.isEmpty(height)) {
  367. height = ($(window).height() - 50);
  368. };
  369. var index = layer.open({
  370. type: 2,
  371. area: [width + 'px', height + 'px'],
  372. fix: false,
  373. //不固定
  374. maxmin: true,
  375. shade: 0.3,
  376. title: title,
  377. content: url,
  378. // 弹层外区域关闭
  379. shadeClose: true
  380. });
  381. layer.full(index);
  382. },
  383. // 打开遮罩层
  384. loading: function (message) {
  385. $.blockUI({ message: '<div class="loaderbox"><div class="loading-activity"></div> ' + message + '</div>' });
  386. },
  387. // 关闭遮罩层
  388. closeLoading: function () {
  389. setTimeout(function(){
  390. $.unblockUI();
  391. }, 50);
  392. },
  393. // 重新加载
  394. reload: function () {
  395. parent.location.reload();
  396. }
  397. },
  398. // 操作封装处理
  399. operate: {
  400. // 提交数据
  401. submit: function(url, type, dataType, data) {
  402. $.modal.loading("正在处理中,请稍后...");
  403. var config = {
  404. url: url,
  405. type: type,
  406. dataType: dataType,
  407. data: data,
  408. success: function(result) {
  409. $.operate.ajaxSuccess(result);
  410. }
  411. };
  412. $.ajax(config)
  413. },
  414. // post请求传输
  415. post: function(url, data) {
  416. $.operate.submit(url, "post", "json", data);
  417. },
  418. // 详细信息
  419. detail: function(id) {
  420. var _url = $.common.isEmpty(id) ? $.table._option.detailUrl : $.table._option.detailUrl.replace("{id}", id);
  421. layer.open({
  422. type: 2,
  423. area: ['800px', ($(window).height() - 50) + 'px'],
  424. fix: false,
  425. //不固定
  426. maxmin: true,
  427. shade: 0.3,
  428. title: $.table._option.modalName + "详细",
  429. content: _url,
  430. btn: ['<i class="fa fa-close"></i> 关闭'],
  431. // 弹层外区域关闭
  432. shadeClose: true,
  433. cancel: function(index) {
  434. return true;
  435. }
  436. });
  437. },
  438. // 删除信息
  439. remove: function(id) {
  440. $.modal.confirm("确定删除该条" + $.table._option.modalName + "信息吗?", function() {
  441. var url = $.common.isEmpty(id) ? $.table._option.removeUrl : $.table._option.removeUrl.replace("{id}", id);
  442. var data = { "ids": id };
  443. $.operate.submit(url, "post", "json", data);
  444. });
  445. },
  446. // 批量删除信息
  447. removeAll: function() {
  448. var rows = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);
  449. if (rows.length == 0) {
  450. $.modal.alertWarning("请至少选择一条记录");
  451. return;
  452. }
  453. $.modal.confirm("确认要删除选中的" + rows.length + "条数据吗?", function() {
  454. var url = $.table._option.removeUrl;
  455. var data = { "ids": rows.join() };
  456. $.operate.submit(url, "post", "json", data);
  457. });
  458. },
  459. // 清空信息
  460. clean: function() {
  461. $.modal.confirm("确定清空所有" + $.table._option.modalName + "吗?", function() {
  462. var url = $.table._option.cleanUrl;
  463. $.operate.submit(url, "post", "json", "");
  464. });
  465. },
  466. // 添加信息
  467. add: function(id) {
  468. var url = $.common.isEmpty(id) ? $.table._option.createUrl : $.table._option.createUrl.replace("{id}", id);
  469. $.modal.open("添加" + $.table._option.modalName, url);
  470. },
  471. // 修改信息
  472. edit: function(id) {
  473. var url = "/404.html";
  474. if ($.common.isNotEmpty(id)) {
  475. url = $.table._option.updateUrl.replace("{id}", id);
  476. } else {
  477. var id = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);
  478. if (id.length == 0) {
  479. $.modal.alertWarning("请至少选择一条记录");
  480. return;
  481. }
  482. url = $.table._option.updateUrl.replace("{id}", id);
  483. }
  484. $.modal.open("修改" + $.table._option.modalName, url);
  485. },
  486. // 工具栏表格树修改
  487. editTree: function() {
  488. var row = $('#bootstrap-table').bootstrapTreeTable('getSelections')[0];
  489. if ($.common.isEmpty(row)) {
  490. $.modal.alertWarning("请至少选择一条记录");
  491. return;
  492. }
  493. var url = $.table._option.updateUrl.replace("{id}", row[$.table._option.uniqueId]);
  494. $.modal.open("修改" + $.table._option.modalName, url);
  495. },
  496. // 添加信息 全屏
  497. addFull: function(id) {
  498. var url = $.common.isEmpty(id) ? $.table._option.createUrl : $.table._option.createUrl.replace("{id}", id);
  499. $.modal.openFull("添加" + $.table._option.modalName, url);
  500. },
  501. // 修改信息 全屏
  502. editFull: function(id) {
  503. var url = "/404.html";
  504. if ($.common.isNotEmpty(id)) {
  505. url = $.table._option.updateUrl.replace("{id}", id);
  506. } else {
  507. var row = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);
  508. url = $.table._option.updateUrl.replace("{id}", row);
  509. }
  510. $.modal.openFull("修改" + $.table._option.modalName, url);
  511. },
  512. // 保存信息
  513. save: function(url, data) {
  514. $.modal.loading("正在处理中,请稍后...");
  515. var config = {
  516. url: url,
  517. type: "post",
  518. dataType: "json",
  519. data: data,
  520. success: function(result) {
  521. $.operate.saveSuccess(result);
  522. }
  523. };
  524. $.ajax(config)
  525. },
  526. // 保存结果弹出msg刷新table表格
  527. ajaxSuccess: function (result) {
  528. if (result.code == web_status.SUCCESS) {
  529. $.modal.msgSuccess(result.msg);
  530. $.table.refresh();
  531. } else {
  532. $.modal.alertError(result.msg);
  533. }
  534. $.modal.closeLoading();
  535. },
  536. // 保存结果提示msg
  537. saveSuccess: function (result) {
  538. if (result.code == web_status.SUCCESS) {
  539. $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
  540. } else {
  541. $.modal.alertError(result.msg);
  542. }
  543. $.modal.closeLoading();
  544. }
  545. },
  546. // 校验封装处理
  547. validate: {
  548. // 判断返回标识是否唯一 false 不存在 true 存在
  549. unique: function (value) {
  550. if (value == "0") {
  551. return true;
  552. }
  553. return false;
  554. },
  555. // 表单验证
  556. form: function (formId) {
  557. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  558. return $("#" + currentId).validate().form();
  559. }
  560. },
  561. // 树插件封装处理
  562. tree: {
  563. _option: {},
  564. _lastValue: {},
  565. // 初始化树结构
  566. init: function(options) {
  567. $.tree._option = options;
  568. // 属性ID
  569. var _id = $.common.isEmpty(options.id) ? "tree" : options.id;
  570. // 展开等级节点
  571. var _expandLevel = $.common.isEmpty(options.expandLevel) ? 0 : options.expandLevel;
  572. // 树结构初始化加载
  573. var setting = {
  574. check: options.check,
  575. view: { selectedMulti: false, nameIsHTML: true },
  576. data: { key: { title: "title" }, simpleData: { enable: true } },
  577. callback: { onClick: options.onClick }
  578. };
  579. $.get(options.url, function(data) {
  580. var treeName = $("#treeName").val();
  581. var treeId = $("#treeId").val();
  582. tree = $.fn.zTree.init($("#" + _id), setting, data);
  583. $._tree = tree;
  584. // 展开第一级节点
  585. var nodes = tree.getNodesByParam("level", 0);
  586. for (var i = 0; i < nodes.length; i++) {
  587. if(_expandLevel > 0) {
  588. tree.expandNode(nodes[i], true, false, false);
  589. }
  590. $.tree.selectByIdName(treeId, treeName, nodes[i]);
  591. }
  592. // 展开第二级节点
  593. nodes = tree.getNodesByParam("level", 1);
  594. for (var i = 0; i < nodes.length; i++) {
  595. if(_expandLevel > 1) {
  596. tree.expandNode(nodes[i], true, false, false);
  597. }
  598. $.tree.selectByIdName(treeId, treeName, nodes[i]);
  599. }
  600. // 展开第三级节点
  601. nodes = tree.getNodesByParam("level", 2);
  602. for (var i = 0; i < nodes.length; i++) {
  603. if(_expandLevel > 2) {
  604. tree.expandNode(nodes[i], true, false, false);
  605. }
  606. $.tree.selectByIdName(treeId, treeName, nodes[i]);
  607. }
  608. }, null, null, "正在加载,请稍后...");
  609. },
  610. // 搜索节点
  611. searchNode: function() {
  612. // 取得输入的关键字的值
  613. var value = $.common.trim($("#keyword").val());
  614. if ($.tree._lastValue === value) {
  615. return;
  616. }
  617. // 保存最后一次搜索名称
  618. $.tree._lastValue = value;
  619. var nodes = $._tree.getNodes();
  620. // 如果要查空字串,就退出不查了。
  621. if (value == "") {
  622. $.tree.showAllNode(nodes);
  623. return;
  624. }
  625. $.tree.hideAllNode(nodes);
  626. // 根据搜索值模糊匹配
  627. $.tree.updateNodes($._tree.getNodesByParamFuzzy("name", value));
  628. },
  629. // 根据Id和Name选中指定节点
  630. selectByIdName: function(treeId, treeName, node) {
  631. if ($.common.isNotEmpty(treeName) && $.common.isNotEmpty(treeId)) {
  632. if (treeId == node.id && treeName == node.name) {
  633. $._tree.selectNode(node, true);
  634. }
  635. }
  636. },
  637. // 显示所有节点
  638. showAllNode: function(nodes) {
  639. nodes = $._tree.transformToArray(nodes);
  640. for (var i = nodes.length - 1; i >= 0; i--) {
  641. if (nodes[i].getParentNode() != null) {
  642. $._tree.expandNode(nodes[i], true, false, false, false);
  643. } else {
  644. $._tree.expandNode(nodes[i], true, true, false, false);
  645. }
  646. $._tree.showNode(nodes[i]);
  647. $.tree.showAllNode(nodes[i].children);
  648. }
  649. },
  650. // 隐藏所有节点
  651. hideAllNode: function(nodes) {
  652. var tree = $.fn.zTree.getZTreeObj("tree");
  653. var nodes = $._tree.transformToArray(nodes);
  654. for (var i = nodes.length - 1; i >= 0; i--) {
  655. $._tree.hideNode(nodes[i]);
  656. }
  657. },
  658. // 显示所有父节点
  659. showParent: function(treeNode) {
  660. var parentNode;
  661. while ((parentNode = treeNode.getParentNode()) != null) {
  662. $._tree.showNode(parentNode);
  663. $._tree.expandNode(parentNode, true, false, false);
  664. treeNode = parentNode;
  665. }
  666. },
  667. // 显示所有孩子节点
  668. showChildren: function(treeNode) {
  669. if (treeNode.isParent) {
  670. for (var idx in treeNode.children) {
  671. var node = treeNode.children[idx];
  672. $._tree.showNode(node);
  673. $.tree.showChildren(node);
  674. }
  675. }
  676. },
  677. // 更新节点状态
  678. updateNodes: function(nodeList) {
  679. $._tree.showNodes(nodeList);
  680. for (var i = 0, l = nodeList.length; i < l; i++) {
  681. var treeNode = nodeList[i];
  682. $.tree.showChildren(treeNode);
  683. $.tree.showParent(treeNode)
  684. }
  685. },
  686. // 获取当前被勾选集合
  687. getCheckedNodes: function(column) {
  688. var _column = $.common.isEmpty(column) ? "id" : column;
  689. var nodes = $._tree.getCheckedNodes(true);
  690. return $.map(nodes, function (row) {
  691. return row[_column];
  692. }).join();
  693. },
  694. // 不允许根父节点选择
  695. notAllowParents: function(_tree) {
  696. var nodes = _tree.getSelectedNodes();
  697. for (var i = 0; i < nodes.length; i++) {
  698. if (nodes[i].level == 0) {
  699. $.modal.msgError("不能选择根节点(" + nodes[i].name + ")");
  700. return false;
  701. }
  702. if (nodes[i].isParent) {
  703. $.modal.msgError("不能选择父节点(" + nodes[i].name + ")");
  704. return false;
  705. }
  706. }
  707. return true;
  708. },
  709. // 隐藏/显示搜索栏
  710. toggleSearch: function() {
  711. $('#search').slideToggle(200);
  712. $('#btnShow').toggle();
  713. $('#btnHide').toggle();
  714. $('#keyword').focus();
  715. },
  716. // 折叠
  717. collapse: function() {
  718. $._tree.expandAll(false);
  719. },
  720. // 展开
  721. expand: function() {
  722. $._tree.expandAll(true);
  723. }
  724. },
  725. // 通用方法封装处理
  726. common: {
  727. // 判断字符串是否为空
  728. isEmpty: function (value) {
  729. if (value == null || this.trim(value) == "") {
  730. return true;
  731. }
  732. return false;
  733. },
  734. // 判断一个字符串是否为非空串
  735. isNotEmpty: function (value) {
  736. return !$.common.isEmpty(value);
  737. },
  738. // 是否显示数据 为空默认为显示
  739. visible: function (value) {
  740. if ($.common.isEmpty(value) || value == true) {
  741. return true;
  742. }
  743. return false;
  744. },
  745. // 空格截取
  746. trim: function (value) {
  747. if (value == null) {
  748. return "";
  749. }
  750. return value.toString().replace(/(^\s*)|(\s*$)|\r|\n/g, "");
  751. },
  752. // 指定随机数返回
  753. random: function (min, max) {
  754. return Math.floor((Math.random() * max) + min);
  755. }
  756. }
  757. });
  758. })(jQuery);
  759. /** 消息状态码 */
  760. web_status = {
  761. SUCCESS: 0,
  762. FAIL: 500
  763. };
  764. /** 弹窗状态码 */
  765. modal_status = {
  766. SUCCESS: "success",
  767. FAIL: "error",
  768. WARNING: "warning"
  769. };