欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3 + ElementPlus 封裝列表表格組件包含分頁(yè)

 更新時(shí)間:2025年02月06日 09:37:04   作者:scorpion_V  
文章介紹了如何在Vue3和ElementPlus中封裝一個(gè)包含分頁(yè)功能的通用列表表格組件,組件通過(guò)props接收表格數(shù)據(jù)、列配置、總條數(shù)、加載狀態(tài)和分頁(yè)配置,并通過(guò)events處理分頁(yè)和刷新事件,此外,還提供了自定義列內(nèi)容和操作按鈕的功能,感興趣的朋友跟隨小編一起看看吧

在前端開(kāi)發(fā)中,封裝組件是必不可少的。今天就來(lái)封裝一個(gè)通用的列表表格組件,包含分頁(yè)功能,可以提高代碼的復(fù)用性和可維護(hù)性。

1. 組件設(shè)計(jì)

Props:

  • tableData:表格數(shù)據(jù)。
  • columns:表格列配置。
  • total:總條數(shù)。
  • loading:加載狀態(tài)。
  • pagination:分頁(yè)配置(當(dāng)前頁(yè)、每頁(yè)條數(shù))。

Events:

  • update:pagination:分頁(yè)變化時(shí)觸發(fā)。
  • refresh:刷新數(shù)據(jù)時(shí)觸發(fā)。

Slots:

  • 自定義列內(nèi)容。
  • 自定義操作按鈕。

2. 封裝代碼

TableWithPagination.vue

<template>
  <div class="table-with-pagination">
    <!-- 表格 -->
    <el-table
      :data="tableData"
      border
      stripe
      v-loading="loading"
      style="width: 100%"
    >
      <!-- 動(dòng)態(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'"
      >
        <!-- 自定義列內(nèi)容 -->
        <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>
    <!-- 分頁(yè) -->
    <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']);
// 分頁(yè)大小變化
const handleSizeChange = (pageSize) => {
  emit('update:pagination', { ...props.pagination, pageSize });
  emit('refresh');
};
// 當(dāng)前頁(yè)變化
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="任務(wù)名稱(chēng)">
        <el-input v-model="queryParams.taskName" placeholder="請(qǐng)輸入任務(wù)名稱(chēng)" />
      </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'; // 假設(shè)有一個(gè)獲取任務(wù)列表的 API
// 表格列配置
const columns = [
  { prop: 'taskName', label: '任務(wù)名稱(chēng)' },
  { prop: 'taskType', label: '任務(wù)類(lèi)型' },
  { prop: 'status', label: '狀態(tài)', slot: 'status' }, // 使用自定義列
];
// 表格數(shù)據(jù)
const tableData = ref([]);
const total = ref(0);
const loading = ref(false);
// 查詢(xún)參數(shù)
const queryParams = ref({
  taskName: '',
});
// 分頁(yè)參數(shù)
const pagination = ref({
  pageNo: 1,
  pageSize: 10,
});
// 獲取數(shù)據(jù)
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('獲取數(shù)據(jù)失?。?, error);
  } finally {
    loading.value = false;
  }
};
// 分頁(yè)變化
const handlePaginationChange = (newPagination) => {
  pagination.value = newPagination;
  fetchData();
};
// 搜索
const handleSearch = () => {
  pagination.value.pageNo = 1; // 重置頁(yè)碼
  fetchData();
};
// 編輯
const handleEdit = (row) => {
  console.log('編輯:', row);
};
// 刪除
const handleDelete = (row) => {
  console.log('刪除:', row);
};
// 初始化加載數(shù)據(jù)
onMounted(() => {
  fetchData();
});
</script>

父組件中使用 TableWithPagination以上就是封裝 Vue 3 和 Element Plus 中封裝一個(gè)通用的列表表格組件,將表格和分頁(yè)邏輯封裝在一個(gè)組件中,便于維護(hù)和擴(kuò)展。

到此這篇關(guān)于vue3 + ElementPlus 封裝列表表格組件包含分頁(yè)的文章就介紹到這了,更多相關(guān)vue ElementPlus封裝表格組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue3中使用keepAlive緩存路由組件不生效的問(wèn)題解決

    vue3中使用keepAlive緩存路由組件不生效的問(wèn)題解決

    這篇文章主要介紹了vue3中使用keepAlive緩存路由組件不生效的問(wèn)題解決,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-06-06
  • Vue Element前端應(yīng)用開(kāi)發(fā)之動(dòng)態(tài)菜單和路由的關(guān)聯(lián)處理

    Vue Element前端應(yīng)用開(kāi)發(fā)之動(dòng)態(tài)菜單和路由的關(guān)聯(lián)處理

    這篇文章主要介紹了Vue Element前端應(yīng)用開(kāi)發(fā)之動(dòng)態(tài)菜單和路由的關(guān)聯(lián)處理,對(duì)vue感興趣的同學(xué),可以參考下
    2021-05-05
  • Vue列表渲染的示例代碼

    Vue列表渲染的示例代碼

    這篇文章主要介紹了Vue列表渲染的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • Vue組件通信深入分析

    Vue組件通信深入分析

    對(duì)于vue來(lái)說(shuō),組件之間的消息傳遞是非常重要的,用vue可以是要組件復(fù)用的,而組件實(shí)例的作用域是相互獨(dú)立,這意味著不同組件之間的數(shù)據(jù)無(wú)法互相引用,一般來(lái)說(shuō),組件之間可以有幾種關(guān)系,下面是我對(duì)組件之間消息傳遞的常用方式的總結(jié)
    2022-08-08
  • vue項(xiàng)目中公用footer組件底部位置的適配問(wèn)題

    vue項(xiàng)目中公用footer組件底部位置的適配問(wèn)題

    footer為公用組件,其他頁(yè)面都需要引入。接下來(lái)通過(guò)本文給大家分享vue項(xiàng)目中公用footer組件底部位置的適配問(wèn)題,需要的朋友可以參考下
    2018-05-05
  • vue3的介紹和兩種創(chuàng)建方式詳解(cli和vite)

    vue3的介紹和兩種創(chuàng)建方式詳解(cli和vite)

    這篇文章主要介紹了vue3的介紹和兩種創(chuàng)建方式(cli和vite),vue3對(duì)比vue2帶來(lái)的性能提升有很多優(yōu)勢(shì),總體來(lái)說(shuō)Vue 3在性能、開(kāi)發(fā)體驗(yàn)和代碼組織方面都有所改進(jìn),使得它更加適合于大型、復(fù)雜的應(yīng)用程序開(kāi)發(fā),需要的朋友可以參考下
    2023-04-04
  • vuex狀態(tài)管理數(shù)據(jù)狀態(tài)查詢(xún)與更改方式

    vuex狀態(tài)管理數(shù)據(jù)狀態(tài)查詢(xún)與更改方式

    這篇文章主要介紹了vuex狀態(tài)管理數(shù)據(jù)狀態(tài)查詢(xún)與更改方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue在install時(shí)node-sass@4.14.1?postinstall:node?scripts/build.js錯(cuò)誤解決

    vue在install時(shí)node-sass@4.14.1?postinstall:node?scripts/buil

    最近在npm install 的時(shí)候遇到了個(gè)問(wèn)題,所以給大家總結(jié)下,下面這篇文章主要給大家介紹了關(guān)于vue在install時(shí)node-sass@4.14.1?postinstall:node?scripts/build.js錯(cuò)誤的解決方法,需要的朋友可以參考下
    2023-05-05
  • Element-UI介紹主題定制、自定義組件和插件擴(kuò)展的代碼示例

    Element-UI介紹主題定制、自定義組件和插件擴(kuò)展的代碼示例

    本文介紹了使用Element-UI實(shí)現(xiàn)主題定制、自定義組件和擴(kuò)展插件的方法和實(shí)用案例,在開(kāi)發(fā)過(guò)程中,我們可以根據(jù)自己的需求,靈活選擇相關(guān)的技術(shù)手段,并不斷探索和嘗試,以提高開(kāi)發(fā)效率和用戶(hù)體驗(yàn),感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • vueJs實(shí)現(xiàn)DOM加載完之后自動(dòng)下拉到底部的實(shí)例代碼

    vueJs實(shí)現(xiàn)DOM加載完之后自動(dòng)下拉到底部的實(shí)例代碼

    這篇文章主要介紹了vueJs實(shí)現(xiàn)DOM加載完成之后自動(dòng)下拉到底部的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-08-08

最新評(píng)論