vue3 + ElementPlus Encapsulated list table component contains paging
In front-end development, encapsulation of components is essential. Today, we will encapsulate a common list table component, including paging functions, which can improve code reusability and maintainability.
1. Component design
Props:
- tableData: Table data.
- columns: table column configuration.
- total: total number of entries.
- loading: loading status.
- pagination: pagination configuration (current page, number of bars per page).
Events:
- update:pagination: Triggered when the pagination changes.
- refresh: fired when refreshing data.
Slots:
- Customize the column content.
- Custom action buttons.
2. Encapsulation code
<template> <div class="table-with-pagination"> <!-- sheet --> <el-table :data="tableData" border stripe v-loading="loading" style="width: 100%" > <!-- Dynamic columns --> <el-table-column v-for="column in columns" :key="" :prop="" :label="" :width="" :align=" || 'center'" > <!-- Customize column content --> <template #default="scope" v-if=""> <slot :name="" :row=""></slot> </template> </el-table-column> <!-- Operation column --> <el-table-column v-if="$" label="operate" align="center" :width="actionsWidth" > <template #default="scope"> <slot name="actions" :row=""></slot> </template> </el-table-column> </el-table> <!-- Pagination --> <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="" :current-page="" @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']); // Change of page sizeconst handleSizeChange = (pageSize) => { emit('update:pagination', { ..., pageSize }); emit('refresh'); }; // Changes to the current pageconst handleCurrentChange = (pageNo) => { emit('update:pagination', { ..., pageNo }); emit('refresh'); }; </script> <style scoped> .table-with-pagination { margin-top: 20px; } .pagination { margin-top: 20px; text-align: right; } </style>
3. Use example
<template> <div> <!-- Search bar --> <el-form :inline="true" :model="queryParams"> <el-form-item label="Task Name"> <el-input v-model="" placeholder="Please enter the task name" /> </el-form-item> <el-form-item> <el-button type="primary" @click="handleSearch">search</el-button> </el-form-item> </el-form> <!-- Table Components --> <TableWithPagination :table-data="tableData" :columns="columns" :total="total" :loading="loading" :pagination="pagination" @update:pagination="handlePaginationChange" @refresh="fetchData" > <!-- Custom columns --> <template #status="{ row }"> <el-tag :type=" === 1 ? 'success' : 'danger'"> { { === 1 ? 'Enable' : 'Disable' }} </el-tag> </template> <!-- Operation column --> <template #actions="{ row }"> <el-button type="primary" size="small" @click="handleEdit(row)"> edit </el-button> <el-button type="danger" size="small" @click="handleDelete(row)"> delete </el-button> </template> </TableWithPagination> </div> </template> <script setup> import { ref, onMounted } from 'vue'; import TableWithPagination from './components/'; import { fetchTaskList } from '@/api/task'; // Suppose there is an API to get the task list// Table column configurationconst columns = [ { prop: 'taskName', label: 'Task Name' }, { prop: 'taskType', label: 'Task Type' }, { prop: 'status', label: 'state', slot: 'status' }, // Use custom columns]; // Table dataconst tableData = ref([]); const total = ref(0); const loading = ref(false); // Query parametersconst queryParams = ref({ taskName: '', }); //Pagination parametersconst pagination = ref({ pageNo: 1, pageSize: 10, }); // Get dataconst fetchData = async () => { try { = true; const res = await fetchTaskList({ ..., ..., }); = ; = ; } catch (error) { ('Failed to get data:', error); } finally { = false; } }; // Pagination changesconst handlePaginationChange = (newPagination) => { = newPagination; fetchData(); }; // searchconst handleSearch = () => { = 1; // Reset page number fetchData(); }; // editconst handleEdit = (row) => { ('edit:', row); }; // deleteconst handleDelete = (row) => { ('delete:', row); }; // Initialize load dataonMounted(() => { fetchData(); }); </script>
Using TableWithPagination in the parent component is to encapsulate a common list table component in Vue 3 and Element Plus, encapsulating tables and paging logic in one component for easy maintenance and expansion.
This is the article about the vue3 + ElementPlus encapsulated list table component containing paging. For more related contents of vue ElementPlus encapsulated table component, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!
Related Articles
Detailed explanation of vue3 axios installation and usage examples
This article mainly introduces the sample code for vue3 axios installation and use. This article introduces you very detailed through the sample code. Interested friends follow the editor to take a look.2024-04-04How to dynamically splice the variables after this in vue
This article mainly introduces how to dynamically splice the variables after this in vue, which is of good reference value and hopes to be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice2023-07-07Vuejs2 + Webpack framework, example explanation of simulation download
Today, the editor will share with you an example explanation of simulation download in the Vuejs2 + Webpack framework. It has good reference value and I hope it will be helpful to everyone. Let's take a look with the editor2018-09-09Let’s learn how to communicate between components of Vue
This article mainly introduces the communication method between Vue components in detail. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it. I hope it can help you.2022-03-03Introduction to the usage of props in Vue
This article mainly shares with you the introduction of the usage of props in Vue. In Vue, props can connect originally isolated components, that is, children can receive data passed by the parent component. Let's enter the article together to see the detailed introduction of the content. Friends who need it can also refer to it.2021-11-11Detailed explanation of the computed attribute instance in vue
Expressions within templates are very convenient, but they were designed for simple operations. Next, through this article, I will introduce the computing properties in vue. If you need it, please refer to it.2018-09-09Vuejs implements method to dynamically change css styles for tag tabs
This article mainly introduces the method of implementing the tag tab of vuejs - dynamically changing the css style. The code is divided into html and js. Friends who need it can refer to it2018-05-05vue using element ui to customize mobile phone verification rules issues
This article mainly introduces the issue of using element UI to customize mobile phone verification rules for vue, which is of good reference value and hopes to be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice2022-12-12vue uses the table table in ant design and the event operation triggered when clicking on a certain row
This article mainly introduces the event operation triggered when clicking on a certain row using the table table in vue. It has good reference value and hopes it will be helpful to everyone. Let's take a look with the editor2020-10-10Some tips for using less in vue project
I have been learning vue some time ago and started to record the problems I encountered. The following article mainly introduces some tips on using less in the vue project. The article introduces the example code in detail. Friends who need it can refer to it.2021-09-09