当前位置:首页 > VUE

vue实现表格样式

2026-02-20 12:24:04VUE

Vue 实现表格样式的方法

使用原生 HTML 表格

通过 Vue 的模板语法直接渲染 HTML 表格,结合 CSS 控制样式。

<template>
  <table class="custom-table">
    <thead>
      <tr>
        <th v-for="header in headers" :key="header">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="row in rows" :key="row.id">
        <td v-for="cell in row.cells" :key="cell">{{ cell }}</td>
      </tr>
    </tbody>
  </table>
</template>

<style scoped>
.custom-table {
  width: 100%;
  border-collapse: collapse;
}
.custom-table th, .custom-table td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}
.custom-table th {
  background-color: #f2f2f2;
}
.custom-table tr:nth-child(even) {
  background-color: #f9f9f9;
}
.custom-table tr:hover {
  background-color: #e9e9e9;
}
</style>

使用 Element UI 表格组件

Element UI 提供了丰富的表格组件和样式配置。

<template>
  <el-table :data="tableData" style="width: 100%" stripe border>
    <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: '2023-01-01', name: '张三', address: '北京市' },
        { date: '2023-01-02', name: '李四', address: '上海市' }
      ]
    }
  }
}
</script>

使用 Vuetify 表格组件

Vuetify 提供了 Material Design 风格的表格组件。

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

<script>
export default {
  data() {
    return {
      headers: [
        { text: '姓名', value: 'name' },
        { text: '年龄', value: 'age' }
      ],
      items: [
        { name: '张三', age: 25 },
        { name: '李四', age: 30 }
      ]
    }
  }
}
</script>

动态样式绑定

通过 Vue 的动态样式绑定实现条件样式。

<template>
  <table>
    <tr v-for="row in rows" :key="row.id" :class="{ 'highlight-row': row.highlight }">
      <td>{{ row.content }}</td>
    </tr>
  </table>
</template>

<style>
.highlight-row {
  background-color: yellow;
}
</style>

使用 CSS 框架

结合 Bootstrap 等 CSS 框架快速实现表格样式。

vue实现表格样式

<template>
  <table class="table table-striped table-hover">
    <tr v-for="row in rows" :key="row.id">
      <td>{{ row.content }}</td>
    </tr>
  </table>
</template>

这些方法可以根据项目需求灵活选择,原生表格适合简单需求,UI 框架适合快速开发,动态样式绑定适合复杂交互场景。

标签: 样式表格
分享给朋友:

相关文章

vue实现表格数据修改

vue实现表格数据修改

实现表格数据修改的基本思路 在Vue中实现表格数据修改通常涉及以下核心步骤:数据绑定、编辑状态切换、表单输入处理和数据提交。以下是一个典型实现方案: 数据绑定与渲染 使用v-for指令循环渲染表格数…

vue实现表格报表

vue实现表格报表

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

vue实现表格计算

vue实现表格计算

Vue 表格计算实现方法 使用计算属性 在 Vue 中可以通过计算属性来实现表格数据的动态计算。计算属性会自动缓存结果,只有当依赖的数据发生变化时才会重新计算。 computed: { tota…

vue实现搜索表格

vue实现搜索表格

Vue 实现搜索表格功能 数据绑定与表格渲染 在 Vue 中通过 v-model 绑定搜索输入框,实时监听用户输入。表格数据使用 v-for 动态渲染,初始数据可从 API 或本地获取。 <t…

vue 实现表格分页

vue 实现表格分页

Vue 实现表格分页的方法 使用 Element UI 的分页组件 Element UI 提供了现成的分页组件 el-pagination,结合 el-table 可以快速实现表格分页功能。 安装…

vue表格实现日历

vue表格实现日历

Vue 表格实现日历的方法 基础表格结构 使用 el-table 或原生 HTML 表格,通过 v-for 循环生成日历格子。月份天数通过 new Date() 计算,动态渲染表格内容。 &…