<template>
    <el-select v-model="mineStatus" placeholder="请选择区域" 
    multiple
    collapse-tags @change="selectChange" >
      <el-option  :value="mineStatusValue" style="height: auto">
       	<el-tree :data="treedata" show-checkbox node-key="id" ref="tree" highlight-current :props="defaultProps" @check-change="handleCheckChange"></el-tree>
	  </el-option>
  </el-select>
</template>
<script>
export default {
  data() {
    return {
      mineStatus: "",
      mineStatusValue: [],
      data: [
        {
          id: 1,
          label: "江苏省",
          children: [
            {
              id: 4,
              label: "南京市"
            }
          ]
        },
        {
          id: 2,
          label: "安徽省",
          children: [
            {
              id: 5,
              label: "芜湖市"
            },
            {
              id: 6,
              label: "合肥市"
            }
          ]
        },
        {
          id: 3,
          label: "浙江省",
          children: [
            {
              id: 7,
              label: "杭州市"
            },
            {
              id: 8,
              label: "温州市"
            }
          ]
        }
      ],
      defaultProps: {
        children: "children",
        label: "name"
      }
    };
  },
   props:{
        treedata:{
          type:Array,
          default:undefined
      },
   },
  methods: {
//select框值改变时候触发的事件
selectChange(e){
	  var arrNew = [];
      var dataLength = this.mineStatusValue.length;
      var eleng = e.length;
      for(let i = 0; i< dataLength ;i++){
        for(let j = 0; j < eleng; j++){
          if(e[j] === this.mineStatusValue[i].name){
            arrNew.push(this.mineStatusValue[i])
          }
        }
      }
      this.$refs.tree.setCheckedNodes(arrNew);//设置勾选的值
},
 handleCheckChange() {
      let res = this.$refs.tree.getCheckedNodes(true, true); //这里两个true，1. 是否只是叶子节点 2. 是否包含半选节点（就是使得选择的时候不包含父节点）
      let arrLabel = [];
      let arr = [];
      res.forEach(item => {
        arrLabel.push(item.name);
        arr.push(item);
      });
      this.mineStatusValue = arr;
      this.mineStatus = arrLabel;
     // console.log('arr:'+JSON.stringify(arr))
     // console.log('arrLabel:'+arrLabel)
    }
  }
};
</script>>

