当前位置:首页 > 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 提供了丰富的表格组件和样式配置。

vue实现表格样式

<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 风格的表格组件。

vue实现表格样式

<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 框架快速实现表格样式。

<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 框架适合快速开发,动态样式绑定适合复杂交互场景。

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

相关文章

表格tb制作css

表格tb制作css

使用CSS美化HTML表格 通过CSS可以自定义表格样式,使其更美观和符合设计需求。以下是一些常用方法: 基础表格样式 table { width: 100%; border-collap…

uniapp样式库

uniapp样式库

uniapp 常用样式库推荐 uView UI uView 是专为 uniapp 设计的多端兼容 UI 框架,提供丰富的组件和工具类。支持 Vue3 组合式 API,内置主题定制功能,常用于快速构建企…

vue 实现表格单选

vue 实现表格单选

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

vue 实现动态样式

vue 实现动态样式

动态样式绑定方法 在Vue中实现动态样式主要通过v-bind:class和v-bind:style指令完成,以下是具体实现方式: 类名绑定 <div :class="{ active: is…

vue实现表格导入

vue实现表格导入

Vue 实现表格导入的方法 使用 Element UI 的 Upload 组件 Element UI 提供了 el-upload 组件,可以方便地实现文件上传功能。结合 xlsx 库解析 Excel…

vue实现表格选择

vue实现表格选择

Vue 实现表格选择功能 在 Vue 中实现表格选择功能可以通过多种方式完成,常见的方法包括使用复选框、单选框或通过点击行高亮选中。以下是几种常见的实现方式。 使用复选框实现多选 在表格的每一行添加…