国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

vue3 組合式 ant.design組件Table嵌套表格,從后端獲取數(shù)據(jù)并動態(tài)渲染

這篇具有很好參考價值的文章主要介紹了vue3 組合式 ant.design組件Table嵌套表格,從后端獲取數(shù)據(jù)并動態(tài)渲染。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

在根據(jù)官方文檔使用ant.design中的嵌套表格時,發(fā)現(xiàn)官方文檔很多地方都不夠詳細。在過程中踩了不少坑,例如:

子表如何獲取父表的數(shù)據(jù)?

如何獲取子表的行索引?

如何讓子表的數(shù)據(jù)源來自父表該行的數(shù)據(jù)?

總之,最后還是磕磕絆絆做完了功能,于是第一時間把代碼整理出來以作分享。

首先,后端返回的數(shù)據(jù)是json格式。文章來源地址http://www.zghlxwxcb.cn/news/detail-616376.html

后端返回數(shù)據(jù)格式如下:

{
    "result": true,
    "errorCode": null,
    "errorMsg": null,
    "data": [
        {
            "id": "001",
            "name": "A",
            "releaseVersion": "v2.00.02",
            "subSystem": "a",
            "type": "edit",
            "history": [
                {
                    "editVersion": 1,
                    "timestamp": "2023/05/08 12:23",
                    "comment": "備注1.1"
                },
                {
                    "editVersion": 2,
                    "timestamp": "2023/05/08 12:23",
                    "comment": "備注1.2"
                }
            ]
        },
        {
            "id": "002",
            "name": "B",
            "releaseVersion": "v2.00.01",
            "subSystem": "b",
            "type": "edit",
            "history": [
                {
                    "editVersion": 1,
                    "timestamp": "2023/05/08 12:23",
                    "comment": "備注2.1"
                },
                {
                    "editVersion": 2,
                    "timestamp": "2023/05/08 12:23",
                    "comment": "備注2.2"
                }
            ]
        }
    ],
    "recordsTotal": 123
}

前端渲染的數(shù)據(jù)格式如下:

[
    {
        "key": 0,
        "name": "A",
        "subSystem": "a",
        "type": "edit",
        "expand": " ",
        "innerDatas": [
            {
                "key": 0,
                "ID": "001",
                "version": "v2.00.021",
                "editComment": "備注1.1",
                "editTime": "2023/05/08 12:23",
                "more": " "
            },
            {
                "key": 0,
                "ID": "001",
                "version": "v2.00.022",
                "editComment": "備注1.2",
                "editTime": "2023/05/08 12:23",
                "more": " "
            }
        ]
    },
    {
        "key": 1,
        "name": "B",
        "subSystem": "b",
        "type": "edit",
        "expand": " ",
        "innerDatas": [
            {
                "key": 1,
                "ID": "002",
                "version": "v2.00.011",
                "editComment": "備注2.1",
                "editTime": "2023/05/08 12:23",
                "more": " "
            },
            {
                "key": 1,
                "ID": "002",
                "version": "v2.00.012",
                "editComment": "備注2.2",
                "editTime": "2023/05/08 12:23",
                "more": " "
            }
        ]
    }
        ]

代碼:

<template>

? ? <div id="main">

? ? ? ? <a-table>

? ? ? ? ? ? <template #expandedRowRender="{record}"> 
//這一行代碼的意思是添加了一個插槽,#expandedRowRender="{record, index, indent, expanded}",template里面的元素可以使用大括號內(nèi)的值。其中record是父表內(nèi)一行的數(shù)據(jù),index是行的索引,indent是縮進,expanded表示是否展開。

? ? ? ? ? ? ? ? <div class="child">

? ? ? ? ? ? ? ? ? ? <a-table?:columns="innerColumns" :data-source="record.innerDatas"?>


//表示子表的數(shù)據(jù)源來自父表record一行數(shù)據(jù)的innerDatas

? ? ? ? ? ? ? ? ? ? ? ? <template #bodyCell="{ text, record, index, column}"> 
//text表示該行該列也就是這個單元格內(nèi)的值,record表示子表一行的數(shù)據(jù),index表示子表行的索引,column表示子表的列。同樣的,該插槽放在父表內(nèi)表示的就是父表的數(shù)據(jù)。

? ? ? ? ? ? ? ? ? ? ? ? ? ? <template v-if="column.key === 'more'"> 
//這表示子表里column.key為more的這一列,都顯示“詳細”

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<a @click="cilikRow = index, modalVisible = cilikRow > -1" >

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 詳細

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </a>

? ? ? ? ? ? ? ? ? ? ? ? ? ? </template>

? ? ? ? ? ? ? ? ? ? ? ? </template>

? ? ? ? ? ? ? ? ? ? </a-table>

? ? ? ? ? ? ? ? ? ? <div class="createModel" ref="createModel">

? ? ? ? ? ? ? ? ? ? ? ? <a-modal v-model:visible="modalVisible" title=" ">

? ? ? ? ? ? ? ? ? ? ? ? ? ? <p class="modal-editComment">{{ dataList[0].innerDatas[cilikRow.valueOf()].editComment }}</p>?


//一個a-modal彈窗,當(dāng)點擊“詳細”時顯示子表中editComment的內(nèi)容

? ? ? ? ? ? ? ? ? ? ? ? </a-modal>

? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? </template>

? ? ? ? </a-table>

? ? </div>

</template>

<script lang="ts">

import { DownOutlined } from '@ant-design/icons-vue';

import { defineComponent } from 'vue';

import axios from 'axios';



const cilikRow = ref<Number>(-1)

const columns = [ //父表的列設(shè)置

? ? { title: '標(biāo)題', dataIndex: 'name', key: 'name', className: 'thChange', width: 400 },

? ? { title: '副標(biāo)題', dataIndex: 'subSystem', key: 'subSystem', className: 'thChange', width: 400 },

? ? { title: '類型', dataIndex: 'type', key: 'type', className: 'thChange', width: 550 },

? ? { title: ' ?', dataIndex: 'expand', key: 'expand', className: 'thChange', expandFixed: 'right' },

];

const innerColumns = [ //子表的列設(shè)置

? ? { title: 'ID', dataIndex: 'ID', key: 'ID', className: 'thColor', width: 100 },

? ? { title: '版本', dataIndex: 'version', key: 'version', width: 100 },

? ? { title: '編輯備注', dataIndex: 'editComment', key: 'editComment', ellipsis: true, width: 250 },

? ? { title: '編輯日期', dataIndex: 'editTime', key: 'editTime', width: 150 },

? ? { title: '', dataIndex: 'more', key: "more" }

];

interface DataItem { //父表的數(shù)據(jù)類型定義,我在這里直接把子表的內(nèi)容放在了父表中

? ? key: number;

? ? name: string;

? ? subSystem: string;

? ? type: string;

? ? expand: any;

? ? innerDatas: innerDataItem[]

}

interface innerDataItem { //子表的數(shù)據(jù)類型定義

? ? key: number;

? ? ID: string;

? ? version: string;

? ? editComment: string;

? ? editTime: string;

? ? more: string

}

let innerData= reactive<Array<innerDataItem>>([])??//定義一個innerDataItem類型的響應(yīng)式數(shù)組存放子表的數(shù)據(jù)

const dataList = reactive<Array<DataItem>>([])?//定義一個DataItem類型的響應(yīng)式數(shù)組存放父表的數(shù)據(jù)

axios.get('http://127.0.0.0/Test/Test123.json')

? ? .then((res) => {

? ? ? ? for (let i = 0; i < res.data.data.length; i++) {

? ? ? ? ? ? innerData = [];? ? //每次循環(huán)將子表數(shù)據(jù)清空 確保子表數(shù)組中的只有父表這一行對應(yīng)的子表數(shù)據(jù)而不是子表所有的數(shù)據(jù)

? ? ? ? ? ? for (let j = 0; j < res.data.data[i].history.length; j++) { //子表數(shù)據(jù)存入

? ? ? ? ? ? ? ? ? ? innerData.push({

? ? ? ? ? ? ? ? ? ? ? ? key: i,

? ? ? ? ? ? ? ? ? ? ? ? ID: res.data.data[i].id,

? ? ? ? ? ? ? ? ? ? ? ? version: res.data.data[i].releaseVersion + res.data.data[i].history[j].editVersion,

? ? ? ? ? ? ? ? ? ? ? ? editComment: res.data.data[i].history[j].comment,

? ? ? ? ? ? ? ? ? ? ? ? editTime: res.data.data[i].history[j].timestamp,

? ? ? ? ? ? ? ? ? ? ? ? more: " "

? ? ? ? ? ? ? ? ? ? })

? ? ? ? ? ? ? ? }



? ? ? ? ? ? dataList.push({ //父表數(shù)據(jù)存入

? ? ? ? ? ? ? ? key: i,

? ? ? ? ? ? ? ? name: res.data.data[i].name,

? ? ? ? ? ? ? ? subSystem: res.data.data[i].subSystem,

? ? ? ? ? ? ? ? type: res.data.data[i].type,

? ? ? ? ? ? ? ? expand: " ",

? ? ? ? ? ? ? ? innerDatas:innerData? ? ? ? ? ? ? ?

? ? ? ? ? ? })? ? ? ? ? ? ? ? ? ? ?

? ? ? ? }? ? ??

? ? ? ? console.log(dataList)

? ? })

const modalVisible = ref<boolean>(false);

export default defineComponent({

? ? components: {

? ? ? ? DownOutlined,

? ? },

? ? setup() {

? ? ? ? return {

? ? ? ? ? ? modal: false,

? ? ? ? ? ? centerDialogVisible: false,

? ? ? ? ? ? modalVisible,

? ? ? ? ? ? dataList,

? ? ? ? ? ? columns,

? ? ? ? ? ? innerColumns,

? ? ? ? ? ? innerData,

? ? ? ? ? ? cilikRow,

? ? ? ? };

? ? },

});

</script>

到了這里,關(guān)于vue3 組合式 ant.design組件Table嵌套表格,從后端獲取數(shù)據(jù)并動態(tài)渲染的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Vue3組合式API

    Vue3組合式API

    目錄 composition API vs options API 體驗 composition API setup 函數(shù) reactive 函數(shù) ref 函數(shù) script setup 語法 計算屬性 computed 函數(shù) 監(jiān)聽器 watch 函數(shù) 生命周期 模板中 ref 的使用 組件通訊 - 父傳子 組件通訊 - 子傳父 依賴注入 - provide 和 inject 保持響應(yīng)式 - toRefs 函數(shù) vue2 采用的就是 options API (

    2024年02月07日
    瀏覽(22)
  • vue3組合式API介紹

    根據(jù)官方的說法,vue3.0的變化包括性能上的改進、更小的 bundle 體積、對 TypeScript 更好的支持、用于處理大規(guī)模用例的全新 API,全新的api指的就是本文主要要說的組合式api。 在 vue3 版本之前,我們復(fù)用組件(或者提取和重用多個組件之間的邏輯),通常有以下幾種方式: M

    2023年04月22日
    瀏覽(24)
  • 快速入門vue3組合式API

    快速入門vue3組合式API

    (創(chuàng)作不易,感謝有你,你的支持,就是我前行的最大動力,如果看完對你有幫助,請留下您的足跡) 目錄 使用create-vue創(chuàng)建項目 熟悉項目目錄和關(guān)鍵文件? 組合式API? setup選項 setup選項的寫法和執(zhí)行時機 script setup?語法糖 reactive和ref函數(shù) reactive() ref() computed watch 偵聽單個數(shù)據(jù)

    2024年02月12日
    瀏覽(22)
  • Vue3 組合式函數(shù),實現(xiàn)minxins

    Vue3 組合式函數(shù),實現(xiàn)minxins

    截至目前,組合式函數(shù)應(yīng)該是在VUE 3應(yīng)用程序中組織業(yè)務(wù)邏輯最佳的方法。它讓我們可以把一些小塊的通用邏輯進行抽離、復(fù)用,使我們的代碼更易于編寫、閱讀和維護。 根據(jù)官方文檔說明,在 Vue 應(yīng)用的概念中, “組合式函數(shù)”是一個利用 Vue 組合式 API 來封裝和復(fù)用有狀態(tài)

    2024年02月08日
    瀏覽(20)
  • 【Vue3】如何創(chuàng)建Vue3項目及組合式API

    【Vue3】如何創(chuàng)建Vue3項目及組合式API

    文章目錄 前言 一、如何創(chuàng)建vue3項目? ①使用 vue-cli 創(chuàng)建 ?②使用可視化ui創(chuàng)建 ?③npm init vite-app? ?④npm init vue@latest 二、 API 風(fēng)格 2.1 選項式 API (Options API) 2.2 組合式 API (Composition API) 總結(jié) 例如:隨著前端領(lǐng)域的不斷發(fā)展,vue3學(xué)習(xí)這門技術(shù)也越來越重要,很多人都開啟了學(xué)習(xí)

    2024年02月03日
    瀏覽(14)
  • vue3:7、組合式API-watch

    vue3:7、組合式API-watch

    ?

    2024年02月09日
    瀏覽(23)
  • 帶你了解vue3組合式api基本寫法

    本文的目的,是為了讓已經(jīng)有 Vue2 開發(fā)經(jīng)驗的 人 ,快速掌握 Vue3 的寫法。 因此, 本篇假定你已經(jīng)掌握 Vue 的核心內(nèi)容 ,只為你介紹編寫 Vue3 代碼,需要了解的內(nèi)容。 一、Vue3 里 script 的三種寫法 首先,Vue3 新增了一個叫做組合式 api 的東西,英文名叫 Composition API。因此 Vu

    2024年02月01日
    瀏覽(20)
  • vue3組合式寫法在方法中出發(fā)點擊事件

    vue3組合式寫法在方法中出發(fā)點擊事件

    問: 用vue3組合式寫法,如何在一個方法中調(diào)用a標(biāo)簽的點擊事件 回答: 在Vue 3的組合式API中,可以通過ref來獲取DOM元素的引用,然后使用$el屬性訪問DOM元素并觸發(fā)其點擊事件。下面是示例代碼: 在上述代碼中,首先通過ref創(chuàng)建了一個名為linkRef的引用變量,并將其初始化為

    2024年02月15日
    瀏覽(20)
  • vue3 組合式api中 ref 和$parent 的使用

    vue3 組合式api中 ref 和$parent 的使用

    ref 的使用 vue3中, 在 組件中添加一個 component ref=“xxx” ,就可以在父組件中得到 子組件的 dom 對象, 以及 虛擬的 dom 對象, 有了虛擬 dom, 我們就可以在父組件中控制子組件的顯示了 ref 的使用方法 vue3中ref 的特點 以上如果在vue2中,就可以使用 子組件的對象來改變子組件的

    2024年02月10日
    瀏覽(41)
  • Vue3的組合式API中如何使用provide/inject?

    聽說 Vue 3 加入了超多酷炫的新功能,比如組合式 API 等等。今天我們就來聊聊 Vue 3 中的組合式 API,并且如何使用 provide/inject 來搞定它! 首先,我們來了解一下組合式 API 是什么。其實,組合式 API 就是一個用來構(gòu)建和組合函數(shù)的工具,它能讓我們的代碼更加簡潔、可讀性更

    2024年02月11日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包