Skip to content

Commit 2ecef53

Browse files
committed
修复一个很奇怪的渲染性能问题
1 parent c3a52a3 commit 2ecef53

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

src/search-node.vue

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ export default {
5050
{
5151
root.$scopedSlots.default ? root.$scopedSlots.default(data) : <p class="tree-text">
5252
{
53-
data.$keys?.length ? data[name].split('').map(
54-
(curr, i) => <span style={{ color: data.$keys.indexOf(i) > -1 ? 'red': '#666' }}>{curr}</span>
55-
) : <span style={{ color: '#666' }}>{data[name]}</span>
53+
data.$keys.length ? data[name].split('').map((curr, i) => {
54+
return <span class={ data.$keys.includes(i) ? 'red': 'gary' }>{curr}</span>
55+
}) : <span class="gary">{data[name]}</span>
5656
}
5757
</p>
5858
}
@@ -141,4 +141,10 @@ export default {
141141
.point {
142142
cursor: pointer;
143143
}
144+
.red {
145+
color: red;
146+
}
147+
.gary {
148+
color: gray;
149+
}
144150
</style>

src/search-tree.vue

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ export default {
6161
type: Array,
6262
default: () => []
6363
},
64-
clearRecovery: { // 清空搜索关键词时, 是否复原排序顺序
65-
type: Boolean,
66-
default: false
67-
},
6864
props: { // 配置项
6965
type: Object,
7066
default: () => {
@@ -104,14 +100,17 @@ export default {
104100
_search (val) {
105101
clearTimeout(this.timer)
106102
this.timer = setTimeout(_ => {
107-
if (val || !this.clearRecovery) return this.deepData = this._getLdqTree(this.deepData)
103+
if (val) return this.deepData = this._getLdqTree(this.deepData)
104+
const keys = this.showCheckbox ? this.getCheckedKeys() : []
108105
/**
109-
* 如果清空搜索关键词时, 开启了复原初始结构 clearRecovery = true 时
110-
* 经测试 4k+ 的单一树节点卡顿在15秒左右, 建议分散处理可以降到2秒左右
106+
* 这里必须先清空数据, 再进行赋值, 不然会产生严重的性能问题
107+
* 很有可能是vue内部对多次赋值操作进行合并所产生的
111108
*/
112-
const keys = this.showCheckbox ? this.getCheckedKeys() : []
113-
this.deepData = deepCopy(this.sourceData)
114-
this.setCheckedByKeys(keys, true)
109+
this.deepData = []
110+
this.$nextTick(_ => {
111+
this.deepData = deepCopy(this.sourceData)
112+
this.setCheckedByKeys(keys, true)
113+
})
115114
}, this.searchDebounce)
116115
}
117116
},
@@ -199,12 +198,12 @@ export default {
199198
},
200199
_initData () { // 初始化数据
201200
const { children } = this.defaultProps
202-
const _deep = (arr, parent) => {
201+
const dfs = (arr, parent) => {
203202
let checkedNum = 0, anyOne = false
204203
arr.forEach(item => {
205204
this._initNode(item, parent)
206205
checkedNum += +item.checked
207-
item[children].length && _deep(item[children], item)
206+
item[children].length && dfs(item[children], item)
208207
item.expand && parent && (parent.expand = true)
209208
if (item.indeterminate) anyOne = true
210209
})
@@ -215,7 +214,7 @@ export default {
215214
parent.indeterminate = anyOne || (!!checkedNum && checkedNum != arr.length) || (!parent.checked && !!this._preorder(arr, item => item.checked))
216215
}
217216
}
218-
_deep(this.sourceData)
217+
dfs(this.sourceData)
219218
const data = deepCopy(this.sourceData)
220219
this.deepData = this._getLdqTree(data)
221220
},

src/utils.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// 计算匹配优先值
22
export const computSortNum = keys => {
3-
keys = JSON.parse(JSON.stringify(keys))
3+
keys = keys.slice(0)
44
let lev = 0, curr = keys.shift()
55
return keys.length < 1 ? +!!(curr + 1) : keys.reduce((s, next, i) => {
66
if (next - curr === 1) lev += 1
@@ -21,7 +21,7 @@ export const getSortData = arr => {
2121
export const getDictionary = (name, word) => {
2222
word = word.replace(/\s*/g, '')
2323
let res = []
24-
const _deep = word => {
24+
const dfs = word => {
2525
let keys = [], len = word.length
2626
for (let i = len; i > 0; i--) {
2727
for (let j = 0; j < len + 1 - i; j++) {
@@ -39,10 +39,10 @@ export const getDictionary = (name, word) => {
3939
return true
4040
})) return false
4141
while (step > index) res.push(index++)
42-
if (start - 0) _deep(word.slice(0, start))
43-
if (end - len) _deep(word.slice(end, len))
42+
if (start - 0) dfs(word.slice(0, start))
43+
if (end - len) dfs(word.slice(end, len))
4444
}
45-
_deep(word)
45+
dfs(word)
4646
return res
4747
}
4848
// 深拷贝 (这个随便复制的)

0 commit comments

Comments
 (0)