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í)際開(kāi)發(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)文章
vue element-ui實(shí)現(xiàn)input輸入框金額數(shù)字添加千分位
這篇文章主要介紹了vue element-ui實(shí)現(xiàn)input輸入框金額數(shù)字添加千分位,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12vue實(shí)現(xiàn)虛擬滾動(dòng)渲染成千上萬(wàn)條數(shù)據(jù)
本文主要介紹了vue實(shí)現(xiàn)虛擬滾動(dòng)渲染成千上萬(wàn)條數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Vue路由傳遞參數(shù)與重定向的使用方法總結(jié)
路由的本質(zhì)就是一種對(duì)應(yīng)關(guān)系,比如說(shuō)我們?cè)趗rl地址中輸入我們要訪問(wèn)的url地址之后,瀏覽器要去請(qǐng)求這個(gè)url地址對(duì)應(yīng)的資源,下面這篇文章主要給大家介紹了關(guān)于Vue路由傳遞參數(shù)與重定向的使用方法,需要的朋友可以參考下2022-10-10vue使用formData時(shí)候傳遞參數(shù)是個(gè)空值的情況處理
這篇文章主要介紹了vue使用formData時(shí)候傳遞參數(shù)是個(gè)空值的情況處理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05vue.nextTick()與setTimeout的區(qū)別及說(shuō)明
這篇文章主要介紹了vue.nextTick()與setTimeout的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03el-table?fixed固定列導(dǎo)致錯(cuò)位的解決方法介紹
ElementUI中el-table設(shè)置指定列固定不動(dòng),不受滾動(dòng)條影響,下面這篇文章主要給大家介紹了關(guān)于el-table?fixed固定列導(dǎo)致錯(cuò)位的解決方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11vue+mousemove實(shí)現(xiàn)鼠標(biāo)拖動(dòng)功能(拖動(dòng)過(guò)快失效問(wèn)題解決方法)
這篇文章主要介紹了vue+mousemove實(shí)現(xiàn)鼠標(biāo)拖動(dòng)功能,文中給大家介紹了鼠標(biāo)移動(dòng)過(guò)快拖動(dòng)就失效問(wèn)題的解決方法,需要的朋友可以參考下2018-08-08