vue+element樹形選擇器組件封裝和使用方式
更新時間:2020年04月23日 09:59:42 作者:WalkerShen
這篇文章主要介紹了vue+element樹形選擇器組件封裝和使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
vue+element樹形選擇器組件封裝和使用
演示效果


子組件
<template>
<div>
<el-input
v-model="value"
@focus="showTree"
:placeholder="placeholder"
suffix-icon="el-icon-arrow-down"
readonly
/>
<el-tree
v-if="treeShow"
:data="data"
:node-key="nodeKey"
default-expand-all
:props="defaultProps"
accordion
@node-click="handleNodeClick"
>
</el-tree>
</div>
</template>
<script>
export default {
data() {
return {
treeShow: false,
};
},
props: {
// input的值
value: {
type: String,
},
// 提示語
placeholder: {
type: String,
default: "請點擊",
},
// 樹節(jié)點數(shù)據(jù)
data: {
type: Array,
},
// 節(jié)點的key
nodeKey: {
type: String,
default: "id",
},
// 樹的props
defaultProps: {
type: Object,
default: {
children: "children",
label: "name",
},
},
// 方法
handleData: {
type: Function,
},
},
methods: {
// 點擊方法
handleNodeClick(data) {
this.treeShow = false;
this.handleData(data);
},
// 顯示樹
showTree() {
this.treeShow = !this.treeShow;
},
},
// 監(jiān)聽value數(shù)據(jù),解決組件傳值沒實時更新問題
watch: {
value: {
handler: function (v, ov) {
this.value = v;
},
deep: true,
immediate: true,
},
},
};
</script>
<style lang="scss" scoped></style>
父組件
<treeSelect
:value="form.parentName"
placeholder="點擊獲取父級區(qū)域"
:data="treeList"
:defaultProps="defaultProps"
:handleData="receiveData"
></treeSelect>
export default{
//組件引入
components: {
treeSelect: () => import("../components/treeSelect.vue"),
},
//
data(){
return {
form: {},
treeShow: false,
treeList: [],
defaultProps: {
children: "children",
label: "name",
},
}
},
methods:{
receiveData(data) {
console.log("接收數(shù)據(jù):", data);
// 需要使用$set設置
this.$set(this.form, "parentName", data.name);
this.$set(this.form, "parentId", data.id);
},
// 獲取樹的接口
treeRegion() {
let params = {};
treeRegion(params).then((res) => {
this.treeList = res.data;
});
},
}
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
axios無法加載響應數(shù)據(jù):no?data?found?for?resource?with?given?i
最近在在做一個小查詢功能的時候踩了一個坑,所以這篇文章主要給大家介紹了關(guān)于axios無法加載響應數(shù)據(jù):no?data?found?for?resource?with?given?identifier報錯的解決方法,需要的朋友可以參考下2022-11-11
基于vue+element實現(xiàn)全局loading過程詳解
這篇文章主要介紹了基于vue+element實現(xiàn)全局loading過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-07-07
vue 判斷兩個時間插件結(jié)束時間必選大于開始時間的代碼
這篇文章主要介紹了vue 判斷兩個時間插件結(jié)束時間必選大于開始時間的代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
iview中實現(xiàn)this.$Modal.confirm自定義彈出框換行加樣式
這篇文章主要介紹了iview中實現(xiàn)this.$Modal.confirm自定義彈出框換行加樣式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
vue中 process.env與process.VUE_CLI_SERVICE詳解
這篇文章主要介紹了vue中process.env與process.VUE_CLI_SERVICE的相關(guān)資料,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05

