Building a Reusable Vue Table Component with Pagination and Loading

This article explains how to create a reusable Vue component called PageTable that encapsulates Element UI table and pagination with built-in loading handling, data fetching via a parent-provided fetchData method, and slot-based column customization.

HomeTech
HomeTech
HomeTech
Building a Reusable Vue Table Component with Pagination and Loading

The article introduces a reusable Vue component PageTable that encapsulates Element UI's el-table and el-pagination.

The component receives a fetchData function prop from the parent, which returns a promise with data and total count.

The template binds table data to tableData and uses slots for custom columns:

<template><br/> <div><br/>  <el-table :data="tableData"><br/>   <slot /><br/>  </el-table><br/>  <el-pagination<br/>   :layout="total, sizes, prev, pager, next, jumper"<br/>   ::current-page.sync="page.current"<br/>   ::page-size.sync="page.size"<br/>   ::total="page.total"<br/>   :@size-change="search"<br/>   :@current-change="getTableData"<br/>  /><br/> </div><br/></template>

The script defines props, data, and methods: getTableData calls fetchData, updates loading state via $emit('update:loading'), and handles errors.

async getTableData() {<br/>    try {<br/>        const { current, size } = this.page;<br/>        this.$emit('update:loading', true);<br/>        const { data, total } = await this.fetchData(current, size);<br/>        this.tableData = data;<br/>        this.page.total = total;<br/>    } catch (err) {<br/>        this.tableData = [];<br/>        this.page.total = 0;<br/>    } finally {<br/>        this.$emit('update:loading', false);<br/>    }<br/>}

Additional features include exposing the table's ref via getTableRef and passing through attributes and listeners with v-bind="$attrs" and v-on="$listeners".

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

frontendComponentVuepaginationloadingElement UI
HomeTech
Written by

HomeTech

HomeTech tech sharing

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.