亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Vue動態數據怎么實現el-select多級聯動及數據回顯

發布時間:2022-07-27 10:13:36 來源:億速云 閱讀:278 作者:iii 欄目:開發技術

本篇內容介紹了“Vue動態數據怎么實現el-select多級聯動及數據回顯”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

動態數據 el-select 多級聯動、數據回顯

Vue動態數據怎么實現el-select多級聯動及數據回顯

 父組件

<ProviderCategory v-model="classificationId"></ProviderCategory>
 
import ProviderCategory from './ProviderCategory'
<script>
import ProviderCategory from './ProviderCategory'
export default {
  components: {
        ProviderCategory
  },
  data() {
    return {
      classificationId:111
    }
  },
</script>

子組件 

<template>
    <div>
            <el-select class="form-width-160"  v-for="(item,idx) in selectedListArr.length" :key="idx"
                       v-model="selectedValueArr[idx]"
                       @change="(value)=>changeSelectData(value, idx )"
                       value-key="id"
            >
                <el-option v-for="item of selectedListArr[idx]"
                           :key="`${item.id}${idx}`" :label="item.categoryName"
                           :value="item"></el-option>
            </el-select>
 
    </div>
</template>
<script>
    export default {
        data () {
            return {
                classificationList: [],//一級目錄的數據,其實就是總的數據
                selectedListArr: [],//二維數組,每一級目錄的數據存入當前下標
                selectedValueArr: [],//一維數組,選中數據組成的數組對象
            }
        },
        props:{
            value : ''
        },
        created () {
            console.log(this.value)//父節點傳過來的數據
            this.getGoodsCategoryList()//初始化數據獲取所有的數據
        },
        methods: {
            //this.value如果有數據,調用該方法
            findId(arr1,id){//數據回顯
                var temp = []
                var arrId=[];
                let newArr=[];
                var forFn = function (arr, id) {
                    for (var i = 0; i < arr.length; i++) {
                        var item = arr[i]
                        if (item.id === id) {
                            newArr.unshift(arr);//二維數組,每一級目錄的數據存入當前下標
                            temp.unshift(item);//一維數組,選中數據組成的數組對象
                            arrId.unshift(id);//選中數據的id,包括父id
                            forFn(arr1, item.pId)
                            break
                        } else {
                            if (item.childNodes) {
                                forFn(item.childNodes, id)
                            }
                        }
                    }
                }
                forFn(arr1, id)
                this.selectedListArr = newArr;
                this.selectedValueArr = temp;
                console.log(newArr,"測試數據")
                console.log(temp,"數據")
                console.log(arrId,"id數據")
                // return temp
            },
            //加載數據
            initSelectArr(index){
                if(index == 0) {//當下標為0時,其實就是新增的數據
                    this.selectedListArr = [this.classificationList];
                    this.selectedValueArr=[''];
                }else{//否則對數據進行處理,
                    this.selectedListArr = this.selectedListArr.slice(0, index+1);
                    this.selectedValueArr = this.selectedValueArr.slice(0, index+1);
                }
 
            },
            //選中數據后觸發的方法
            changeSelectData(item, idx) {
                console.log(item, idx);
                this.initSelectArr(idx);
                this.selectedValueArr[idx] = item;
                if(item.childNodes && item.childNodes.length>0){
                    this.selectedListArr.push(item.childNodes);
                    this.selectedValueArr.push('');
                }
            },
            //初始化數據獲取所有的數據
            getGoodsCategoryList() {
                let childNodes = [];//模擬后端傳過來的數據,在下面
                this.classificationList = childNodes;
                this.initSelectArr(0);//新增數據,頁面加載
                if(this.value && this.value !== ''){
                    this.findId(this.classificationList , this.value)
                }
                // this.$http.get('/test/testData', {
                //     params: {}
                // }).then(res => {
                //     res = res.data
                //     if (res.code === 200) {
                //         this.classificationList = res.data.childNodes
                //     } else {
                //         this.$message.error(res.msg)
                //     }
                // }).catch(err => {
                //     console.log(err)
                // })
            }
        }
    }
</script>
 
<style scoped>
 
</style>
//測試模擬數據
childNodes = [
                    {
                        categoryImg: "https://www.baidu.com/img/bd_logo1.png",
                        categoryName: "上衣",
                        id:2,
                        pId: 1,
                        sort: 1,
                        childNodes: [
                            {
                                categoryImg: "https://www.baidu.com/img/bd_logo1.png",
                                categoryName: "短袖",
                                haveGoods: true,
                                id: 5,
                                pId: 2,
                                sort: 1,
                                childNodes:[
                                    {
                                        id:111,
                                        pId: 5,
                                        sort: 1,
                                        categoryImg: "https://www.baidu.com/img/bd_logo1.png",
                                        categoryName: "短袖褲子",
                                        childNodes: []
                                    },
                                    {
                                        id:122,
                                        pId: 5,
                                        sort: 1,
                                        categoryImg: "https://www.baidu.com/img/bd_logo1.png",
                                        categoryName: "短袖鞋子",
                                        childNodes: []
                                    }
                                ],
                            },
                            {
                                categoryImg: "https://www.baidu.com/img/bd_logo1.png",
                                categoryName: "西裝",
                                haveGoods: false,
                                id: 6,
                                pId: 2,
                                sort: 1,
                                childNodes:[
                                    {
                                        id:112,
                                        pId: 6,
                                        sort: 1,
                                        categoryImg: "https://www.baidu.com/img/bd_logo1.png",
                                        categoryName: "西裝褲子",
                                        childNodes: []
                                    },
                                    {
                                        id:121,
                                        pId: 6,
                                        sort: 1,
                                        categoryImg: "https://www.baidu.com/img/bd_logo1.png",
                                        categoryName: "西裝鞋子",
                                        childNodes: []
                                    }
                                ],
                            }
                        ]
                    },
                    {
                        id:11,
                        pId: 1,
                        sort: 1,
                        categoryImg: "https://www.baidu.com/img/bd_logo1.png",
                        categoryName: "褲子",
                        childNodes: [
                            {
                                categoryImg: "https://www.baidu.com/img/bd_logo1.png",
                                categoryName: "牛仔",
                                haveGoods: true,
                                id: 112222,
                                pId: 11,
                                sort: 1,
                                childNodes:[],
                            },
                        ]
                    },
                    {
                        id:12,
                        pId: 1,
                        sort: 1,
                        categoryImg: "https://www.baidu.com/img/bd_logo1.png",
                        categoryName: "鞋子",
                        childNodes: [
                            {
                                categoryImg: "https://www.baidu.com/img/bd_logo1.png",
                                categoryName: "耐克",
                                haveGoods: true,
                                id: 1121,
                                pId: 12,
                                sort: 1,
                                childNodes:[
                                     {
                                        categoryImg: "https://www.baidu.com/img/bd_logo1.png",
                                        categoryName: "西牛仔",
                                        haveGoods: true,
                                        id: 11211,
                                        pId: 1121,
                                        sort: 1,
                                        childNodes:[],
                                    },
                                ],
                            },
                            {
                                categoryImg: "https://www.baidu.com/img/bd_logo1.png",
                                categoryName: "阿迪",
                                haveGoods: true,
                                id: 1122,
                                pId: 12,
                                sort: 1,
                                childNodes:[],
                            },
                        ]
                    }
                ];
        //數據回顯
        findId(arr, id) { //將選中的數組和id組成一個數組
 
            for (let i = 0; i < arr.length; i++) {
                if (arr[i].id === id) {
                    return [
                        [arr, i]
                    ]
                    break
                }
            }
            let c = []
            arr.forEach((item, i) => {
                let r = this.findId(item.childNodes || [], id)
                if (r && r.length) {
                    c = [
                        [arr, i]
                    ].concat(r)
                }
            })
            // console.log(c,"回顯數據")
            return c
        },

多級聯動select菜單(易懂)

<div id="checkbox">
<select v-model="selected"  @change='func()'>
  <option value='' disabled selected style='display:none;'>選擇1</option>
  <option v-for="option in options" v-bind:value="option.value">
    {{ option.text }}
  </option>
</select>
<select name="" id="">
  <option value='' disabled selected style='display:none;'>選擇2</option>
  <option v-for="option in options2" v-bind:value="option.text">
  {{ option.text }}
  </option>
</select>
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
new Vue({
  el: '#checkbox',
  data: {
    selected: '',
    options: [//一級菜單
      { text: 'A', value: 'A' },
      { text: 'B', value: 'B' },
      { text: 'C', value: 'C' }
    ],
    options2:[],//二級菜單
  },
  methods:{
    func:function(){//綁定事件,給二級菜單賦值
      switch(this.selected){
        case 'A':this.options2=[{text:'A-1'},{text:'A-2'},{text:'A-3'}];break;
        case 'B':this.options2=[{text:'B-1'},{text:'B-2'},{text:'B-3'}];break;
        case 'C':this.options2=[{text:'C-1'},{text:'C-2'},{text:'C-3'}];break;
      }
    }
  }
})
</script>

“Vue動態數據怎么實現el-select多級聯動及數據回顯”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

安陆市| 镇雄县| 福贡县| 武汉市| 佛教| 龙江县| 通山县| 镇雄县| 龙南县| 栖霞市| 莱州市| 南投县| 天全县| 新建县| 库车县| 大邑县| 奉化市| 乐山市| 子洲县| 和龙市| 禄丰县| 隆林| 九江县| 增城市| 措勤县| 迁安市| 缙云县| 米易县| 浦北县| 渝中区| 永平县| 九龙县| 长寿区| 文成县| 成安县| 清原| 永德县| 梅州市| 穆棱市| 吴桥县| 洪雅县|