当前位置:首页 > VUE

vue实现简单表格

2026-03-08 05:46:08VUE

Vue 实现简单表格的方法

基础表格实现

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

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

添加样式和交互

为表格添加基础样式和点击事件:

<template>
  <table class="vue-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"
        @click="selectRow(index)"
        :class="{ 'selected': selectedRow === 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, 职业: '设计师' }
      ],
      selectedRow: null
    }
  },
  methods: {
    selectRow(index) {
      this.selectedRow = index
    }
  }
}
</script>

<style>
.vue-table {
  width: 100%;
  border-collapse: collapse;
}
.vue-table th, .vue-table td {
  border: 1px solid #ddd;
  padding: 8px;
}
.vue-table tr:hover {
  background-color: #f5f5f5;
}
.vue-table .selected {
  background-color: #e0e0e0;
}
</style>

使用组件化方式

将表格封装为可复用组件:

<!-- TableComponent.vue -->
<template>
  <table class="vue-table">
    <thead>
      <tr>
        <th v-for="header in headers" :key="header">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <slot name="body" :rows="rows"></slot>
    </tbody>
  </table>
</template>

<script>
export default {
  props: {
    headers: {
      type: Array,
      required: true
    },
    rows: {
      type: Array,
      required: true
    }
  }
}
</script>

使用该组件:

<template>
  <TableComponent :headers="headers" :rows="rows">
    <template #body="{ rows }">
      <tr v-for="(row, index) in rows" :key="index">
        <td v-for="(value, key) in row" :key="key">{{ value }}</td>
      </tr>
    </template>
  </TableComponent>
</template>

<script>
import TableComponent from './TableComponent.vue'

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

添加排序功能

实现表格列的排序功能:

<template>
  <table class="vue-table">
    <thead>
      <tr>
        <th 
          v-for="header in headers" 
          :key="header.key"
          @click="sortBy(header.key)"
        >
          {{ header.label }}
          <span v-if="sortKey === header.key">
            {{ sortOrder > 0 ? '↑' : '↓' }}
          </span>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="row in sortedRows" :key="row.id">
        <td v-for="header in headers" :key="header.key">{{ row[header.key] }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      headers: [
        { key: 'name', label: '姓名' },
        { key: 'age', label: '年龄' },
        { key: 'job', label: '职业' }
      ],
      rows: [
        { id: 1, name: '张三', age: 25, job: '工程师' },
        { id: 2, name: '李四', age: 30, job: '设计师' }
      ],
      sortKey: '',
      sortOrder: 1
    }
  },
  computed: {
    sortedRows() {
      if (!this.sortKey) return this.rows
      return [...this.rows].sort((a, b) => {
        const aVal = a[this.sortKey]
        const bVal = b[this.sortKey]
        return (aVal > bVal ? 1 : -1) * this.sortOrder
      })
    }
  },
  methods: {
    sortBy(key) {
      if (this.sortKey === key) {
        this.sortOrder *= -1
      } else {
        this.sortKey = key
        this.sortOrder = 1
      }
    }
  }
}
</script>

使用第三方库

对于更复杂的需求,可以考虑使用专门的表格库如 Element UIVuetify

vue实现简单表格

<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="name" label="姓名"></el-table-column>
    <el-table-column prop="age" label="年龄"></el-table-column>
    <el-table-column prop="job" label="职业"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 25, job: '工程师' },
        { name: '李四', age: 30, job: '设计师' }
      ]
    }
  }
}
</script>

以上方法涵盖了从基础实现到高级功能的 Vue 表格开发方式,可根据项目需求选择合适的方案。

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

相关文章

css好看的表格制作

css好看的表格制作

CSS表格美化方法 边框与间距优化 使用border-collapse合并边框,避免双线边框。设置padding增加单元格内边距,提升可读性。 table { border-collapse:…

css好看的表格制作

css好看的表格制作

使用CSS制作美观表格的方法 边框与间距优化 通过border-collapse属性合并边框,避免双边框效果。设置padding增加单元格内边距,提升可读性。 table { border-…

简单实现vue github

简单实现vue github

实现 Vue 项目并托管到 GitHub 的步骤 创建 Vue 项目 使用 Vue CLI 快速初始化项目,确保已安装 Node.js 和 npm/yarn。 运行命令: npm init vu…

vue实现导出表格

vue实现导出表格

Vue 实现导出表格的方法 使用 xlsx 库导出 Excel 安装 xlsx 库 npm install xlsx 在 Vue 组件中引入 xlsx import * as XLSX from…

使用vue实现表格

使用vue实现表格

使用 Vue 实现表格 Vue.js 提供了灵活的方式来实现表格功能,可以通过组合 v-for 指令和动态数据绑定快速构建表格。以下是几种常见的实现方法: 基础表格实现 通过 v-for 遍历数组数…

vue动态表格实现

vue动态表格实现

Vue动态表格实现方法 使用v-for指令渲染动态数据 通过v-for指令可以循环遍历数组数据生成表格行,数据变化时表格自动更新。示例代码: <template> <table…