vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > Vue Hook 表格

Vue Hook 封装通用型表格的详细过程

作者:最小生成树

这篇文章主要介绍了Vue Hook 封装通用型表格,设计通用型表格组件首先,需要设计一个基础的表格组件,它接受列配置、数据和分页信息等参数,本文给大家介绍的非常详细,需要的朋友可以参考下

一、创建通用型表格的需求

实现一个通用型表格组件,具备以下功能:

二、设计通用型表格组件

首先,需要设计一个基础的表格组件,它接受列配置、数据和分页信息等参数。

1. 创建 useTable Hook

在 src/hooks 目录下创建 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. 创建 TableComponent.vue

在 src/components 目录下创建 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. 创建 ExampleTable.vue 组件

在 src/views 目录下创建 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) => {
      // 模拟数据获取
      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>

四、解释代码

到此这篇关于Vue Hook 封装通用型表格的文章就介绍到这了,更多相关Vue Hook 封装通用型表格内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文