Vue?Hook?封裝通用型表格的詳細過程
一、創(chuàng)建通用型表格的需求
實現(xiàn)一個通用型表格組件,具備以下功能:
- 動態(tài)列配置。
- 分頁功能。
- 排序功能。
- 可擴展的行操作功能。
二、設(shè)計通用型表格組件
首先,需要設(shè)計一個基礎(chǔ)的表格組件,它接受列配置、數(shù)據(jù)和分頁信息等參數(shù)。
1. 創(chuàng)建 useTable Hook
在 src/hooks
目錄下創(chuàng)建 useTable.js
文件:
import { ref, reactive, onMounted, toRefs } from 'vue'; export function useTable(fetchData) { const state = reactive({ loading: false, data: [], pagination: { currentPage: 1, pageSize: 10, total: 0, }, sort: { field: '', order: '', }, }); const loadData = async () => { state.loading = true; const { currentPage, pageSize } = state.pagination; const { field, order } = state.sort; const result = await fetchData(currentPage, pageSize, field, order); state.data = result.data; state.pagination.total = result.total; state.loading = false; }; const changePage = (page) => { state.pagination.currentPage = page; loadData(); }; const changePageSize = (size) => { state.pagination.pageSize = size; loadData(); }; const changeSort = (field, order) => { state.sort.field = field; state.sort.order = order; loadData(); }; onMounted(() => { loadData(); }); return { ...toRefs(state), loadData, changePage, changePageSize, changeSort, }; }
2. 創(chuàng)建 TableComponent.vue
在 src/components
目錄下創(chuàng)建 TableComponent.vue
文件:
<template> <div> <table> <thead> <tr> <th v-for="col in columns" :key="col.key" @click="changeSort(col.key)"> {{ col.title }} <span v-if="sort.field === col.key">{{ sort.order === 'asc' ? '↑' : '↓' }}</span> </th> </tr> </thead> <tbody> <tr v-for="row in data" :key="row.id"> <td v-for="col in columns" :key="col.key">{{ row[col.key] }}</td> </tr> </tbody> </table> <div class="pagination"> <button @click="changePage(pagination.currentPage - 1)" :disabled="pagination.currentPage === 1">Previous</button> <span>{{ pagination.currentPage }} / {{ Math.ceil(pagination.total / pagination.pageSize) }}</span> <button @click="changePage(pagination.currentPage + 1)" :disabled="pagination.currentPage === Math.ceil(pagination.total / pagination.pageSize)">Next</button> </div> </div> </template> <script> import { ref, onMounted } from 'vue'; import { useTable } from '@/hooks/useTable'; export default { props: { fetchData: { type: Function, required: true, }, columns: { type: Array, required: true, }, }, setup(props) { const { data, loading, pagination, sort, loadData, changePage, changePageSize, changeSort } = useTable(props.fetchData); return { data, loading, pagination, sort, loadData, changePage, changePageSize, changeSort, }; }, }; </script> <style scoped> .pagination { display: flex; justify-content: center; margin-top: 10px; } </style>
三、使用通用型表格組件
在實際項目中,可以這樣使用這個通用型表格組件:
1. 創(chuàng)建 ExampleTable.vue 組件
在 src/views
目錄下創(chuàng)建 ExampleTable.vue
文件:
<template> <div> <TableComponent :fetchData="fetchData" :columns="columns" /> </div> </template> <script> import TableComponent from '@/components/TableComponent.vue'; export default { components: { TableComponent, }, setup() { const columns = [ { key: 'name', title: 'Name' }, { key: 'age', title: 'Age' }, { key: 'email', title: 'Email' }, ]; const fetchData = async (page, pageSize, sortField, sortOrder) => { // 模擬數(shù)據(jù)獲取 const total = 100; const data = Array.from({ length: pageSize }, (v, i) => ({ id: (page - 1) * pageSize + i + 1, name: `Name ${(page - 1) * pageSize + i + 1}`, age: 20 + ((page - 1) * pageSize + i + 1) % 30, email: `user${(page - 1) * pageSize + i + 1}@example.com`, })); return { data, total }; }; return { columns, fetchData, }; }, }; </script>
四、解釋代碼
定義
useTable
Hook:- 使用 Vue 的
ref
和reactive
定義表格狀態(tài)。 - 定義
loadData
、changePage
、changePageSize
和changeSort
函數(shù)來處理數(shù)據(jù)加載和分頁、排序變化。 - 使用
onMounted
生命周期鉤子在組件掛載時加載數(shù)據(jù)。
- 使用 Vue 的
定義
TableComponent
組件:- 接受
fetchData
和columns
作為組件屬性。 - 使用
useTable
Hook 獲取表格數(shù)據(jù)和操作函數(shù)。 - 渲染表格頭部、主體和分頁組件,并綁定相關(guān)事件。
- 接受
使用通用型表格組件:
- 在
ExampleTable.vue
中定義列配置和數(shù)據(jù)獲取函數(shù)。 - 使用
TableComponent
并傳遞fetchData
和columns
屬性。
- 在
到此這篇關(guān)于Vue Hook 封裝通用型表格的文章就介紹到這了,更多相關(guān)Vue Hook 封裝通用型表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Vue實現(xiàn)可選擇不連續(xù)的時間范圍的日期選擇器
這篇文章主要為大家詳細介紹了如何基于Vue.js實現(xiàn)一個可選擇不連續(xù)的時間范圍的日期選擇器,文中的示例代碼簡潔易懂,需要的可以參考一下2023-06-06vue通過tailwindcss實現(xiàn)class動態(tài)綁定
這篇文章主要介紹了vue通過tailwindcss實現(xiàn)class動態(tài)綁定,文中給大家介紹了一些常用類名語法記錄,對vue動態(tài)綁定class相關(guān)知識感興趣的朋友一起看看吧2023-07-07Vue?Router修改query參數(shù)url參數(shù)沒有變化問題及解決
這篇文章主要介紹了Vue?Router修改query參數(shù)url參數(shù)沒有變化問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09利用Vue-draggable組件實現(xiàn)Vue項目中表格內(nèi)容的拖拽排序
這篇文章主要介紹了利用Vue-draggable組件實現(xiàn)Vue項目中表格內(nèi)容的拖拽排序,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06vue中的render函數(shù)、h()函數(shù)、函數(shù)式組件詳解
在vue中我們使用模板HTML語法來組建頁面的,使用render函數(shù)我們可以用js語言來構(gòu)建DOM,這篇文章主要介紹了vue中的render函數(shù)、h()函數(shù)、函數(shù)式組件,需要的朋友可以參考下2023-02-02基于Vue el-autocomplete 實現(xiàn)類似百度搜索框功能
本文通過代碼給大家介紹了Vue el-autocomplete 實現(xiàn)類似百度搜索框功能,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10