el-table中数据的动态加载

<template>
  <div>
    <el-button @click="hahahah">转换数据</el-button>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column
        v-for="(item, index) in propsArray"
        :key="index"
        :prop="item.prop"
        :label="item.label"
      >
        <template slot-scope="scope">
          {{ scope.row[item.prop] }}
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

  <script>
export default {
  data() {
    return {
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        },
      ],
      propsArray: [
        {
          prop: "date",
          label: "日期",
        },
        {
          prop: "name",
          label: "姓名",
        },
        {
          prop: "address",
          label: "地址",
        },
      ],
      list: [
        {
          iphone: "苹果",
          price: "8999",
          color: "红色",
        },
        {
          iphone: "华为",
          price: "8999",
          color: "绿色",
        },
        {
          iphone: "oppop",
          price: "3000",
          color: "金色",
        },
        {
          iphone: "vivo",
          price: "2300",
          color: "蓝色",
        },
      ],
      listArray: [
        {
          prop: "iphone",
          label: "品牌",
        },
        {
          prop: "price",
          label: "价格",
        },
        {
          prop: "color",
          label: "颜色",
        },
      ],
    };
  },
  methods: {
    hahahah() {
      this.tableData = [];
      this.propsArray = [];
      this.propsArray = this.listArray;
      this.tableData = this.list;
    },
  },
};
</script>

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

效果:

点击“转换按钮”之后,数据变化

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 `expand-row` 事件来实现 el-table 的懒加载,具体步骤如下: 1. 在 el-table 设置 `:tree-props="{lazy:true, load:loadChildren}"`,其 `lazy:true` 表示启用懒加载,`load:loadChildren` 表示加载远程子节点数据的方法为 `loadChildren`。 2. 在 `methods` 定义 `loadChildren` 方法,该方法接收三个参数:`node` 表示当前节点,`resolve` 表示加载完成后的回调函数,`reject` 表示加载失败后的回调函数。 3. 在 `loadChildren` 方法通过远程请求获取当前节点的子节点数据,然后将数据传给 `resolve` 函数。 4. 在 `expand-row` 事件调用 `loadChildren` 方法,将当前节点和回调函数作为参数传递进去。 下面是一个示例代码: ```html <template> <el-table :data="tableData" :tree-props="{lazy:true, load:loadChildren}" @expand-row="expandRow" > <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [] } }, methods: { loadChildren(node, resolve, reject) { // 发送远程请求获取当前节点的子节点数据 axios.get(`/api/children?id=${node.id}`).then(response => { resolve(response.data) }).catch(error => { reject(error) }) }, expandRow(row, expandedRows) { // 调用 loadChildren 方法加载子节点数据 this.loadChildren(row, children => { row.children = children expandedRows.push(row) }) } } } </script> ``` 在上面的示例,我们通过 `axios` 发送了一个远程请求来获取子节点数据,并通过 `resolve` 函数将数据传递给 el-table 组件。在 `expand-row` 事件,我们调用了 `loadChildren` 方法来加载子节点数据,并将回调函数 `children => {row.children = children; expandedRows.push(row)}` 作为参数传递进去。在回调函数,我们将子节点数据赋值给当前节点的 `children` 属性,并将当前节点加入到 `expandedRows` 数组,以便展开当前节点时可以正常显示子节点。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值