elementui 树形控件分页组件

子组件  tree.vue

<template>
  <div>
    <el-tree
      :props="defaultProps"
      :data="treeData"
      node-key="id"
      ref="tree"
      show-checkbox
      @check="check"
       @check-change="treeCheckChange"
      default-expand-all
    >
    </el-tree>
     <div style="text-align:right">
            <span
              @click="prePage"
              v-show="pagination.page !== 1"
              style="color:#409eff;margin-right:40px;user-select: none;"
            >
              上一页
            </span>
            <span
              @click="nextPage"
              v-show="pagination.page * pagination.pagesize < this.data.length"
              style="color:#409eff;user-select: none;"
            >
              下一页
            </span>
          </div>
  </div>
</template>

<script>
export default {
  props:{
    data:Array
  },
  data() {
    return {
       defaultProps: {
        children: "book",
        label: "label",
      },
       pagination: {
        page: 1,
        pagesize: 5,
      },
    };
  },
  created () {
  },
  computed: {
    treeData() {
      return [
        {
          id: "*-1",
          label: "全部",
          book: this.data.slice(
            (this.pagination.page - 1) * this.pagination.pagesize,
            this.pagination.page * this.pagination.pagesize
          ),
        },
      ];
    },
    
  },
  methods: {
      treeCheckChange(item, isCheck) {
         const checkedNodes = this.$refs.tree.getCheckedNodes() 
            // item.checked = isCheck
            // console.log(isCheck)
            // console.log(item.checked)
            this.$emit('check-change',item,isCheck,checkedNodes)
        },
    //当复选框被点击的时候触发
    check(a, b, c) {
     this.$emit('check',a,b,c)
     console.log(this.treeData)
    },
     //数形控件上一页
    prePage() {
      if (this.pagination.page > 1) {
        this.pagination.page--;
      }
    },
    //树形控件下一页
    nextPage() {
      if (this.pagination.page * this.pagination.pagesize < this.data.length) {
        this.pagination.page++;
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>

父组件调用

<template>
  <div>
  
          <tree
            ref="tree"
            :data="data"
            @check-change="treeCheckChange"
            @check="check"></tree>
         
  </div>
</template>

<script>

import tree from "../../components/tree.vue";
export default {
  components: { tree },
  data() {
    return {
     
      data: [],
        arr:[],
     
  },
  created() {
    this.getInfo();
  },
  methods: {
    

    //获取期刊数列
    getTreeData() {
      let type = 1;
      book_aggregate(type).then((res) => {
        if (res.msg == "成功") {
          if (res.data.length == 0) {
            console.log("没有数据");
            this.dialogVisible = false;
            this.$message.error("没有需要印刷的刊物");
          } else {
            this.dialogVisible = true;
            res.data.map((item) => {
              item.label = item.title;
              item.disabled = false;
              item.id = "*" + item.id;
              if (item.book.length != 0) {
                item.book.map((i) => {
                  i.label = i.subtitle;
                  i.disabled = false;
                  i.id = i.book_id + "";
                });
              }
            });
            this.data = res.data;
          }

          console.log(this.data);
        }
      });
    },

   

    //当复选框被点击的时候触发
    check(a, b, c) {
      if (b.checkedKeys.length != 0) {
        this.arr = this.removeStr(b.checkedKeys, "*");
        console.log(this.arr);
      }
    },
    treeCheckChange(item, isCheck, checkedNodes) {
    
      item.checked = isCheck;
    
    },

   
   

};
</script>

<style lang="scss" scoped>

</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java中返回ElementUI树形控件,需要先构建一个树形数据结构,并将该数据结构转化为符合ElementUI树形控件要求的数据格式,最后将数据返回给前端。 首先,在Java中,可以使用类来表示树的节点,例如: ```java public class TreeNode { private String label; private List<TreeNode> children; // 其他属性和方法 // 构造函数 public TreeNode(String label) { this.label = label; this.children = new ArrayList<>(); } // getter和setter方法 // 添加子节点 public void addChild(TreeNode child) { this.children.add(child); } } ``` 然后,构建树的结构,在Java中可以使用递归来实现: ```java public class TreeStructure { public TreeNode buildTree() { TreeNode root = new TreeNode("根节点"); // 添加子节点 TreeNode node1 = new TreeNode("节点1"); TreeNode node2 = new TreeNode("节点2"); TreeNode node3 = new TreeNode("节点3"); root.addChild(node1); root.addChild(node2); root.addChild(node3); // 继续添加子节点 TreeNode subNode1 = new TreeNode("子节点1"); TreeNode subNode2 = new TreeNode("子节点2"); node1.addChild(subNode1); node1.addChild(subNode2); // 添加更多子节点... return root; } } ``` 接下来,将构建好的树形数据结构转化为ElementUI树形控件要求的数据格式,并返回给前端,可以使用JSON格式进行传输。在Java中,可以使用第三方库,如Gson库,将Java对象转化为JSON数据: ```java public class Main { public static void main(String[] args) { TreeStructure treeStructure = new TreeStructure(); TreeNode root = treeStructure.buildTree(); // 转化为ElementUI树形控件要求的数据格式 String jsonData = convertToJSONData(root); // 返回给前端... } public static String convertToJSONData(TreeNode root) { // 使用Gson库将Java对象转化为JSON数据 Gson gson = new Gson(); return gson.toJson(root); } } ``` 最后,将生成的JSON数据返回给前端,前端再使用ElementUI树形控件进行展示即可。以上就是使用Java返回ElementUI树形控件的简单示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值