当前位置:首页 > VUE

vue表格怎么实现

2026-03-09 18:33:34VUE

Vue 表格实现方法

在 Vue 中实现表格功能可以通过多种方式完成,以下是几种常见的方法:

使用原生 HTML 表格

最简单的表格可以通过 Vue 的数据绑定功能结合原生 HTML 表格元素实现:

<template>
  <table>
    <thead>
      <tr>
        <th v-for="header in headers" :key="header">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, index) in rows" :key="index">
        <td v-for="(value, key) in row" :key="key">{{ value }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      headers: ['姓名', '年龄', '职业'],
      rows: [
        { 姓名: '张三', 年龄: 25, 职业: '工程师' },
        { 姓名: '李四', 年龄: 30, 职业: '设计师' }
      ]
    }
  }
}
</script>

使用 Element UI 表格组件

Element UI 提供了功能丰富的表格组件:

<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="date" label="日期" width="180"></el-table-column>
    <el-table-column prop="name" label="姓名" width="180"></el-table-column>
    <el-table-column prop="address" label="地址"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        }
      ]
    }
  }
}
</script>

使用 Ant Design Vue 表格

Ant Design Vue 也提供了强大的表格组件:

<template>
  <a-table :columns="columns" :data-source="data" />
</template>

<script>
const columns = [
  {
    title: '姓名',
    dataIndex: 'name',
    key: 'name'
  },
  {
    title: '年龄',
    dataIndex: 'age',
    key: 'age'
  }
]

const data = [
  {
    key: '1',
    name: '胡彦斌',
    age: 32
  },
  {
    key: '2',
    name: '胡彦祖',
    age: 42
  }
]

export default {
  data() {
    return {
      data,
      columns
    }
  }
}
</script>

实现表格高级功能

对于需要分页、排序、筛选等高级功能的表格:

<template>
  <el-table
    :data="tableData"
    style="width: 100%"
    @sort-change="handleSortChange"
    :default-sort="{prop: 'date', order: 'descending'}"
  >
    <el-table-column prop="date" label="日期" sortable width="180"></el-table-column>
    <el-table-column prop="name" label="姓名" width="180"></el-table-column>
    <el-table-column prop="address" label="地址"></el-table-column>
  </el-table>
  <el-pagination
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-sizes="[10, 20, 30, 40]"
    :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
  ></el-pagination>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  methods: {
    handleSortChange({ column, prop, order }) {
      // 排序逻辑
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    fetchData() {
      // 获取数据逻辑
    }
  }
}
</script>

自定义表格组件

如果需要更灵活的控制,可以创建自定义表格组件:

vue表格怎么实现

<!-- CustomTable.vue -->
<template>
  <div class="custom-table">
    <div class="table-header">
      <div class="header-row">
        <div
          v-for="column in columns"
          :key="column.key"
          class="header-cell"
          :style="{ width: column.width }"
        >
          {{ column.title }}
        </div>
      </div>
    </div>
    <div class="table-body">
      <div
        v-for="(row, rowIndex) in data"
        :key="rowIndex"
        class="body-row"
      >
        <div
          v-for="column in columns"
          :key="column.key"
          class="body-cell"
          :style="{ width: column.width }"
        >
          {{ row[column.dataIndex] }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    columns: {
      type: Array,
      required: true
    },
    data: {
      type: Array,
      required: true
    }
  }
}
</script>

以上方法涵盖了从简单到复杂的 Vue 表格实现方式,可以根据项目需求选择合适的方案。

标签: 表格vue
分享给朋友:

相关文章

vue 实现过滤

vue 实现过滤

Vue 实现过滤的方法 在 Vue 中实现过滤功能可以通过多种方式完成,包括使用计算属性、自定义过滤器、第三方库等。以下是几种常见的实现方法: 使用计算属性进行过滤 计算属性是 Vue 中最常用的过…

vue拖拽实现

vue拖拽实现

Vue 拖拽实现方法 使用 HTML5 原生拖拽 API HTML5 提供了原生拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 drop 事…

vue实现checkbox

vue实现checkbox

Vue 实现 Checkbox 在 Vue 中实现 Checkbox 可以通过原生 HTML 的 <input type="checkbox"> 或使用 Vue 的 v-model 指令绑…

vue实现评价

vue实现评价

Vue实现评价功能 数据绑定与双向绑定 Vue的核心特性是数据绑定和双向绑定,通过v-model指令可以轻松实现表单输入与应用状态之间的双向绑定。在评价功能中,可以使用v-model绑定评论文本和评分…

vue如何实现登录

vue如何实现登录

实现登录功能的基本步骤 使用Vue实现登录功能通常需要结合后端API、状态管理以及路由控制。以下是常见的实现方式: 创建登录表单组件 在Vue组件中构建包含用户名和密码输入框的表单: <te…

vue实现图片循环

vue实现图片循环

实现图片循环的基本方法 在Vue中实现图片循环通常使用v-for指令结合数组数据。以下是基础实现方式: <template> <div> <img v-fo…