当前位置:首页 > VUE

vue实现简单表格

2026-01-08 15:50:30VUE

Vue 实现简单表格的方法

使用原生 HTML 表格

在 Vue 模板中可以直接使用 HTML 原生表格标签,通过 v-for 动态渲染数据:

<template>
  <table>
    <thead>
      <tr>
        <th v-for="header in headers" :key="header">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="item in items" :key="item.id">
        <td v-for="(value, key) in item" :key="key">{{ value }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      headers: ['ID', 'Name', 'Age'],
      items: [
        { id: 1, name: 'Alice', age: 25 },
        { id: 2, name: 'Bob', age: 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: '2023-01-01', name: '张三', address: '北京市' },
        { date: '2023-01-02', name: '李四', address: '上海市' }
      ]
    }
  }
}
</script>

添加交互功能

为表格添加排序、筛选等交互功能:

<template>
  <el-table
    :data="tableData"
    style="width: 100%"
    @sort-change="handleSortChange"
    @filter-change="handleFilterChange">
    <el-table-column
      prop="date"
      label="日期"
      sortable
      width="180">
    </el-table-column>
    <el-table-column
      prop="name"
      label="姓名"
      :filters="nameFilters"
      filter-placement="bottom-end"
      width="180">
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  methods: {
    handleSortChange(column) {
      console.log(column.prop, column.order)
    },
    handleFilterChange(filters) {
      console.log(filters)
    }
  }
}
</script>

响应式表格设计

确保表格在不同屏幕尺寸下都能良好显示:

@media screen and (max-width: 600px) {
  table {
    width: 100%;
  }
  thead {
    display: none;
  }
  tr {
    display: block;
    margin-bottom: 1rem;
    border: 1px solid #ddd;
  }
  td {
    display: block;
    text-align: right;
    padding-left: 50%;
    position: relative;
  }
  td::before {
    content: attr(data-label);
    position: absolute;
    left: 0;
    width: 50%;
    padding-left: 15px;
    font-weight: bold;
    text-align: left;
  }
}

虚拟滚动优化

对于大数据量表格,可以使用虚拟滚动提升性能:

vue实现简单表格

<template>
  <recycle-scroller
    class="table"
    :items="largeData"
    :item-size="50"
    key-field="id">
    <template v-slot="{ item }">
      <div class="table-row">
        <div class="table-cell">{{ item.id }}</div>
        <div class="table-cell">{{ item.name }}</div>
      </div>
    </template>
  </recycle-scroller>
</template>

标签: 表格简单
分享给朋友:

相关文章

vue实现表格输入

vue实现表格输入

Vue实现表格输入的方法 基础表格实现 使用v-for指令动态渲染表格行和列,结合v-model实现双向数据绑定。 <template> <table> <…

vue实现多级表格

vue实现多级表格

Vue 实现多级表格的方法 使用递归组件实现多级表格 递归组件是处理嵌套数据的理想方式,特别适合展示树形结构的数据。 <template> <table> <…

vue实现表格滚动

vue实现表格滚动

Vue 实现表格滚动的方法 使用 CSS 固定表头并滚动内容 通过 CSS 设置表格容器的固定高度和 overflow-y: auto,实现内容区域的滚动。表头部分使用 position: stick…

vue简单功能实现

vue简单功能实现

Vue 简单功能实现示例 以下是一个基于 Vue 3 的简单计数器功能实现,包含模板、脚本和样式部分: <template> <div class="counter">…

vue 实现简单表格

vue 实现简单表格

以下是在 Vue 中实现简单表格的几种方法: 使用原生表格标签 通过 Vue 的数据绑定功能渲染表格数据: <template> <table> <the…

vue 实现简单分页

vue 实现简单分页

Vue 实现简单分页的方法 基础分页组件实现 创建一个基础分页组件,包含页码按钮和翻页功能。以下是一个基于 Vue 3 的示例: <template> <div class="…