vue3+elementplus 樹(shù)節(jié)點(diǎn)過(guò)濾功能實(shí)現(xiàn)
樹(shù)節(jié)點(diǎn)所展示的街道是讀取的成都市金牛區(qū)的范圍邊界線的json文件,街道下對(duì)應(yīng)內(nèi)容市通過(guò)fetch調(diào)用接口獲取的內(nèi)容,通過(guò)mapTreeData函數(shù)循環(huán)遍歷,對(duì)數(shù)據(jù)進(jìn)行處理,最后返回樹(shù)節(jié)點(diǎn)所需要展示的格式,然后elemenplus封裝好的樹(shù)節(jié)點(diǎn)過(guò)濾方法進(jìn)行過(guò)濾,因?yàn)閑lemenplus封裝好的樹(shù)節(jié)點(diǎn)過(guò)濾對(duì)匹配對(duì)應(yīng)的父節(jié)點(diǎn)后不會(huì)展示下面的子節(jié)點(diǎn)內(nèi)容,所以用chooseNode方法,如果輸入的參數(shù)是父節(jié)點(diǎn)且能匹配,則返回該節(jié)點(diǎn)以及其下的所有子節(jié)點(diǎn);如果參數(shù)是子節(jié)點(diǎn),則返回該節(jié)點(diǎn)的父節(jié)點(diǎn)。name是中文字符,enName是英文字符
<marsDialog left="10" top="50" width="370">
<p>數(shù)據(jù)管理</p>
<div>
<el-input v-model="filterText" style="width: 240px" placeholder="模糊搜索" @input="inputHanlder" />
<!-- filter-node-method對(duì)樹(shù)節(jié)點(diǎn)進(jìn)行篩選時(shí)執(zhí)行的方法, 返回 false 則表示這個(gè)節(jié)點(diǎn)會(huì)被隱藏 -->
<el-scrollbar class="scroll-container">
<div class="scroll-content">
<el-tree ref="treeRef" style="max-width: 600px" class="filter-tree" :data="_data" :props="defaultProps"
default-expand-all :filter-node-method="filterNode" @node-click="nodeClick" />
</div>
</el-scrollbar>
</div>
</marsDialog>interface Tree {
[key: string]: any
}
const inputHanlder = (_v: any) => {
// console.log(jdJson.features);
}
const filterText = ref('')
const treeRef = ref<InstanceType<typeof ElTree>>();
const defaultProps = {
children: 'children',
label: 'label',
}
let _allData = reactive({ data: [] });
let _data = ref([])
fetch('xxxxxxx', {
method: 'GET',
headers: {
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzeXNfaWQiOjEsInVpZCI6ImFlMjllODkzODhlMzkyMjljNGE0ZDJkY2U4MTQyZWU4IiwibmFtZSI6ImFkbWluIiwicGFzc3dvcmQiOiIiLCJyZWdpc3RlcnRpbWUiOiIyMDIzLzQvMjcgMTc6MjE6MzEiLCJuaWNrbmFtZSI6ImFkbWluIiwiZW1haWwiOiIyNTU4NkB3ZWxsY2h5Lm5ldCIsInBpYyI6IiIsImlhdCI6MTcxNDk4MjczMywiZXhwIjoxNzE1MDY5MTMzfQ.K-NKPvKCWWLlU2nZ1-dmhkhJI7aDxDtEDcbZUrOcOB0'
}
}).then(response => response.json())
.then(data => {
_allData.data.length = 0;
_allData.data.push(...data.message);
// 在數(shù)據(jù)填充后調(diào)用mapTreeData函數(shù)
let treeData = [
{
id: 1,
label: '金牛區(qū)',
children: mapTreeData(jdJson, _allData.data)
},
];
_data.value = treeData
})
.catch(error => console.log(error));
const mapTreeData = (data: any, allData: any[]): Tree[] => {
return data.features.map((feature: any, index: number) => {
const districts = allData.filter((district) => feature.properties.Name === district.streetOffice);
const children = districts.map((district) => ({ label: district.name, position: [[district.longitude, district.latitude,500]] }));
return {
id: index + 2,
label: feature.properties.Name,
position: feature.geometry.coordinates,
children: children || []
};
});
};// 監(jiān)聽(tīng)輸入框內(nèi)容的變化,以便執(zhí)行篩選操作
watch(filterText, (val) => {
// 調(diào)用 el-tree 組件的 filter 方法,根據(jù)輸入的值來(lái)篩選節(jié)點(diǎn)
treeRef.value!.filter(val)
})
// 自定義篩選方法,用于決定樹(shù)節(jié)點(diǎn)是否應(yīng)該在篩選結(jié)果中顯示
const filterNode = (value: string, data: Tree, node: any) => {
// 打印篩選值和節(jié)點(diǎn)數(shù)據(jù),用于調(diào)試
console.log(value); //打印的是篩選操作中用戶輸入的值。在這段代碼中,value 是傳遞給 filterNode 方法的第一個(gè)參數(shù),表示用戶輸入的篩選值。
console.log(data); //打印的是當(dāng)前樹(shù)節(jié)點(diǎn)的數(shù)據(jù)。在這段代碼中,data 是傳遞給 filterNode 方法的第二個(gè)參數(shù),表示當(dāng)前被篩選的節(jié)點(diǎn)的數(shù)據(jù),包括標(biāo)簽等信息。
/**
* 這行代碼是用來(lái)處理當(dāng)用戶沒(méi)有輸入篩選值時(shí)的情況。
在樹(shù)形組件中,如果用戶沒(méi)有輸入任何篩選值,那么我們希望所有的節(jié)點(diǎn)都能夠顯示,
而不應(yīng)該進(jìn)行任何篩選操作。因此,當(dāng)用戶沒(méi)有輸入篩選值時(shí),我們直接返回 true,表示當(dāng)前節(jié)點(diǎn)應(yīng)該被顯示在篩選結(jié)果中。
*/
if (!value) return true
// 如果節(jié)點(diǎn)的標(biāo)簽包含篩選值,則返回 true,表示節(jié)點(diǎn)應(yīng)該顯示
// return data.label.includes(value)
if (data.label) {
return chooseNode(value, data, node);
}
}
// 過(guò)濾父節(jié)點(diǎn) / 子節(jié)點(diǎn) (如果輸入的參數(shù)是父節(jié)點(diǎn)且能匹配,則返回該節(jié)點(diǎn)以及其下的所有子節(jié)點(diǎn);如果參數(shù)是子節(jié)點(diǎn),則返回該節(jié)點(diǎn)的父節(jié)點(diǎn)。name是中文字符,enName是英文字符.
const chooseNode = (value: string, data: Tree, node: { level: any; parent: any; }) => {
if (data.label.indexOf(value) !== -1) {
return true
}
const level = node.level
// 如果傳入的節(jié)點(diǎn)本身就是一級(jí)節(jié)點(diǎn)就不用校驗(yàn)了
if (level === 1) {
return false
}
// 先取當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)
let parentData = node.parent
// 遍歷當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)
let index = 0
while (index < level - 1) {
// 如果匹配到直接返回,此處name值是中文字符,enName是英文字符。判斷匹配中英文過(guò)濾
if (parentData.data.label.indexOf(value) !== -1) {
return true
}
// 否則的話再往上一層做匹配
parentData = parentData.parent
index++
}
// 沒(méi)匹配到返回false
return false
}
let lastStreetPosition: number | null = null;
const nodeClick = (obj: any) => {
if (obj.label.includes('街道')) {
mapWork.jinNiuJieDaoWall(obj.position[0]);
window.map.flyToPositions(obj.position[0], {
radius: 400,
});
lastStreetPosition = obj.position[0]; // 更新上一次包含街道的位置
} else {
mapWork.jinNiuJieDaoWall(lastStreetPosition);
window.map.flyToPoint(obj.position[0], {
radius: 300,
});
}
}
到此這篇關(guān)于vue3+elementplus 樹(shù)節(jié)點(diǎn)過(guò)濾的文章就介紹到這了,更多相關(guān)vue3樹(shù)節(jié)點(diǎn)過(guò)濾內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue3+Vite+ElementPlus構(gòu)建學(xué)習(xí)筆記
- Vue3通過(guò)JSON渲染ElementPlus表單的流程步驟
- Vue3+ElementPlus封裝圖片空間組件的門(mén)面實(shí)例
- vue3+js+elementPlus使用富文本編輯器@vueup/vue-quill詳細(xì)教程
- vue3基于elementplus 簡(jiǎn)單實(shí)現(xiàn)表格二次封裝過(guò)程
- 如何在Vue3中正確使用ElementPlus,親測(cè)有效,避坑
- vue3+elementplus前端生成圖片驗(yàn)證碼完整代碼舉例
- Vue3 + ElementPlus動(dòng)態(tài)合并數(shù)據(jù)相同的單元格的完整代碼
相關(guān)文章
vue學(xué)習(xí)筆記之vue1.0和vue2.0的區(qū)別介紹
今天我們來(lái)說(shuō)一說(shuō)vue1.0和vue2.0的主要變化有哪些?對(duì)vue相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2017-05-05
Vue3+Ant?design?實(shí)現(xiàn)Select下拉框一鍵全選/清空功能
在做后臺(tái)管理系統(tǒng)項(xiàng)目的時(shí)候,產(chǎn)品增加了一個(gè)在Select選擇器中添加一鍵全選和清空的功能,他又不讓在外部增加按鈕,其實(shí)如果說(shuō)在外部增加按鈕實(shí)現(xiàn)全選或者清空的話,功能比較簡(jiǎn)單的,下面給大家分享Vue3+Ant?design?實(shí)現(xiàn)Select下拉框一鍵全選/清空功能,需要的朋友可以參考下2024-05-05
詳解vue-flickity的fullScreen功能實(shí)現(xiàn)
這篇文章主要介紹了詳解vue-flickity的fullScreen功能實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Vue中Keep-Alive緩存組件使用語(yǔ)法及原理深度解析
keep-alive是vue中的內(nèi)置組件,能在組件切換過(guò)程中將狀態(tài)保留在內(nèi)存中,防止重復(fù)渲染DOM,這篇文章主要介紹了Vue中Keep-Alive緩存組件使用語(yǔ)法及原理,需要的朋友可以參考下2024-07-07
vue如何實(shí)現(xiàn)級(jí)聯(lián)選擇器功能
這篇文章主要介紹了vue如何實(shí)現(xiàn)級(jí)聯(lián)選擇器功能問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
element的el-tree多選樹(shù)(復(fù)選框)父子節(jié)點(diǎn)關(guān)聯(lián)不關(guān)聯(lián)
最近想要實(shí)現(xiàn)多選框關(guān)聯(lián)的功能,但是卻出現(xiàn)了element的el-tree多選樹(shù)(復(fù)選框)父子節(jié)點(diǎn)關(guān)聯(lián)不關(guān)聯(lián)的問(wèn)題,本文就來(lái)介紹一下解決方法,一起來(lái)了解一下2021-05-05
解決vue中使用Axios調(diào)用接口時(shí)出現(xiàn)的ie數(shù)據(jù)處理問(wèn)題
今天小編就為大家分享一篇解決vue中使用Axios調(diào)用接口時(shí)出現(xiàn)的ie數(shù)據(jù)處理問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08

