当前位置:首页 > 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>

添加样式和交互

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

vue实现简单表格

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

使用该组件:

vue实现简单表格

<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

<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 表格开发方式,可根据项目需求选择合适的方案。

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

相关文章

vue实现简单的弹窗

vue实现简单的弹窗

Vue实现简单弹窗的方法 使用v-if和v-show控制显示 通过Vue的指令v-if或v-show可以轻松控制弹窗的显示与隐藏。v-if会完全销毁和重建DOM元素,而v-show仅通过CSS的di…

vue 实现简单登陆

vue 实现简单登陆

Vue 实现简单登录功能 创建 Vue 项目 使用 Vue CLI 创建一个新项目: vue create login-demo 选择默认配置或手动配置(推荐包含 Vue Router 和 Vuex…

vue 实现表格单选

vue 实现表格单选

实现表格单选的基本思路 在Vue中实现表格单选功能,通常需要结合表格组件(如el-table)和数据绑定。核心逻辑是通过点击事件更新当前选中行的唯一标识(如id),并通过:class或:style绑定…

uniapp 表格导入

uniapp 表格导入

uniapp 表格导入的实现方法 使用 uni.chooseFile 选择文件 在 uniapp 中,可以通过 uni.chooseFile API 让用户选择本地文件。该 API 支持多平台,包括…

vue实现简单搜索

vue实现简单搜索

Vue 实现简单搜索功能 数据绑定与输入框 在 Vue 中实现搜索功能,通常使用 v-model 绑定输入框的值到数据属性。创建一个输入框用于用户输入搜索关键词: <template>…

表格制作css

表格制作css

CSS表格样式设计 基础表格样式 通过border-collapse合并边框,使表格更整洁。width控制整体宽度,text-align设置文字对齐方式。 table { border-coll…