作者 若依
提交者 Gitee

!57 若干 ruoyi-ui 前端公共工具函数优化

Merge pull request !57 from FungLeo/master
@@ -37,6 +37,7 @@ nbdist/ @@ -37,6 +37,7 @@ nbdist/
37 # Others 37 # Others
38 *.log 38 *.log
39 *.xml.versionsBackup 39 *.xml.versionsBackup
  40 +*.swp
40 41
41 !*/build/*.java 42 !*/build/*.java
42 !*/build/*.html 43 !*/build/*.html
@@ -54,42 +54,29 @@ export function resetForm(refName) { @@ -54,42 +54,29 @@ export function resetForm(refName) {
54 } 54 }
55 55
56 // 添加日期范围 56 // 添加日期范围
57 -export function addDateRange(params, dateRange) {  
58 - var search = params;  
59 - search.beginTime = "";  
60 - search.endTime = "";  
61 - if (null != dateRange && '' != dateRange) {  
62 - search.beginTime = this.dateRange[0];  
63 - search.endTime = this.dateRange[1]; 57 +export function addDateRange (params = {}, dateRange) {
  58 + if (dateRange != null && dateRange !== '') {
  59 + params.beginTime = this.dateRange[0]
  60 + params.endTime = this.dateRange[1]
64 } 61 }
65 - return search; 62 + return params
66 } 63 }
67 64
68 // 回显数据字典 65 // 回显数据字典
69 -export function selectDictLabel(datas, value) {  
70 - var actions = [];  
71 - Object.keys(datas).some((key) => {  
72 - if (datas[key].dictValue == ('' + value)) {  
73 - actions.push(datas[key].dictLabel);  
74 - return true;  
75 - }  
76 - })  
77 - return actions.join(''); 66 +export function selectDictLabel(datas = [], value = '') {
  67 + if (!value) return '-';
  68 + const dataArr = datas.filter(item => item.dictValue === value.toString());
  69 + return dataArr.length ? dataArr[0].dictLabel : 'Error Dict';
78 } 70 }
79 71
80 // 回显数据字典(字符串数组) 72 // 回显数据字典(字符串数组)
81 -export function selectDictLabels(datas, value, separator) {  
82 - var actions = [];  
83 - var currentSeparator = undefined === separator ? "," : separator;  
84 - var temp = value.split(currentSeparator);  
85 - Object.keys(value.split(currentSeparator)).some((val) => {  
86 - Object.keys(datas).some((key) => {  
87 - if (datas[key].dictValue == ('' + temp[val])) {  
88 - actions.push(datas[key].dictLabel + currentSeparator);  
89 - }  
90 - }) 73 +export function selectDictLabels(datas = [], value = '', separator = ',') {
  74 + const actions = [];
  75 + const temp = value.split(separator).filter(item => item);
  76 + temp.forEach((_, index) => {
  77 + actions.push(selectDictLabel(datas, temp[index]));
91 }) 78 })
92 - return actions.join('').substring(0, actions.join('').length - 1); 79 + return actions.join(separator);
93 } 80 }
94 81
95 // 通用下载方法 82 // 通用下载方法
@@ -127,23 +114,19 @@ export function praseStrEmpty(str) { @@ -127,23 +114,19 @@ export function praseStrEmpty(str) {
127 * @param {*} children 孩子节点字段 默认 'children' 114 * @param {*} children 孩子节点字段 默认 'children'
128 * @param {*} rootId 根Id 默认 0 115 * @param {*} rootId 根Id 默认 0
129 */ 116 */
130 -export function handleTree(data, id, parentId, children, rootId) {  
131 - id = id || 'id'  
132 - parentId = parentId || 'parentId'  
133 - children = children || 'children'  
134 - rootId = rootId || 0 117 +export function handleTree(data = [], id = 'id', parentId = 'parentId', children = 'children', rootId = 0) {
135 //对源数据深度克隆 118 //对源数据深度克隆
136 const cloneData = JSON.parse(JSON.stringify(data)) 119 const cloneData = JSON.parse(JSON.stringify(data))
137 //循环所有项 120 //循环所有项
138 const treeData = cloneData.filter(father => { 121 const treeData = cloneData.filter(father => {
139 - let branchArr = cloneData.filter(child => { 122 + const branchArr = cloneData.filter(child => {
140 //返回每一项的子级数组 123 //返回每一项的子级数组
141 return father[id] === child[parentId] 124 return father[id] === child[parentId]
142 }); 125 });
143 - branchArr.length > 0 ? father.children = branchArr : ''; 126 + branchArr.length && (father.children = branchArr);
144 //返回第一层 127 //返回第一层
145 return father[parentId] === rootId; 128 return father[parentId] === rootId;
146 }); 129 });
147 - return treeData != '' ? treeData : data; 130 + return treeData !== '' ? treeData : data;
148 } 131 }
149 132