vue3+el-table封裝示例詳解(編輯、刪除、查看詳情按鈕一起封裝)
vue3+el-table封裝(編輯、刪除、查看詳情按鈕一起封裝),具體代碼如下所示:
// 封裝源碼(子組件)
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column
v-for="(column, index) in tableDataHeader"
:label="column.label"
:key="index"
:prop="column.prop"
:width="column.width"
>
<template #default="scope" v-if="column.operate">
<el-button
v-for="item in column.children"
:key="item.prop"
:type="item.type"
@click="btnClick(item.method, scope.row, scope.$index)"
>{{ item.label }}</el-button
>
</template>
</el-table-column>
</el-table>
</template>
<script setup lang="ts">
const props = defineProps<{
tableData: Array<any>
tableDataHeader: Array<any>
}>()
const emits = defineEmits(['deleteRow', 'editRow', 'detailRow'])
const btnClick = (method, row, index) => {
console.log('method: ', method)
emits(method, row, index)
}
</script>
<style scoped></style>// 父組件調(diào)用
<template>
<CustomTable
:tableData="tableData"
:tableDataHeader="tableDataHeader"
@deleteRow="deleteRow"
@editRow="edit"
@detailRow="detail"
>
</CustomTable>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref, type Ref } from 'vue'
import CustomTable from '@/components/Custom-table.vue'
import { data } from '@/data/data.ts'
const tableData: Ref<Array> = ref(data.tableData)
const tableDataHeader = ref(data.tableDataHeader)
const deleteRow = (row: any, index: number) => {
tableData.value.splice(index, 1)
console.log('this tableData: ', tableData)
pagenation.value.total = tableData.value.length
}
const edit = (row, index) => {
console.log('row: ', row, index)
}
const detail = (row, index) => {
console.log('row: ', row, index)
}
</script>
<style scoped></style>對(duì)應(yīng)的tableData和tableDataHeader文件(實(shí)際開發(fā)中,應(yīng)該從后端拿tableData,tableHeader根據(jù)情況自定義)
export const data = {
tableData: [
{
name: 'knife1',
date: '2024-09-22',
type: 'butterfly'
},
{
name: 'knife2',
date: '2024-09-22',
type: 'M9'
},
{
name: 'knife3',
date: '2024-09-22',
type: 'butterfly'
}
],
tableDataHeader: [
{
label: 'Knife Name',
prop: 'name',
width: 180
},
{
label: 'Favorite Date',
prop: 'date',
width: 180
},
{
label: 'Knife Type',
prop: 'type',
width: 180
},
{
label: 'Operation',
operate: true,
prop: 'Operation',
width: '280',
children: [
{
label: 'edit',
prop: 'edit',
method: 'editRow',
type: 'primary'
},
{
label: 'Delete',
prop: 'Delete',
method: 'deleteRow',
type: 'warning'
},
{
label: 'Detail',
prop: 'Detail',
method: 'detailRow',
type: 'info'
}
]
}
]
}到此這篇關(guān)于vue3+el-table封裝示例詳解(編輯、刪除、查看詳情按鈕一起封裝)的文章就介紹到這了,更多相關(guān)vue3 el-tale封裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3.2+ts實(shí)現(xiàn)在方法中可調(diào)用的擬態(tài)框彈窗(類el-MessageBox)
這篇文章主要介紹了vue3.2+ts實(shí)現(xiàn)在方法中可調(diào)用的擬態(tài)框彈窗(類el-MessageBox),這個(gè)需求最主要的是要通過(guò)方法去調(diào)用,為了像el-messagebox使用那樣方便,需要的朋友可以參考下2022-12-12
Vue使用Echarts實(shí)現(xiàn)大屏可視化布局示例詳細(xì)講解
這篇文章主要介紹了Vue使用Echarts實(shí)現(xiàn)大屏可視化布局示例,本文通過(guò)實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01
Vue雙向數(shù)據(jù)綁定與響應(yīng)式原理深入探究
本節(jié)介紹雙向數(shù)據(jù)綁定以及響應(yīng)式的原理,回答了雙向數(shù)據(jù)綁定和數(shù)據(jù)響應(yīng)式是否相同,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-08-08
從0到1解鎖Element-Plus組件二次封裝El-Dialog動(dòng)態(tài)調(diào)用的原理解析
作者通過(guò)二次封裝Element-Plus的el-dialog組件,實(shí)現(xiàn)動(dòng)態(tài)顯示與數(shù)據(jù)傳遞功能,提升項(xiàng)目靈活性和代碼復(fù)用性,適用于用戶編輯、文件上傳確認(rèn)、權(quán)限管理等多場(chǎng)景需求,本文給大家介紹從0到1解鎖Element-Plus組件二次封裝El-Dialog動(dòng)態(tài)調(diào)用的原理解析,感興趣的朋友一起看看吧2025-07-07
vue路由history模式頁(yè)面刷新404解決方法Koa?Express
這篇文章主要為大家介紹了vue路由history模式頁(yè)面刷新404解決方法(Koa?Express)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
手把手教你搭建一個(gè)vue項(xiàng)目的完整步驟
身為入行未深的小白前端,不斷的學(xué)習(xí)是我們不可丟失的習(xí)慣,前端流行的框架也是層出不窮,vue在眾多框架中脫穎而出,下面這篇文章主要給大家介紹了關(guān)于搭建一個(gè)vue項(xiàng)目的完整步驟,需要的朋友可以參考下2022-07-07

