Units list out, false (encapsulated) components

 Renderings:

       Because each page uses this unit, list the unit list, pretending to be a packaged component, and use it directly on other pages.

source code:

<template>
  <div style="height: 48rem;overflow-y: scroll">
    <h4>单位列表</h4>
    <el-input
      placeholder="输入关键字进行过滤"
      v-model="filterText"
    >
    </el-input>
    <!-- node-click 点击事件 -->
    <el-tree
      class="filter-tree"
      :data="LeftData"
      :props="defaultProps"
      default-expand-all
      highlight-current
      node-key="ID"
      @node-click="handleNodeClick"
      :filter-node-method="filterNode"
      ref="treeList"
    >
    </el-tree>
  </div>
</template>

<script>
export default {
  //组件名称
  name: "",
  //import引入的组件需要注入到对象中才能使用
  components: {},
  //父组件传参,子组件接收
  props: ["LeftTreeData"],
  //生命周期 - 创建完成(可以访问当前this实例)
  created() {},
  //生命周期 - 挂载完成(可以访问DOM元素)
  mounted() {
    this.getLeftData();
  },
  data() {
    return {
      //进行模糊查询的数据
      filterText: "",
      //树形结构
      defaultProps: {
        children: "Children",
        label: "Text",
        id: "ID",
      },
      //  进行传递后端的树形结构的id
      DataID: null,
      //数据:展示的数据
      LeftData: [],
    };
  },
  //方法集合
  methods: {
    //获取左侧单位数据列表
    getLeftData() {
      let that = this;

      //获取菜单左侧数据列表
      that.$API.GetTreeList().then((res) => {
        this.LeftData = res.data.data;
        //传给父组件数据
        this.$emit("getTreeData", this.LeftData);
      });
    },
    //进行切换数据(点击事件)
    handleNodeClick(data, checked) {
      if (checked) {
        //进行勾选的数据
        this.$refs.treeList.setCheckedNodes([data]);
        console.log(data, "data");
        // 进行选中接收表格数据id
        this.DataID = data.ID;
        this.$emit("getTreeDataID", this.DataID);
        this.$emit("GetTreeData", data);
      }
    },
    //进行模糊查询
    filterNode(value, data) {
      if (!value) return true;
      return data.Text.indexOf(value) !== -1;
    },
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    //过滤
    filterText(val) {
      this.$refs.treeList.filter(val);
    },
  },
};
</script>

<style scoped>
</style>

Quoting from other pages:

<div>
 //3. 使用
  <ComUnitSetTreeData @GetTreeData="handleNodeClick" />
</div>

<script>
//1. 引入
import ComUnitSetTreeData from "@/components/zaojia/ComUnitSetTreeData/index.vue";
export default {
  //2. 注册
  components: {
    ComUnitSetTreeData,
  },
  methods:{
    //当点击数据时,调这个方法: (获取点击左侧树的数据)
    handleNodeClick(data) {
      this.selectedDictIDs = data.Value; //ID 左侧树的唯一值
      this.selectLabel = data.Text; //单位名称
      console.log("点击树的时候===", data);
      this.init(this.selectedDictIDs); //调自己的方法(这边是初始化数据)
    },
  }
}
</script>

Guess you like

Origin blog.csdn.net/CMDN123456/article/details/131597872