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

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

相关文章

vue实现简单表格

vue实现简单表格

Vue 实现简单表格的方法 使用原生 HTML 表格 在 Vue 模板中可以直接使用 HTML 原生表格标签,通过 v-for 动态渲染数据: <template> <tab…

css制作三行三列表格

css制作三行三列表格

使用HTML和CSS创建三行三列表格 通过HTML的<table>标签结合CSS样式可以轻松实现三行三列的表格结构。 <table class="three-by-three"&g…

vue实现表格读取数据

vue实现表格读取数据

Vue 实现表格数据读取 在 Vue 中实现表格数据读取通常涉及以下关键步骤: 数据绑定与表格渲染 通过 v-for 指令动态渲染表格行,数据通常存储在组件的 data 或 Vuex 状态中:…

vue表格实现复选框

vue表格实现复选框

Vue表格实现复选框的方法 基础实现(使用v-model绑定数组) 在Vue中可以通过v-model绑定数组来实现复选框功能。以下是一个基础示例: <template> <ta…

jquery 样式

jquery 样式

jQuery 样式操作 jQuery 提供了多种方法来操作 HTML 元素的样式,包括直接修改 CSS 属性、添加或移除类、获取样式值等。 修改 CSS 属性 使用 css() 方法可以直接修改元…

vue element实现表格

vue element实现表格

Vue + Element UI 实现表格 Element UI 提供了强大的表格组件 el-table,结合 Vue 可以快速实现数据展示、排序、筛选等功能。 基础表格实现 安装 Element…