当前位置:首页 > VUE

vue表格实现教学

2026-02-09 12:29:48VUE

Vue 表格实现方法

使用 Element UI 的 el-table 组件

Element UI 提供了功能强大的表格组件 el-table,支持排序、筛选、分页等功能。安装 Element UI 后,可以直接在 Vue 项目中使用。

<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>

使用 Vuetify 的 v-data-table 组件

Vuetify 是另一个流行的 Vue UI 框架,其 v-data-table 组件提供了丰富的功能,包括排序、分页、行选择和搜索。

<template>
  <v-data-table :headers="headers" :items="desserts" :items-per-page="5" class="elevation-1"></v-data-table>
</template>

<script>
export default {
  data() {
    return {
      headers: [
        { text: 'Dessert (100g serving)', value: 'name' },
        { text: 'Calories', value: 'calories' },
        { text: 'Fat (g)', value: 'fat' }
      ],
      desserts: [
        { name: 'Frozen Yogurt', calories: 159, fat: 6.0 },
        { name: 'Ice cream sandwich', calories: 237, fat: 9.0 }
      ]
    }
  }
}
</script>

自定义表格组件

如果需要更灵活的表格功能,可以自定义表格组件。通过 Vue 的 v-for 指令动态渲染表格行和列。

<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="(cell, cellIndex) in row" :key="cellIndex">{{ cell }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      headers: ['姓名', '年龄', '城市'],
      rows: [
        ['张三', 25, '北京'],
        ['李四', 30, '上海']
      ]
    }
  }
}
</script>

添加交互功能

为表格添加排序、筛选或分页功能可以提升用户体验。以下是一个简单的排序功能实现示例。

<template>
  <table>
    <thead>
      <tr>
        <th v-for="header in headers" :key="header" @click="sortBy(header)">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, index) in sortedRows" :key="index">
        <td v-for="(cell, cellIndex) in row" :key="cellIndex">{{ cell }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      headers: ['姓名', '年龄', '城市'],
      rows: [
        ['张三', 25, '北京'],
        ['李四', 30, '上海']
      ],
      sortKey: '',
      sortOrder: 1
    }
  },
  computed: {
    sortedRows() {
      if (!this.sortKey) return this.rows
      const index = this.headers.indexOf(this.sortKey)
      return [...this.rows].sort((a, b) => {
        return a[index] > b[index] ? this.sortOrder : -this.sortOrder
      })
    }
  },
  methods: {
    sortBy(header) {
      if (this.sortKey === header) {
        this.sortOrder *= -1
      } else {
        this.sortKey = header
        this.sortOrder = 1
      }
    }
  }
}
</script>

集成第三方表格库

对于复杂的数据表格需求,可以考虑集成专门的表格库如 ag-Grid 或 Handsontable。这些库提供了高级功能如虚拟滚动、单元格编辑和 Excel 导出。

vue表格实现教学

<template>
  <ag-grid-vue
    style="width: 100%; height: 500px;"
    class="ag-theme-alpine"
    :columnDefs="columnDefs"
    :rowData="rowData"
  ></ag-grid-vue>
</template>

<script>
import { AgGridVue } from 'ag-grid-vue'

export default {
  components: { AgGridVue },
  data() {
    return {
      columnDefs: [
        { headerName: 'Make', field: 'make' },
        { headerName: 'Model', field: 'model' },
        { headerName: 'Price', field: 'price' }
      ],
      rowData: [
        { make: 'Toyota', model: 'Celica', price: 35000 },
        { make: 'Ford', model: 'Mondeo', price: 32000 }
      ]
    }
  }
}
</script>

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

相关文章

vue实现多人视频

vue实现多人视频

实现多人视频通话的Vue方案 使用WebRTC技术结合Vue框架可以构建多人视频通话应用。以下是核心实现方法: 技术栈选择 Vue 2/3作为前端框架 WebRTC用于实时通信 Socket.io…

vue实现批量

vue实现批量

Vue 实现批量操作的方法 在 Vue 中实现批量操作通常涉及选择多个项目并执行统一处理,例如批量删除、批量更新等。以下是几种常见实现方式: 表格多选批量操作 使用 el-table 配合复选框实现…

vue实现cs

vue实现cs

Vue 实现 CS(Client-Side)渲染的常见方法 Vue.js 是一个用于构建用户界面的渐进式框架,默认采用客户端渲染(CSR)。以下是几种常见的实现方式: 使用 Vue CLI 创建项目…

vue 实现回复

vue 实现回复

Vue 实现回复功能 在 Vue 中实现回复功能通常涉及表单提交、数据绑定和列表渲染。以下是实现步骤和代码示例: 数据绑定与表单 <template> <div>…

vue  实现tab

vue 实现tab

实现 Tab 切换功能 在 Vue 中实现 Tab 切换功能可以通过动态组件或条件渲染完成。以下是两种常见方法: 使用 v-if 或 v-show 实现条件渲染 通过绑定 currentTab 变量…

vue实现前端

vue实现前端

Vue 实现前端的核心方法与步骤 安装 Vue 项目 使用 Vue CLI 或 Vite 创建项目。Vue CLI 是传统构建工具,Vite 是新一代轻量级工具。 npm init vue@la…