Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

vue表格组件

作者:死亡之神   发布日期:2025-03-08   浏览:118

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th v-for="(header, index) in headers" :key="index">{{ header }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(row, rowIndex) in tableData" :key="rowIndex">
          <td v-for="(cell, cellIndex) in row" :key="cellIndex">{{ cell }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      headers: ['姓名', '年龄', '职业'],
      tableData: [
        ['张三', 25, '工程师'],
        ['李四', 30, '设计师'],
        ['王五', 22, '学生']
      ]
    };
  }
};
</script>

<style scoped>
table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  border: 1px solid #ddd;
  padding: 8px;
}

th {
  background-color: #f2f2f2;
  text-align: left;
}
</style>

解释说明:

  1. 模板部分 (<template>):

    • 使用了标准的 HTML <table> 元素来创建表格。
    • 表头 (<thead>) 使用 v-for 指令遍历 headers 数组,动态生成表头内容。
    • 表体 (<tbody>) 使用嵌套的 v-for 指令遍历 tableData 数组,动态生成每一行和每个单元格的内容。
  2. 脚本部分 (<script>):

    • 定义了一个 Vue 组件,包含 data 函数返回的对象,其中 headers 是表头数据,tableData 是表格的数据源。
  3. 样式部分 (<style scoped>):

    • 使用了一些基本的 CSS 样式来美化表格,如设置边框、内边距和表头背景颜色等。
    • scoped 属性确保这些样式仅应用于当前组件。

上一篇:vue3富文本

下一篇:vue unshift

大家都在看

vue.config.js configu

node.js vue

vue查看版本

vue等待几秒

vue3 setup computed

vue screenfull

vue json.stringify

vue 遍历list

typescript vue

vue 复选框

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站