首页 > 其他分享 >el-table树形数据与懒加载

el-table树形数据与懒加载

时间:2023-04-11 09:57:10浏览次数:37  
标签:el false list id item 树形 table data children

<template>
  <div class="page">
    <div class="page-box">
      <h3 style="margin-top: 0">类目 / 榜单管理</h3>
      <el-input placeholder="请输入关键字" v-model="keyWord" style="margin-bottom: 20px">
        <el-button slot="append" @click="treeFind" icon="el-icon-search"></el-button>
      </el-input>
      <el-table
        fit
        border
        :data="tableData"
        row-key="id"
        ref="multipleTable"
        resizable
        style="marign-bottom: 20px"
        default-expand-all
        :tree-props="{
          children: 'children',
          hasChildren: 'hasChildren',
        }"
        v-loading="loading"
      >
        <el-table-column width="300" label="类目">
          <template slot-scope="scope">
            <span v-if="scope.row.id > 0"> {{ scope.row.name }}</span>
            <el-input
              v-else
              ref="input"
              @blur="inputBlur(scope.row)"
              @change="inputChange(scope.row)"
              v-model="scope.row.name"
              size="mini"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="Best Sellers">
          <template slot-scope="scope">
            <upload-btn-list
              btnType="success"
              :btnDisabled="scope.row.id > 0 ? false : true"
            ></upload-btn-list>
          </template>
        </el-table-column>
        <el-table-column align="center" label="New Releaser">
          <template slot-scope="scope">
            <upload-btn-list :btnDisabled="scope.row.id > 0 ? false : true"></upload-btn-list>
          </template>
        </el-table-column>
        <el-table-column align="center" label="Movers & Shakers">
          <template slot-scope="scope">
            <upload-btn-list :btnDisabled="scope.row.id > 0 ? false : true"></upload-btn-list>
          </template>
        </el-table-column>
        <el-table-column align="center" label="Most Wished For">
          <template slot-scope="scope">
            <upload-btn-list :btnDisabled="scope.row.id > 0 ? false : true"></upload-btn-list>
          </template>
        </el-table-column>
        <el-table-column align="center" label="Gift Ideas">
          <template slot-scope="scope">
            <upload-btn-list :btnDisabled="scope.row.id > 0 ? false : true"></upload-btn-list>
          </template>
        </el-table-column>
        <el-table-column width="300" align="center" label="操作">
          <template slot-scope="scope">
            <div>
              <el-button
                :disabled="scope.row.id > 0 ? false : true"
                size="small"
                type="info"
                @click="append(scope.row)"
                >新增子级</el-button
              >
              <el-button
                :disabled="scope.row.id > 0 ? false : true"
                size="small"
                type="warning"
                @click="update(scope.row)"
                >编辑</el-button
              >
              <el-button size="small" type="danger" @click="del(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <dialog-edit-add
      :Visible="dialogVisible"
      :data="dialogData"
      @close="dialogClose"
      @save="dialogSave"
    ></dialog-edit-add>
  </div>
</template>
<script>
import dialogEditAdd from '@/components/dialog/data/dialogEditAdd.vue';
import UploadBtnList from '@/components/common/upload-btn-list.vue';
export default {
  components: { dialogEditAdd, UploadBtnList },
  data() {
    return {
      rowId: 0,
      loading: false,
      dialogVisible: false,
      dialogData: {},
      keyWord: '',
      tableData: [],
      tableDataSource: [
        {
          id: 1,
          name: 'Any Department',
          parentId: 0,
          children: [
            {
              id: 2,
              name: 'Amazon Devices & Accessories',
              parentId: 1,
              children: [],
            },
          ],
        },
      ],
    };
  },
  created() {
    this.tableData = this.tableDataSource;
  },
  methods: {
    del(data) {
      this.$confirm(`此操作将永久删除类目${data.name}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.removeChild(this.tableData, data.id);
          const a = this.$message({
            type: 'success',
            message: '删除成功!',
          });
        })
        .catch(() => {});
    },
    open(data, value) {
      if (value == 0) {
        this.dialogData = {
          id: 0,
          name: '',
          parentId: data.id,
          children: [],
        };
      } else if (value == 1) {
        this.dialogData = data;
      }
      this.dialogVisible = true;
    },
    append(data) {
      const newChild = {
        id: 0,
        name: '',
        parentId: data.id,
        children: [],
      };
      data.children.push(newChild);
      this.$nextTick(() => {
        this.$refs.input.focus();
      });
    },
    update(data) {
      this.rowId = data.id;
      data.id = 0;
      this.$nextTick(() => {
        this.$refs.input.focus();
      });
    },
    inputBlur(data) {
      this.inputChange(data);
    },
    inputChange(data) {
      if (data.name == '') {
        this.$message.error('类目名不能为空');
        return false;
      }
      this.loading = true;
      if (this.rowId == 0) {
        //新增
        data.id = 10;
      } else {
        //修改
        data.id = this.rowId;
      }
      setTimeout(() => {
        this.rowId = 0;
        this.loading = false;
      }, 1000);
    },
    treeFind() {
      let result = this.findChild(this.tableDataSource, (node) =>
        node.name.toLowerCase().includes(this.keyWord.toLowerCase())
      );
      if (!result) {
        this.tableData = [];
      } else {
        this.tableData = [];
        this.tableData.push(result);
      }
    },
    dialogClose() {
      this.dialogVisible = false;
    },
    dialogSave(data) {
      if (data.id == 0) {
        this.insertChild(this.tableData, data.parentId, data);
      } else {
        this.updateChild(this.tableData, data.id, data);
      }
      this.dialogVisible = false;
    },
    findChild(tree, func) {
      for (const data of tree) {
        if (func(data)) return data;
        if (data.children) {
          const res = this.findChild(data.children, func);
          if (res) return res;
        }
      }
      return null;
    },
    removeChild(list, id) {
      const parent = list;
      const index = parent.findIndex((child) => child.id == id);
      if (index !== -1) {
        parent.splice(index, 1);
        return list;
      }
      list = list.filter((item) => {
        let flag = item.id != id;
        if (flag) {
          if (item.children.length > 0) {
            item.children = this.removeChild(item.children, id);
          }
          return true;
        } else return flag;
      });
      return list;
    },
    insertChild(list, id, newChild) {
      list.forEach((item) => {
        let flag = item.id == id;
        if (flag) {
          item.children.push(newChild);
        } else {
          if (item.children.length > 0) {
            item.children = this.insertChild(item.children, id, newChild);
          }
        }
      });
      return list;
    },
    updateChild(list, id, newChild) {
      list.forEach((item, index) => {
        let flag = item.id == id;
        if (flag) {
          alert(1);
          this.$set(list, index, newChild);
        } else {
          if (item.children.length > 0) {
            item.children = this.updateChild(item.children, id, newChild);
          }
        }
      });
      return list;
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-input-group {
  width: 300px;
}
::v-deep tr td:first-child .cell {
  display: flex !important;
  flex-wrap: nowrap !important;
}
</style>

 

标签:el,false,list,id,item,树形,table,data,children
From: https://www.cnblogs.com/Aatao/p/17305200.html

相关文章

  • ELK、ELFK企业级日志分析系统
    一、ELK简介1、什么是ELKELK日志分析系统:由3个组件组成Elasticesearch、Kiabana、Logstash完成更强大的用户对日志的查询排序和统计需求日志服务器作用:提高安全性、集中存放日志、缺陷:对日志分析困难2、ELK工作原理及过程①、将日志进行集中化管理,从消息队列进入logstash......
  • excel切片器多表/多图联动
    1、以相同的数据源建立不同维度的透视表(这一步最关键,数据源必须相同,否则无法进行多表链接)  2、选择期中一个数据透视表插入切片器  3、切片器空白处右键选择报表链接  4、在需要联动的透视表打勾即可以透视表为基础制作图标,即可实现多图联动 ......
  • delphi 11.3 java.ioexception:cleartext http traffic [IP地址] not permitted
    要在AndroidManifest.xml添加如下属性即可:参考:HowtoFixCleartextHTTPTrafficnotPermittedinAndroid-TRENDOCEANS ......
  • selenium驱动未随浏览器更新而同步更新的问题
    基于selenium模拟谷歌浏览器登录时,依赖chromedriver.exe版本信息。但谷歌浏览器升级后,之前创建的脚本可能会出现因驱动版本过低,使得之前创建的脚本运行失败的问题。下面针对该问题进行探索和解决。selenium版本importseleniumselenium.__version__#'4.7.2'获取谷歌浏览......
  • celery正常启动后能收到任务,但不执行任务的解决办法
    错误截图:celery接收到任务却不执行(多出在windows系统中)解决方法1添加–pool=solo参数celery-Acelery_tasks.mainworker--pool=solo-linfo解决方法2先安装gevent,然后在启动celery的时候添加gevent参数pipinstallgeventcelery-Acelery_tasks.mainworker-linfo......
  • Semantic Kernel 入门系列:
    如果把提示词也算作一种代码的话,那么语义技能所带来的将会是全新编程方式,自然语言编程。通常情况下一段prompt就可以构成一个SemanticFunction,如此这般简单,如果我们提前可以组织好一段段prompt的管理方式,甚至可以不需要写任何的代码,就可以构造出足够多的技能来。使用文件夹管......
  • python+selenium写自动化脚本遇到的坑
    1.定位不到元素网速不好定位的元素还没有刷新出来使用等待有三种强制等待,显式等待,隐式等待动态ID不要复制xpath,要手写。判断是动态ID的方法,多次关闭浏览器再打开网址,查看ID是否会发生变化下拉框,文件无法定位还没有解决......
  • Js中delete的作用
    JavaScript中的delete用于删除对象的属性或数组的元素。它可以让你删除一个对象的指定属性或数组的指定元素。以下是使用delete来删除一个对象的属性的示例:constperson={name:"John",age:30,city:"NewYork"};deleteperson.age;console.log(person);//输出......
  • element-ui校验表单只能输入数字
    element-ui校验表单只能输入数字原文链接:https://blog.csdn.net/q879936814/article/details/126788782接到需求,让表单中只能输入数字,使用v-model的.number可以实现,但是不能以0为开头;又试了rule加type=number校验,输入数组一直报输入的不是数字。。最后使用了onkeyup方法<el......
  • elementUI input只能输入数字、小数的几种方法
    elementUIinput只能输入数字、小数的几种方法原文链接:https://blog.csdn.net/weixin_42397937/article/details/123297108elementUI文本框input设置仅可输入数字或者小数的几种方法一、设置type=“number”这个属性可以帮助我们限制文本框输入的文本只能是数字或者小数,但......