Skip to content

Commit d4cac1d

Browse files
committed
优化模糊搜索
1 parent 1100089 commit d4cac1d

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/search-tree.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export default {
211211
},
212212
_getLdqTree (tree) { // 获取关键词索引并排序
213213
const { name, children } = this.defaultProps
214-
tree.forEach(item => {
214+
for (let item of tree) {
215215
if (this._search) {
216216
const keys = getDictionary(item[name], this._search)
217217
item.$keys = keys
@@ -230,7 +230,7 @@ export default {
230230
item.expand = this.defaultExpandAll || this.defaultExpandedKeys.indexOf(item[this.nodeKey]) > -1
231231
if (item[children].length) item[children] = this._getLdqTree(item[children])
232232
}
233-
})
233+
}
234234
return getSortData(tree)
235235
},
236236
_updateChecked (data, checked) { // 更新当前节点的状态

src/utils.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export const getSortData = arr => {
1818
return usable.concat(disable)
1919
}
2020
// 反推字典表
21-
export const getDictionary = (name, word) => {
22-
word = word.replace(/\s*/g, '')
21+
export const getDictionary = (text, input) => {
22+
let words = input.trim().split(' ')
2323
let res = []
2424
const dfs = word => {
2525
let keys = [], len = word.length
@@ -29,20 +29,25 @@ export const getDictionary = (name, word) => {
2929
}
3030
}
3131
let start = 0, end = 0, index = 0, step = 0
32-
if (!keys.some(item => {
33-
index = name.indexOf(item)
34-
if (index === -1) return false
35-
if (res.length && res.indexOf(index) > -1) return false
36-
start = word.indexOf(item)
37-
end = start + item.length
38-
step = index + item.length
39-
return true
40-
})) return false
32+
let flag = true
33+
for (let key of keys) {
34+
index = text.indexOf(key)
35+
if (index === -1) continue
36+
if (res.length && res.indexOf(index) > -1) continue
37+
start = word.indexOf(key)
38+
end = start + key.length
39+
step = index + key.length
40+
flag = false
41+
break
42+
}
43+
if (flag) return
4144
while (step > index) res.push(index++)
4245
if (start - 0) dfs(word.slice(0, start))
4346
if (end - len) dfs(word.slice(end, len))
4447
}
45-
dfs(word)
48+
for (let word of words) {
49+
dfs(word)
50+
}
4651
return res
4752
}
4853
// 深拷贝 (这个随便复制的)

0 commit comments

Comments
 (0)