当前位置:首页 > VUE

vue实现表格内容

2026-01-16 02:09:01VUE

Vue实现表格内容的方法

使用原生HTML表格

在Vue模板中直接使用HTML的<table>标签构建表格结构,通过v-for指令动态渲染数据。

<template>
  <table>
    <thead>
      <tr>
        <th v-for="(header, index) in headers" :key="index">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, rowIndex) in rows" :key="rowIndex">
        <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>

使用第三方组件库

通过集成Element UI或Ant Design Vue等UI库快速实现功能丰富的表格。

Element UI示例:

<template>
  <el-table :data="tableData">
    <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>

动态表格渲染

结合计算属性实现动态数据绑定和条件渲染。

<template>
  <table>
    <tr v-for="(item, index) in processedData" :key="index">
      <td>{{ item.name }}</td>
      <td :class="{ 'highlight': item.age > 28 }">{{ item.age }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  computed: {
    processedData() {
      return this.rawData.map(item => ({
        ...item,
        status: item.age > 30 ? '资深' : '普通'
      }))
    }
  },
  data() {
    return {
      rawData: [
        { name: '王五', age: 35 },
        { name: '赵六', age: 27 }
      ]
    }
  }
}
</script>

表格交互功能

实现排序、筛选等交互功能。

<template>
  <table>
    <thead>
      <tr>
        <th @click="sortBy('name')">姓名</th>
        <th @click="sortBy('age')">年龄</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="item in sortedData" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      sortKey: '',
      sortOrder: 1,
      items: [
        { id: 1, name: '张三', age: 25 },
        { id: 2, name: '李四', age: 30 }
      ]
    }
  },
  computed: {
    sortedData() {
      return [...this.items].sort((a, b) => {
        return a[this.sortKey] > b[this.sortKey] ? this.sortOrder : -this.sortOrder
      })
    }
  },
  methods: {
    sortBy(key) {
      this.sortOrder = this.sortKey === key ? -this.sortOrder : 1
      this.sortKey = key
    }
  }
}
</script>

虚拟滚动优化

对于大数据量表格,使用vue-virtual-scroller等插件优化性能。

vue实现表格内容

<template>
  <RecycleScroller
    class="scroller"
    :items="bigData"
    :item-size="50"
    key-field="id"
    v-slot="{ item }"
  >
    <div class="row">
      <span>{{ item.id }}</span>
      <span>{{ item.name }}</span>
    </div>
  </RecycleScroller>
</template>

<script>
import { RecycleScroller } from 'vue-virtual-scroller'
export default {
  components: { RecycleScroller },
  data() {
    return {
      bigData: Array.from({ length: 1000 }, (_, i) => ({
        id: i,
        name: `项目 ${i}`
      }))
    }
  }
}
</script>

标签: 表格内容
分享给朋友:

相关文章

国外vue实现表格控件

国外vue实现表格控件

以下是国外流行的 Vue 表格控件实现方案及特点,供参考: vue-good-table 基于 Vue 2/3 的轻量级表格组件,支持排序、分页、筛选等功能。 安装方式: npm instal…

vue表格实现单选

vue表格实现单选

实现 Vue 表格单选功能 使用 v-model 绑定选中状态 在表格的每一行添加单选按钮或点击行触发选中,通过 v-model 绑定一个变量存储当前选中行的唯一标识(如 id)。 示例代码:…

用css制作表格

用css制作表格

使用CSS制作表格 基础表格结构 HTML中表格由<table>、<tr>(行)、<th>(表头)和<td>(单元格)组成。基础结构如下: <t…

vue实现表格筛选

vue实现表格筛选

实现表格筛选的基本思路 在Vue中实现表格筛选通常涉及以下几个核心步骤:数据绑定、筛选逻辑处理、动态渲染。以下是具体实现方法。 基础数据绑定与表格渲染 定义表格数据和表头结构,使用v-for循环渲染…

vue实现sku表格

vue实现sku表格

Vue实现SKU表格的方法 SKU(Stock Keeping Unit)表格常用于电商平台展示商品的多属性组合,例如颜色、尺寸等。以下是基于Vue的实现方法: 数据结构设计 SKU的核心是属性组合…

vue实现表格报表

vue实现表格报表

Vue 实现表格报表的方法 使用 Element UI 的表格组件 Element UI 提供了功能丰富的表格组件,适合快速实现报表功能。安装 Element UI 后,可以直接使用 el-table…