import ProviderCategory from './ProviderCategory'
当前位置:   article > 正文

Vue 动态数据实现 el-select 多级联动、数据回显_vue中俩个有联动的el-select数据回显

vue中俩个有联动的el-select数据回显

 

  1. 父组件
  2. <ProviderCategory v-model="classificationId"></ProviderCategory>
  3. import ProviderCategory from './ProviderCategory'
  4. <script>
  5. import ProviderCategory from './ProviderCategory'
  6. export default {
  7. components: {
  8. ProviderCategory
  9. },
  10. data() {
  11. return {
  12. classificationId:111
  13. }
  14. },
  15. </script>
  1. 子组件
  2. <template>
  3. <div>
  4. <el-select class="form-width-160" v-for="(item,idx) in selectedListArr.length" :key="idx"
  5. v-model="selectedValueArr[idx]"
  6. @change="(value)=>changeSelectData(value, idx )"
  7. value-key="id"
  8. >
  9. <el-option v-for="item of selectedListArr[idx]"
  10. :key="`${item.id}${idx}`" :label="item.categoryName"
  11. :value="item"></el-option>
  12. </el-select>
  13. </div>
  14. </template>
  15. <script>
  16. export default {
  17. data () {
  18. return {
  19. classificationList: [],//一级目录的数据,其实就是总的数据
  20. selectedListArr: [],//二维数组,每一级目录的数据存入当前下标
  21. selectedValueArr: [],//一维数组,选中数据组成的数组对象
  22. }
  23. },
  24. props:{
  25. value : ''
  26. },
  27. created () {
  28. console.log(this.value)//父节点传过来的数据
  29. this.getGoodsCategoryList()//初始化数据获取所有的数据
  30. },
  31. methods: {
  32. //this.value如果有数据,调用该方法
  33. findId(arr1,id){//数据回显
  34. var temp = []
  35. var arrId=[];
  36. let newArr=[];
  37. var forFn = function (arr, id) {
  38. for (var i = 0; i < arr.length; i++) {
  39. var item = arr[i]
  40. if (item.id === id) {
  41. newArr.unshift(arr);//二维数组,每一级目录的数据存入当前下标
  42. temp.unshift(item);//一维数组,选中数据组成的数组对象
  43. arrId.unshift(id);//选中数据的id,包括父id
  44. forFn(arr1, item.pId)
  45. break
  46. } else {
  47. if (item.childNodes) {
  48. forFn(item.childNodes, id)
  49. }
  50. }
  51. }
  52. }
  53. forFn(arr1, id)
  54. this.selectedListArr = newArr;
  55. this.selectedValueArr = temp;
  56. console.log(newArr,"测试数据")
  57. console.log(temp,"数据")
  58. console.log(arrId,"id数据")
  59. // return temp
  60. },
  61. //加载数据
  62. initSelectArr(index){
  63. if(index == 0) {//当下标为0时,其实就是新增的数据
  64. this.selectedListArr = [this.classificationList];
  65. this.selectedValueArr=[''];
  66. }else{//否则对数据进行处理,
  67. this.selectedListArr = this.selectedListArr.slice(0, index+1);
  68. this.selectedValueArr = this.selectedValueArr.slice(0, index+1);
  69. }
  70. },
  71. //选中数据后触发的方法
  72. changeSelectData(item, idx) {
  73. console.log(item, idx);
  74. this.initSelectArr(idx);
  75. this.selectedValueArr[idx] = item;
  76. if(item.childNodes && item.childNodes.length>0){
  77. this.selectedListArr.push(item.childNodes);
  78. this.selectedValueArr.push('');
  79. }
  80. },
  81. //初始化数据获取所有的数据
  82. getGoodsCategoryList() {
  83. let childNodes = [];//模拟后端传过来的数据,在下面
  84. this.classificationList = childNodes;
  85. this.initSelectArr(0);//新增数据,页面加载
  86. if(this.value && this.value !== ''){
  87. this.findId(this.classificationList , this.value)
  88. }
  89. // this.$http.get('/test/testData', {
  90. // params: {}
  91. // }).then(res => {
  92. // res = res.data
  93. // if (res.code === 200) {
  94. // this.classificationList = res.data.childNodes
  95. // } else {
  96. // this.$message.error(res.msg)
  97. // }
  98. // }).catch(err => {
  99. // console.log(err)
  100. // })
  101. }
  102. }
  103. }
  104. </script>
  105. <style scoped>
  106. </style>
  1. //测试模拟数据
  2. childNodes = [
  3. {
  4. categoryImg: "https://www.baidu.com/img/bd_logo1.png",
  5. categoryName: "上衣",
  6. id:2,
  7. pId: 1,
  8. sort: 1,
  9. childNodes: [
  10. {
  11. categoryImg: "https://www.baidu.com/img/bd_logo1.png",
  12. categoryName: "短袖",
  13. haveGoods: true,
  14. id: 5,
  15. pId: 2,
  16. sort: 1,
  17. childNodes:[
  18. {
  19. id:111,
  20. pId: 5,
  21. sort: 1,
  22. categoryImg: "https://www.baidu.com/img/bd_logo1.png",
  23. categoryName: "短袖裤子",
  24. childNodes: []
  25. },
  26. {
  27. id:122,
  28. pId: 5,
  29. sort: 1,
  30. categoryImg: "https://www.baidu.com/img/bd_logo1.png",
  31. categoryName: "短袖鞋子",
  32. childNodes: []
  33. }
  34. ],
  35. },
  36. {
  37. categoryImg: "https://www.baidu.com/img/bd_logo1.png",
  38. categoryName: "西装",
  39. haveGoods: false,
  40. id: 6,
  41. pId: 2,
  42. sort: 1,
  43. childNodes:[
  44. {
  45. id:112,
  46. pId: 6,
  47. sort: 1,
  48. categoryImg: "https://www.baidu.com/img/bd_logo1.png",
  49. categoryName: "西装裤子",
  50. childNodes: []
  51. },
  52. {
  53. id:121,
  54. pId: 6,
  55. sort: 1,
  56. categoryImg: "https://www.baidu.com/img/bd_logo1.png",
  57. categoryName: "西装鞋子",
  58. childNodes: []
  59. }
  60. ],
  61. }
  62. ]
  63. },
  64. {
  65. id:11,
  66. pId: 1,
  67. sort: 1,
  68. categoryImg: "https://www.baidu.com/img/bd_logo1.png",
  69. categoryName: "裤子",
  70. childNodes: [
  71. {
  72. categoryImg: "https://www.baidu.com/img/bd_logo1.png",
  73. categoryName: "牛仔",
  74. haveGoods: true,
  75. id: 112222,
  76. pId: 11,
  77. sort: 1,
  78. childNodes:[],
  79. },
  80. ]
  81. },
  82. {
  83. id:12,
  84. pId: 1,
  85. sort: 1,
  86. categoryImg: "https://www.baidu.com/img/bd_logo1.png",
  87. categoryName: "鞋子",
  88. childNodes: [
  89. {
  90. categoryImg: "https://www.baidu.com/img/bd_logo1.png",
  91. categoryName: "耐克",
  92. haveGoods: true,
  93. id: 1121,
  94. pId: 12,
  95. sort: 1,
  96. childNodes:[
  97. {
  98. categoryImg: "https://www.baidu.com/img/bd_logo1.png",
  99. categoryName: "西牛仔",
  100. haveGoods: true,
  101. id: 11211,
  102. pId: 1121,
  103. sort: 1,
  104. childNodes:[],
  105. },
  106. ],
  107. },
  108. {
  109. categoryImg: "https://www.baidu.com/img/bd_logo1.png",
  110. categoryName: "阿迪",
  111. haveGoods: true,
  112. id: 1122,
  113. pId: 12,
  114. sort: 1,
  115. childNodes:[],
  116. },
  117. ]
  118. }
  119. ];
  1. //数据回显
  2. findId(arr, id) { //将选中的数组和id组成一个数组
  3. for (let i = 0; i < arr.length; i++) {
  4. if (arr[i].id === id) {
  5. return [
  6. [arr, i]
  7. ]
  8. break
  9. }
  10. }
  11. let c = []
  12. arr.forEach((item, i) => {
  13. let r = this.findId(item.childNodes || [], id)
  14. if (r && r.length) {
  15. c = [
  16. [arr, i]
  17. ].concat(r)
  18. }
  19. })
  20. // console.log(c,"回显数据")
  21. return c
  22. },

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/577804
推荐阅读
相关标签
  

闽ICP备14008679号