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

vue 表格组件

作者:萫蕉姺森う   发布日期:2025-06-19   浏览:85

<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: [
        ['张三', 20, '男'],
        ['李四', 22, '女'],
        ['王五', 25, '男']
      ]
    };
  }
};
</script>

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

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

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

解释说明:

  • 模板部分 (<template>):

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

    • 定义了 data 函数,返回一个包含 headerstableData 的对象。
    • headers 是一个数组,表示表格的列标题。
    • tableData 是一个二维数组,每一项代表表格中的一行数据。
  • 样式部分 (<style scoped>):

    • 给表格设置了宽度为 100%,并且合并边框。
    • 设置了表头和单元格的边框、内边距等样式,使表格看起来更美观。

上一篇:vue2 inject

下一篇:vue router.push

大家都在看

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 中文站