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>對應的tableData和tableDataHeader文件(實際開發(fā)中,應該從后端拿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'
}
]
}
]
}到此這篇關于vue3+el-table封裝示例詳解(編輯、刪除、查看詳情按鈕一起封裝)的文章就介紹到這了,更多相關vue3 el-tale封裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue element-ui實現(xiàn)input輸入框金額數(shù)字添加千分位
這篇文章主要介紹了vue element-ui實現(xiàn)input輸入框金額數(shù)字添加千分位,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12
vue實現(xiàn)虛擬滾動渲染成千上萬條數(shù)據(jù)
本文主要介紹了vue實現(xiàn)虛擬滾動渲染成千上萬條數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02
vue使用formData時候傳遞參數(shù)是個空值的情況處理
這篇文章主要介紹了vue使用formData時候傳遞參數(shù)是個空值的情況處理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
vue.nextTick()與setTimeout的區(qū)別及說明
這篇文章主要介紹了vue.nextTick()與setTimeout的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
vue+mousemove實現(xiàn)鼠標拖動功能(拖動過快失效問題解決方法)
這篇文章主要介紹了vue+mousemove實現(xiàn)鼠標拖動功能,文中給大家介紹了鼠標移動過快拖動就失效問題的解決方法,需要的朋友可以參考下2018-08-08

