当前位置:首页 > 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库快速实现功能丰富的表格。

vue实现表格内容

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>

动态表格渲染

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

vue实现表格内容

<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等插件优化性能。

<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 表格实现方法 基础表格实现 使用 el-table 组件(Element UI)实现基础表格: <template> <el-table :data="tableDat…

vue实现表格筛选

vue实现表格筛选

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

vue实现多级表格

vue实现多级表格

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

vue 实现tab表格

vue 实现tab表格

实现 Tab 表格的基本思路 在 Vue 中实现 Tab 表格功能,通常需要结合 v-for 动态渲染表格内容,并通过 v-if 或 v-show 控制不同 Tab 的显示切换。核心逻辑是通过数据驱动…

vue 实现页面 表格

vue 实现页面 表格

Vue 实现页面表格的方法 Vue 提供了多种方式实现表格功能,可以根据需求选择不同的方案。以下是常见的实现方法: 使用原生 HTML 表格 通过 Vue 的模板语法直接渲染表格数据: <t…

vue easyui表格实现

vue easyui表格实现

Vue 与 EasyUI 表格集成方法 Vue 本身不直接支持 EasyUI,但可通过第三方库或手动集成实现。以下是两种主流方案: 方案一:使用 vue-easyui 封装库 安装官方维护的 Vue…