#Vue 3 + ts + antd table表格的使用(嵌套 子表格版)

1. 嵌套子表格的使用

在这里插入图片描述

<template>
  <a-table :columns="columns" :data-source="data" class="components-table-demo-nested">
    <template #bodyCell="{ column }">
      <template v-if="column.key === 'operation'">
        <a>Publish</a>
      </template>
    </template>
    <template #expandedRowRender="{ record }">
      <a-table :columns="innerColumns" :data-source="record.innerData" :pagination="false">
        <template #bodyCell="{ column }">
          <template v-if="column.key === 'state'">
            <span>
              <a-badge status="success" />
              Finished
            </span>
          </template>
          <template v-else-if="column.key === 'operation'">
            <span class="table-operation">
              <a>Pause</a>
              <a>Stop</a>
              <a-dropdown>
                <template #overlay>
                  <a-menu>
                    <a-menu-item>Action 1</a-menu-item>
                    <a-menu-item>Action 2</a-menu-item>
                  </a-menu>
                </template>
                <a>
                  Mores
                  <down-outlined />
                </a>
              </a-dropdown>
            </span>
          </template>
        </template>
      </a-table>
    </template>
  </a-table>
</template>
<script setup lang="ts">

import { DownOutlined } from '@ant-design/icons-vue';
import {fetchOrders} from "@/service/order/order";
import {CTYPE, Utils} from "@/utils";

const columns = [
  { title: '联系人', dataIndex: 'contacts', key: 'contacts' },
  { title: '创单时间', dataIndex: 'createOrderTime', key: 'createOrderTime' },
  { title: '订单状态', dataIndex: 'orderStatus', key: 'orderStatus' },
  { title: '订单类型', dataIndex: 'orderType', key: 'orderType' },
  { title: '订单id', dataIndex: 'ordersId', key: 'ordersId' },
  { title: '原始金额', dataIndex: 'originAmount', key: 'originAmount' },
  { title: '支付时间', dataIndex: 'payTime', key: 'payTime' },
  { title: '实收金额', dataIndex: 'receiptAmount', key: 'receiptAmount' },
  { title: '商品名称', dataIndex: 'skuName', key: 'skuName' },
  { title: '商品类型', dataIndex: 'skuType', key: 'skuType' },
  { title: '状态', dataIndex: 'status', key: 'status' },
  { title: '操作', key: 'operation' },
];
// 请求数据源 的数据类型 
interface DataItem {
  key: number;
  name?: string;
  platform?: string;
  version?: string;
  upgradeNum?: number;
  creator?: string;
  createdAt?: string;

  certificate?: string;
  contacts?: string;
  createOrderTime?: number;
  orderStatus?: number;
  orderType?: number;
  ordersId?: string;
  originAmount?: number;
  payTime?: number;
  receiptAmount?: number;
  skuName?: string;
  skuType?: string;
  status?: number;
  innerData: innerDataItem[]; // 新增属性
}
// 子表格
interface innerDataItem {
  key: number;
  order_item_id: string;
  certificate_id: string;
  item_status: number;
  item_update_time: number;
  refund_amount: number;
  refund_time: number;
}

// 分页的一些内容,对demo 可以忽略
const pagination = ref<COMMON.Pageable>({
  pageSize: CTYPE.pagination.pageSize,
  current: 1,
  total: 0
});
const orderQo = reactive<ORDER.OrderQo>({
  status: 1,
  sortPropertyName: 'id',
  sortAscending: false
});


// 响应数据
let contentData  = reactive<any>([]);
const data: DataItem[] = reactive([]);
const innerData: innerDataItem[] = reactive([]);


const innerColumns = [
  { title: '券维度的订单号', dataIndex: 'order_item_id', key: 'order_item_id' },
  { title: '券码号', dataIndex: 'certificate_id', key: 'certificate_id' },
  { title: '券维度订单状态', dataIndex: 'item_status',key: 'item_status' },
  { title: '退款金额', dataIndex: 'item_update_time', key: 'item_update_time' },
  { title: '退款时间', dataIndex: 'refund_amount', key: 'refund_amount' },
  { title: '券维度订单更新时间', dataIndex: 'refund_time', key: 'refund_time' },
  {
    title: 'Action',
    dataIndex: 'operation',
    key: 'operation',
  },
];
// 加载数据
const loadData = async () => {
  try {
    const result = await fetchOrders({
      ...orderQo,
      pageNumber: pagination.value.current,
      pageSize: pagination.value.pageSize,
    }) as ORDER.OrderPager;
    const { content = [] } = result;
    // 重点:获取到后台数据后,对数据进行处理,请重点看下面的数据处理思路!!!
    contentData = content;

    // 重置 data 和 innerData
    data.length = 0;
    innerData.length = 0;
    // 遍历数据
    for (let i = 0; i < contentData.length; ++i) {
      const outerItem: DataItem = {
        key: i,
        contacts: contentData[i].contacts,
        createOrderTime: contentData[i].createOrderTime,
        orderStatus: contentData[i].orderStatus,
        orderType: contentData[i].orderType,
        ordersId: contentData[i].ordersId,
        originAmount: contentData[i].originAmount /100  ,
        payTime: contentData[i].payTime,
        receiptAmount: (contentData[i].receiptAmount /100) ,
        skuName: contentData[i].skuName,
        skuType: contentData[i].skuType,
        status: contentData[i].status,
        innerData: [], // 初始化内层数据为空数组
      };

      // 解析当前订单的数据
      let jsondata: Array<{
        key: number;
        certificate_id: string;
        order_item_id: string;
        refund_time: number;
        refund_amount: number;
        item_status: number;
        item_update_time: number;
      }> = [];

      try {
        jsondata = JSON.parse(contentData[i].certificate);
      } catch (error) {
        console.error(`Failed to parse certificate data of order at index ${i}:`, error);
        continue; // 如果解析失败,跳过该订单
      }
      // 填充内层数据并将其关联到外层数据项
      for (let k = 0; k < jsondata.length; ++k) {
        outerItem.innerData.push({
          key: k,
          order_item_id: jsondata[k].order_item_id,
          certificate_id: jsondata[k].certificate_id,
          item_status: jsondata[k].item_status,
          item_update_time: jsondata[k].item_update_time,
          refund_amount: jsondata[k].refund_amount,
          refund_time: jsondata[k].refund_time,
        });

      }
      // 将外层数据项添加到 data 数组中
      data.push(outerItem);
      console.log("innerDatas的值", outerItem.innerData);
    }
  } catch (error) {
    // loading.value = false;
  }
}

// 初始化渲染数据
// onMounted 生命周期钩子会在组件挂载后立即执行,确保在组件渲染前完成数据的获取和初始化工作,进而使得内外层表格能正确渲染数据
onMounted(async () => {
  await loadData();
});

</script>


  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果您想使用Vue3和TypeScript来删除后台表格中的数据,可以使用以下步骤: 1. 在Vue3组件中定义table表格的数据结构和获取方式。 2. 在组件中定义一个方法来处理删除操作,理想情况下应该是在后台进行处理。 3. 在方法中使用axios库发送请求来删除所选行的数据。 4. 在Vue3中使用watchEffect函数来监听表格数据的变化,并实时更新数据。 5. 最后,您可以将删除操作绑定到table表格中的一列上,并在单击时触发该方法。 以下是一个示例代码: ```typescript <template> <table> <thead> <tr> <th>Id</th> <th>Name</th> <th>Delete</th> </tr> </thead> <tbody> <tr v-for="item in items" :key="item.id"> <td>{{ item.id }}</td> <td>{{ item.name }}</td> <td> <button @click="deleteItem(item.id)">Delete</button> </td> </tr> </tbody> </table> </template> <script> import { reactive, watchEffect } from 'vue'; import axios from 'axios'; export default { setup() { // 定义表格数据结构 const items = reactive([ { id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }, { id: 3, name: 'Item 3' }, { id: 4, name: 'Item 4' }, ]); // 删除操作方法 const deleteItem = async (id: number) => { const response = await axios.delete(`/items/${id}`); if (response.status === 200) { items.value = items.value.filter((item) => item.id !== id); } }; // 监听表格数据变化并更新 watchEffect(() => { console.log('Items updated:', items); }); // 返回视图数据 return { items, deleteItem, }; }, }; </script> ``` 在此示例中,我们使用reactive函数创建可响应的表格数据items,并在deleteItem方法中使用axios库发送删除数据请求。在请求成功后,我们使用filter方法更新表格数据中的值。最后,使用watchEffect函数监听items的变化并在控制台上进行打印。在模板中,我们将deleteItem方法绑定到“删除”按钮上,以便在单击按钮时调用它。 请注意,此示例代码仅供参考。具体实现取决于您的后端API和表格组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Light~One

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值