ant-design-vue/components/tree/demo/search.md

123 lines
2.9 KiB
Markdown
Raw Normal View History

2018-04-13 10:58:58 +00:00
<cn>
#### 可搜索
可搜索的树。
</cn>
<us>
#### Searchable
Searchable Tree.
</us>
2019-10-09 10:32:23 +00:00
```tpl
2018-04-13 10:58:58 +00:00
<template>
2018-04-14 13:09:35 +00:00
<div>
<a-input-search style="margin-bottom: 8px" placeholder="Search" @change="onChange" />
<a-tree
@expand="onExpand"
:expandedKeys="expandedKeys"
:autoExpandParent="autoExpandParent"
2018-09-28 06:35:26 +00:00
:treeData="gData"
2018-04-14 13:09:35 +00:00
>
2018-09-28 06:35:26 +00:00
<template slot="title" slot-scope="{title}">
<span v-if="title.indexOf(searchValue) > -1">
{{title.substr(0, title.indexOf(searchValue))}}
2018-04-14 13:09:35 +00:00
<span style="color: #f50">{{searchValue}}</span>
2018-09-28 06:35:26 +00:00
{{title.substr(title.indexOf(searchValue) + searchValue.length)}}
2018-04-14 13:09:35 +00:00
</span>
2018-09-28 06:35:26 +00:00
<span v-else>{{title}}</span>
2018-04-14 13:09:35 +00:00
</template>
</a-tree>
</div>
2018-04-13 10:58:58 +00:00
</template>
2018-04-14 13:09:35 +00:00
2018-04-13 10:58:58 +00:00
<script>
2019-09-28 12:45:07 +00:00
const x = 3;
const y = 2;
const z = 1;
const gData = [];
2018-04-14 13:09:35 +00:00
2019-09-28 12:45:07 +00:00
const generateData = (_level, _preKey, _tns) => {
const preKey = _preKey || '0';
const tns = _tns || gData;
2018-04-13 10:58:58 +00:00
2019-09-28 12:45:07 +00:00
const children = [];
for (let i = 0; i < x; i++) {
const key = `${preKey}-${i}`;
tns.push({ title: key, key, scopedSlots: { title: 'title' } });
if (i < y) {
children.push(key);
}
2018-04-14 13:09:35 +00:00
}
2019-09-28 12:45:07 +00:00
if (_level < 0) {
return tns;
2018-04-14 13:09:35 +00:00
}
2019-09-28 12:45:07 +00:00
const level = _level - 1;
children.forEach((key, index) => {
tns[index].children = [];
return generateData(level, key, tns[index].children);
});
};
generateData(z);
2018-04-14 13:09:35 +00:00
2019-09-28 12:45:07 +00:00
const dataList = [];
const generateList = data => {
for (let i = 0; i < data.length; i++) {
const node = data[i];
const key = node.key;
dataList.push({ key, title: key });
if (node.children) {
generateList(node.children, node.key);
2018-04-14 13:09:35 +00:00
}
}
2019-09-28 12:45:07 +00:00
};
generateList(gData);
const getParentKey = (key, tree) => {
let parentKey;
for (let i = 0; i < tree.length; i++) {
const node = tree[i];
if (node.children) {
if (node.children.some(item => item.key === key)) {
parentKey = node.key;
} else if (getParentKey(key, node.children)) {
parentKey = getParentKey(key, node.children);
2018-04-14 13:09:35 +00:00
}
2019-09-28 12:45:07 +00:00
}
}
return parentKey;
};
export default {
data() {
return {
expandedKeys: [],
searchValue: '',
2018-04-14 13:09:35 +00:00
autoExpandParent: true,
2019-09-28 12:45:07 +00:00
gData,
};
2018-04-14 13:09:35 +00:00
},
2019-09-28 12:45:07 +00:00
methods: {
onExpand(expandedKeys) {
this.expandedKeys = expandedKeys;
this.autoExpandParent = false;
},
onChange(e) {
const value = e.target.value;
const expandedKeys = dataList
.map(item => {
if (item.key.indexOf(value) > -1) {
return getParentKey(item.key, gData);
}
return null;
})
.filter((item, i, self) => item && self.indexOf(item) === i);
Object.assign(this, {
expandedKeys,
searchValue: value,
autoExpandParent: true,
});
},
},
};
2018-04-13 10:58:58 +00:00
</script>
```