vue3 + ElementPlus 封裝列表表格組件包含分頁
在前端開發(fā)中,封裝組件是必不可少的。今天就來封裝一個通用的列表表格組件,包含分頁功能,可以提高代碼的復用性和可維護性。
1. 組件設計
Props:
- tableData:表格數據。
- columns:表格列配置。
- total:總條數。
- loading:加載狀態(tài)。
- pagination:分頁配置(當前頁、每頁條數)。
Events:
- update:pagination:分頁變化時觸發(fā)。
- refresh:刷新數據時觸發(fā)。
Slots:
- 自定義列內容。
- 自定義操作按鈕。
2. 封裝代碼
TableWithPagination.vue
<template> <div class="table-with-pagination"> <!-- 表格 --> <el-table :data="tableData" border stripe v-loading="loading" style="width: 100%" > <!-- 動態(tài)列 --> <el-table-column v-for="column in columns" :key="column.prop" :prop="column.prop" :label="column.label" :width="column.width" :align="column.align || 'center'" > <!-- 自定義列內容 --> <template #default="scope" v-if="column.slot"> <slot :name="column.slot" :row="scope.row"></slot> </template> </el-table-column> <!-- 操作列 --> <el-table-column v-if="$slots.actions" label="操作" align="center" :width="actionsWidth" > <template #default="scope"> <slot name="actions" :row="scope.row"></slot> </template> </el-table-column> </el-table> <!-- 分頁 --> <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="pagination.pageSize" :current-page="pagination.pageNo" @size-change="handleSizeChange" @current-change="handleCurrentChange" /> </div> </template> <script setup> import { ref, watch } from 'vue'; const props = defineProps({ tableData: { type: Array, default: () => [], }, columns: { type: Array, default: () => [], }, total: { type: Number, default: 0, }, loading: { type: Boolean, default: false, }, pagination: { type: Object, default: () => ({ pageNo: 1, pageSize: 10, }), }, actionsWidth: { type: String, default: '180', }, }); const emit = defineEmits(['update:pagination', 'refresh']); // 分頁大小變化 const handleSizeChange = (pageSize) => { emit('update:pagination', { ...props.pagination, pageSize }); emit('refresh'); }; // 當前頁變化 const handleCurrentChange = (pageNo) => { emit('update:pagination', { ...props.pagination, pageNo }); emit('refresh'); }; </script> <style scoped> .table-with-pagination { margin-top: 20px; } .pagination { margin-top: 20px; text-align: right; } </style>
3. 使用示例
<template> <div> <!-- 搜索欄 --> <el-form :inline="true" :model="queryParams"> <el-form-item label="任務名稱"> <el-input v-model="queryParams.taskName" placeholder="請輸入任務名稱" /> </el-form-item> <el-form-item> <el-button type="primary" @click="handleSearch">搜索</el-button> </el-form-item> </el-form> <!-- 表格組件 --> <TableWithPagination :table-data="tableData" :columns="columns" :total="total" :loading="loading" :pagination="pagination" @update:pagination="handlePaginationChange" @refresh="fetchData" > <!-- 自定義列 --> <template #status="{ row }"> <el-tag :type="row.status === 1 ? 'success' : 'danger'"> { { row.status === 1 ? '啟用' : '禁用' }} </el-tag> </template> <!-- 操作列 --> <template #actions="{ row }"> <el-button type="primary" size="small" @click="handleEdit(row)"> 編輯 </el-button> <el-button type="danger" size="small" @click="handleDelete(row)"> 刪除 </el-button> </template> </TableWithPagination> </div> </template> <script setup> import { ref, onMounted } from 'vue'; import TableWithPagination from './components/TableWithPagination.vue'; import { fetchTaskList } from '@/api/task'; // 假設有一個獲取任務列表的 API // 表格列配置 const columns = [ { prop: 'taskName', label: '任務名稱' }, { prop: 'taskType', label: '任務類型' }, { prop: 'status', label: '狀態(tài)', slot: 'status' }, // 使用自定義列 ]; // 表格數據 const tableData = ref([]); const total = ref(0); const loading = ref(false); // 查詢參數 const queryParams = ref({ taskName: '', }); // 分頁參數 const pagination = ref({ pageNo: 1, pageSize: 10, }); // 獲取數據 const fetchData = async () => { try { loading.value = true; const res = await fetchTaskList({ ...queryParams.value, ...pagination.value, }); tableData.value = res.data.list; total.value = res.data.total; } catch (error) { console.error('獲取數據失敗:', error); } finally { loading.value = false; } }; // 分頁變化 const handlePaginationChange = (newPagination) => { pagination.value = newPagination; fetchData(); }; // 搜索 const handleSearch = () => { pagination.value.pageNo = 1; // 重置頁碼 fetchData(); }; // 編輯 const handleEdit = (row) => { console.log('編輯:', row); }; // 刪除 const handleDelete = (row) => { console.log('刪除:', row); }; // 初始化加載數據 onMounted(() => { fetchData(); }); </script>
父組件中使用 TableWithPagination以上就是封裝 Vue 3 和 Element Plus 中封裝一個通用的列表表格組件,將表格和分頁邏輯封裝在一個組件中,便于維護和擴展。
到此這篇關于vue3 + ElementPlus 封裝列表表格組件包含分頁的文章就介紹到這了,更多相關vue ElementPlus封裝表格組件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue3中使用keepAlive緩存路由組件不生效的問題解決
這篇文章主要介紹了vue3中使用keepAlive緩存路由組件不生效的問題解決,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-06-06Vue Element前端應用開發(fā)之動態(tài)菜單和路由的關聯處理
這篇文章主要介紹了Vue Element前端應用開發(fā)之動態(tài)菜單和路由的關聯處理,對vue感興趣的同學,可以參考下2021-05-05vue3的介紹和兩種創(chuàng)建方式詳解(cli和vite)
這篇文章主要介紹了vue3的介紹和兩種創(chuàng)建方式(cli和vite),vue3對比vue2帶來的性能提升有很多優(yōu)勢,總體來說Vue 3在性能、開發(fā)體驗和代碼組織方面都有所改進,使得它更加適合于大型、復雜的應用程序開發(fā),需要的朋友可以參考下2023-04-04vue在install時node-sass@4.14.1?postinstall:node?scripts/buil
最近在npm install 的時候遇到了個問題,所以給大家總結下,下面這篇文章主要給大家介紹了關于vue在install時node-sass@4.14.1?postinstall:node?scripts/build.js錯誤的解決方法,需要的朋友可以參考下2023-05-05Element-UI介紹主題定制、自定義組件和插件擴展的代碼示例
本文介紹了使用Element-UI實現主題定制、自定義組件和擴展插件的方法和實用案例,在開發(fā)過程中,我們可以根據自己的需求,靈活選擇相關的技術手段,并不斷探索和嘗試,以提高開發(fā)效率和用戶體驗,感興趣的朋友跟隨小編一起看看吧2024-02-02