element-plus el-table、动态添加、删除行(带子级)

<templat>
      <div>
        <el-button @click="addRow">Add Row</el-button>
        <el-button @click="deleteAllRows">Delete All Rows</el-button>
        <el-table
            :data="tableData"
            style="width: 100%"
            row-key="id"
            border
            lazy
            :load="load"
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
            height="500px"
        >
          <el-table-column prop="date" label="Date" />
          <el-table-column prop="name" label="Name" />
          <el-table-column prop="address" label="Address" />
          <el-table-column label="Actions">
            <template #default="{ row }">
              <el-button @click="addChild(row)">Add Child</el-button>
              <el-button @click="deleteRow(row)">Delete Row</el-button>
            </template>
          </el-table-column>
          <el-table-column type="expand">
            <template #default="{ row }">
              <el-table v-if="row.children && row.children.length > 0" :data="row.children" style="width: 100%">
                <el-table-column prop="date" label="DateChild" />
                <el-table-column prop="name" label="NameChild" />
                <el-table-column prop="address" label="AddressChild" />
                <!-- 继续添加子级数据的表头 -->
                <el-table-column label="Actions">
                  <template #default="{ row, $index }">
                    <el-button @click="deleteChild(row)">Delete Child</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </template>
// 初始数据
    const tableData = ref([
      {
        id: 1,
        date: '2022-01-01',
        name: 'John',
        address: 'New York',
        children: [
          {
            id: 11,
            date: '2022-01-02',
            name: 'May',
            address: 'Los Angeles'
          }
        ]
      },
      {
        id: 2,
        date: '2022-01-02',
        name: 'Jane',
        address: 'San Francisco'
      }
    ]);

    // 添加行
    const addRow = () => {
      const newRow = {
        id: Date.now(),
        date: '',
        name: '',
        address: '',
        children: []
      };
      tableData.value.push(newRow);
    };

    // 删除行
    const deleteRow = (row) => {
      const index = tableData.value.indexOf(row);
      if (index !== -1) {
        tableData.value.splice(index, 1);
      }
    };

    // 添加子行
    const addChild = (row) => {
      if (!row.children) {
        row.children = [];
      }
      row.children.push({
        id: Date.now(),
        date: '',
        name: '',
        address: ''
      });
    };

    // 删除子行
    const deleteChild = (childRow) => {
      // 通过 childRow 访问父级行数据
      const parentRow = getParentRow(childRow);
      if (parentRow) {
        const childIndex = parentRow.children.indexOf(childRow);
        if (childIndex !== -1) {
          parentRow.children.splice(childIndex, 1);
        } else {
          console.warn('Child not found');
        }
      }
    };

    // 辅助函数,用于获取父级行数据
    const getParentRow = (childRow) => {
      for (const row of tableData.value) {
        if (row.children && row.children.includes(childRow)) {
          return row;
        }
      }
      return null;
    };

    // 删除所有行
    const deleteAllRows = () => {
      tableData.value = [];
    };

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值