表格 Table
基础用法
1 | 李小颖 | 522 | 4 | |
15 | 杜小浩 | 462 | 18 | |
18 | 汤小青 | 482 | 12 | |
28 | 田小锋 | 475 | 15 | |
35 | 吴小豪 | 372 | 41 | |
41 | 吴小可 | 404 | 33 | |
46 | 木小亦 | 409 | 40 | |
43 | 邹小辉 | 293 | 53 |
vue
<template>
<bp-table
:data="list"
border
row-key="seat"
:row-selection="rowSelection"
v-model:selectedKeys="selectedKeys"
v-model:selectedKey="selectedKey"
>
<template #columns>
<bp-table-column title="座号" data-index="seat" :width="100">
<template #cell="{ record }">
<span style="font-weight: bold">{{ record.seat }}</span>
</template>
</bp-table-column>
<bp-table-column title="姓名" data-index="name" align="center" />
<bp-table-column title="成绩" data-index="results" :width="100" />
<bp-table-column title="班级排名" data-index="ranking" :width="100" />
</template>
</bp-table>
</template>
<script setup lang="ts">
import { ref } from "vue";
const list = [
{ seat: "1", name: "李小颖", results: "522", ranking: "4" },
{ seat: "15", name: "杜小浩", results: "462", ranking: "18" },
{ seat: "18", name: "汤小青", results: "482", ranking: "12" },
{ seat: "28", name: "田小锋", results: "475", ranking: "15" },
{ seat: "35", name: "吴小豪", results: "372", ranking: "41" },
{ seat: "41", name: "吴小可", results: "404", ranking: "33" },
{ seat: "46", name: "木小亦", results: "409", ranking: "40" },
{ seat: "43", name: "邹小辉", results: "293", ranking: "53" },
];
const selectedKey = ref("");
const selectedKeys = ref([]);
const rowSelection = {
type: "radio",
// type: "checkbox",
};
</script>
表头滚动同步测试
测试表头和内容滚动同步,以及列对齐问题
固定列宽 + 横向滚动(容器宽度600px,表格内容宽度约1000px)
1 | 张三 | 28 | 北京 | 前端开发工程师 | zhangsan@example.com | 在职 | |
2 | 李四 | 32 | 上海 | 后端开发工程师 | lisi@example.com | 在职 | |
3 | 王五 | 26 | 深圳 | UI设计师 | wangwu@example.com | 离职 | |
4 | 赵六 | 30 | 广州 | 产品经理 | zhaoliu@example.com | 在职 |
混合宽度(部分固定宽度 + 自适应)
1 | 张三 | 28 | 北京 | 前端开发工程师 | zhangsan@example.com | 在职 |
2 | 李四 | 32 | 上海 | 后端开发工程师 | lisi@example.com | 在职 |
3 | 王五 | 26 | 深圳 | UI设计师 | wangwu@example.com | 离职 |
4 | 赵六 | 30 | 广州 | 产品经理 | zhaoliu@example.com | 在职 |
纵向 + 横向滚动
1 | 张三 | 28 | 北京 | 前端开发工程师 | zhangsan@example.com | 13800138001 | 在职 | |
2 | 李四 | 32 | 上海 | 后端开发工程师 | lisi@example.com | 13800138002 | 在职 | |
3 | 王五 | 26 | 深圳 | UI设计师 | wangwu@example.com | 13800138003 | 离职 | |
4 | 赵六 | 30 | 广州 | 产品经理 | zhaoliu@example.com | 13800138004 | 在职 | |
5 | 用户5 | 25 | 北京 | 开发工程师 | user5@example.com | 138001380005 | 离职 | |
6 | 用户6 | 26 | 上海 | 设计师 | user6@example.com | 138001380006 | 在职 | |
7 | 用户7 | 27 | 深圳 | 产品经理 | user7@example.com | 138001380007 | 在职 | |
8 | 用户8 | 28 | 广州 | 测试工程师 | user8@example.com | 138001380008 | 离职 | |
9 | 用户9 | 29 | 杭州 | 运营 | user9@example.com | 138001380009 | 在职 | |
10 | 用户10 | 30 | 北京 | 开发工程师 | user10@example.com | 138001380010 | 在职 | |
11 | 用户11 | 31 | 上海 | 设计师 | user11@example.com | 138001380011 | 离职 | |
12 | 用户12 | 32 | 深圳 | 产品经理 | user12@example.com | 138001380012 | 在职 | |
13 | 用户13 | 33 | 广州 | 测试工程师 | user13@example.com | 138001380013 | 在职 | |
14 | 用户14 | 34 | 杭州 | 运营 | user14@example.com | 138001380014 | 离职 | |
15 | 用户15 | 25 | 北京 | 开发工程师 | user15@example.com | 138001380015 | 在职 | |
16 | 用户16 | 26 | 上海 | 设计师 | user16@example.com | 138001380016 | 在职 | |
17 | 用户17 | 27 | 深圳 | 产品经理 | user17@example.com | 138001380017 | 离职 | |
18 | 用户18 | 28 | 广州 | 测试工程师 | user18@example.com | 138001380018 | 在职 | |
19 | 用户19 | 29 | 杭州 | 运营 | user19@example.com | 138001380019 | 在职 | |
20 | 用户20 | 30 | 北京 | 开发工程师 | user20@example.com | 138001380020 | 离职 | |
21 | 用户21 | 31 | 上海 | 设计师 | user21@example.com | 138001380021 | 在职 | |
22 | 用户22 | 32 | 深圳 | 产品经理 | user22@example.com | 138001380022 | 在职 | |
23 | 用户23 | 33 | 广州 | 测试工程师 | user23@example.com | 138001380023 | 离职 | |
24 | 用户24 | 34 | 杭州 | 运营 | user24@example.com | 138001380024 | 在职 |
vue
<template>
<div>
<h3>表头滚动同步测试</h3>
<p>测试表头和内容滚动同步,以及列对齐问题</p>
<!-- 测试固定列宽 + 横向滚动 -->
<h4>固定列宽 + 横向滚动(容器宽度600px,表格内容宽度约1000px)</h4>
<div style="width: 600px; ">
<bp-table
:data="list"
border
row-key="id"
:scroll="{ x: 1000 }"
:row-selection="{ type: 'radio' }"
>
<template #columns>
<bp-table-column title="ID" data-index="id" :width="80" />
<bp-table-column title="姓名" data-index="name" :width="120" />
<bp-table-column title="年龄" data-index="age" :width="80" />
<bp-table-column title="城市" data-index="city" :width="120" />
<bp-table-column title="职业" data-index="job" :width="200" />
<bp-table-column title="邮箱" data-index="email" :width="250" />
<bp-table-column title="状态" data-index="status" :width="100" />
</template>
</bp-table>
</div>
<!-- 测试混合宽度 -->
<h4>混合宽度(部分固定宽度 + 自适应)</h4>
<div style="width: 500px; ">
<bp-table
:data="list"
border
row-key="id"
:scroll="{ x: 800 }"
>
<template #columns>
<bp-table-column title="ID" data-index="id" :width="60" />
<bp-table-column title="姓名" data-index="name" :width="100" />
<bp-table-column title="年龄" data-index="age" />
<bp-table-column title="城市" data-index="city" />
<bp-table-column title="职业" data-index="job" :width="180" />
<bp-table-column title="邮箱" data-index="email" :width="220" />
<bp-table-column title="状态" data-index="status" />
</template>
</bp-table>
</div>
<!-- 测试纵向 + 横向滚动 -->
<h4>纵向 + 横向滚动</h4>
<div style="width: 700px; ">
<bp-table
:data="bigList"
border
row-key="id"
:scroll="{ x: 900, y: 200 }"
:row-selection="{ type: 'checkbox' }"
>
<template #columns>
<bp-table-column title="ID" data-index="id" :width="60" />
<bp-table-column title="姓名" data-index="name" :width="100" />
<bp-table-column title="年龄" data-index="age" :width="80" />
<bp-table-column title="城市" data-index="city" :width="120" />
<bp-table-column title="职业" data-index="job" :width="180" />
<bp-table-column title="邮箱" data-index="email" :width="200" />
<bp-table-column title="电话" data-index="phone" :width="120" />
<bp-table-column title="状态" data-index="status" :width="80" />
</template>
</bp-table>
</div>
</div>
</template>
<script setup lang="ts">
import { ref } from "vue";
const list = ref([
{
id: 1,
name: "张三",
age: 28,
city: "北京",
job: "前端开发工程师",
email: "zhangsan@example.com",
phone: "13800138001",
status: "在职"
},
{
id: 2,
name: "李四",
age: 32,
city: "上海",
job: "后端开发工程师",
email: "lisi@example.com",
phone: "13800138002",
status: "在职"
},
{
id: 3,
name: "王五",
age: 26,
city: "深圳",
job: "UI设计师",
email: "wangwu@example.com",
phone: "13800138003",
status: "离职"
},
{
id: 4,
name: "赵六",
age: 30,
city: "广州",
job: "产品经理",
email: "zhaoliu@example.com",
phone: "13800138004",
status: "在职"
}
]);
// 生成更多数据用于测试纵向滚动
const bigList = ref([
...list.value,
...Array.from({ length: 20 }, (_, i) => ({
id: i + 5,
name: `用户${i + 5}`,
age: 25 + (i % 10),
city: ["北京", "上海", "深圳", "广州", "杭州"][i % 5],
job: ["开发工程师", "设计师", "产品经理", "测试工程师", "运营"][i % 5],
email: `user${i + 5}@example.com`,
phone: `1380013800${(i + 5).toString().padStart(2, '0')}`,
status: i % 3 === 0 ? "离职" : "在职"
}))
]);
</script>
<style scoped>
h3, h4 {
margin: 20px 0 10px 0;
color: #333;
}
p {
margin: 10px 0;
color: #666;
font-size: 14px;
}
.bp-table {
margin-bottom: 30px;
}
div[style*="border"] {
margin-bottom: 20px;
border-radius: 4px;
background-color: #fafafa;
}
</style>
横向滚动示例
当列数较多时,表格会自动启用横向滚动,确保每列都有适当的宽度显示
自动横向滚动(无限制)
1 | 张三 | 28 | 北京 | 前端开发工程师 | zhangsan@example.com | 13800138001 | 北京市朝阳区建国门外大街1号 | 科技有限公司 | 15000 |
2 | 李四 | 32 | 上海 | 后端开发工程师 | lisi@example.com | 13800138002 | 上海市浦东新区世纪大道100号 | 互联网公司 | 18000 |
3 | 王五 | 26 | 深圳 | UI设计师 | wangwu@example.com | 13800138003 | 深圳市南山区科技园南区 | 设计工作室 | 12000 |
4 | 赵六 | 30 | 广州 | 产品经理 | zhaoliu@example.com | 13800138004 | 广州市天河区珠江新城 | 创业公司 | 20000 |
指定横向滚动宽度(1200px)
1 | 张三 | 28 | 北京 | 前端开发工程师 | zhangsan@example.com | 13800138001 | 北京市朝阳区建国门外大街1号 | 科技有限公司 | 15000 |
2 | 李四 | 32 | 上海 | 后端开发工程师 | lisi@example.com | 13800138002 | 上海市浦东新区世纪大道100号 | 互联网公司 | 18000 |
3 | 王五 | 26 | 深圳 | UI设计师 | wangwu@example.com | 13800138003 | 深圳市南山区科技园南区 | 设计工作室 | 12000 |
4 | 赵六 | 30 | 广州 | 产品经理 | zhaoliu@example.com | 13800138004 | 广州市天河区珠江新城 | 创业公司 | 20000 |
横向 + 纵向滚动
1 | 张三 | 28 | 北京 | 前端开发工程师 | zhangsan@example.com | 13800138001 | 北京市朝阳区建国门外大街1号 |
2 | 李四 | 32 | 上海 | 后端开发工程师 | lisi@example.com | 13800138002 | 上海市浦东新区世纪大道100号 |
3 | 王五 | 26 | 深圳 | UI设计师 | wangwu@example.com | 13800138003 | 深圳市南山区科技园南区 |
4 | 赵六 | 30 | 广州 | 产品经理 | zhaoliu@example.com | 13800138004 | 广州市天河区珠江新城 |
5 | 用户5 | 25 | 北京 | 开发工程师 | user5@example.com | 13800138005 | 地址5号 |
6 | 用户6 | 26 | 上海 | 设计师 | user6@example.com | 13800138006 | 地址6号 |
7 | 用户7 | 27 | 深圳 | 产品经理 | user7@example.com | 13800138007 | 地址7号 |
8 | 用户8 | 28 | 广州 | 测试工程师 | user8@example.com | 13800138008 | 地址8号 |
9 | 用户9 | 29 | 杭州 | 运营 | user9@example.com | 13800138009 | 地址9号 |
10 | 用户10 | 30 | 北京 | 开发工程师 | user10@example.com | 138001380010 | 地址10号 |
11 | 用户11 | 31 | 上海 | 设计师 | user11@example.com | 138001380011 | 地址11号 |
12 | 用户12 | 32 | 深圳 | 产品经理 | user12@example.com | 138001380012 | 地址12号 |
13 | 用户13 | 33 | 广州 | 测试工程师 | user13@example.com | 138001380013 | 地址13号 |
14 | 用户14 | 34 | 杭州 | 运营 | user14@example.com | 138001380014 | 地址14号 |
15 | 用户15 | 25 | 北京 | 开发工程师 | user15@example.com | 138001380015 | 地址15号 |
16 | 用户16 | 26 | 上海 | 设计师 | user16@example.com | 138001380016 | 地址16号 |
17 | 用户17 | 27 | 深圳 | 产品经理 | user17@example.com | 138001380017 | 地址17号 |
18 | 用户18 | 28 | 广州 | 测试工程师 | user18@example.com | 138001380018 | 地址18号 |
19 | 用户19 | 29 | 杭州 | 运营 | user19@example.com | 138001380019 | 地址19号 |
20 | 用户20 | 30 | 北京 | 开发工程师 | user20@example.com | 138001380020 | 地址20号 |
21 | 用户21 | 31 | 上海 | 设计师 | user21@example.com | 138001380021 | 地址21号 |
22 | 用户22 | 32 | 深圳 | 产品经理 | user22@example.com | 138001380022 | 地址22号 |
23 | 用户23 | 33 | 广州 | 测试工程师 | user23@example.com | 138001380023 | 地址23号 |
24 | 用户24 | 34 | 杭州 | 运营 | user24@example.com | 138001380024 | 地址24号 |
vue
<template>
<div>
<h3>横向滚动示例</h3>
<p>当列数较多时,表格会自动启用横向滚动,确保每列都有适当的宽度显示</p>
<!-- 无滚动限制 - 自动计算 -->
<h4>自动横向滚动(无限制)</h4>
<bp-table
:data="list"
border
row-key="id"
style="width: 800px;"
>
<template #columns>
<bp-table-column title="ID" data-index="id" :width="80" />
<bp-table-column title="姓名" data-index="name" :width="120" />
<bp-table-column title="年龄" data-index="age" :width="80" />
<bp-table-column title="城市" data-index="city" :width="120" />
<bp-table-column title="职业" data-index="job" :width="150" />
<bp-table-column title="邮箱" data-index="email" :width="200" />
<bp-table-column title="电话" data-index="phone" :width="150" />
<bp-table-column title="地址" data-index="address" :width="250" />
<bp-table-column title="公司" data-index="company" :width="180" />
<bp-table-column title="薪资" data-index="salary" :width="120" />
</template>
</bp-table>
<!-- 指定横向滚动宽度 -->
<h4>指定横向滚动宽度(1200px)</h4>
<bp-table
:data="list"
border
row-key="id"
:scroll="{ x: 1200 }"
style="width: 600px;"
>
<template #columns>
<bp-table-column title="ID" data-index="id" :width="80" />
<bp-table-column title="姓名" data-index="name" :width="120" />
<bp-table-column title="年龄" data-index="age" :width="80" />
<bp-table-column title="城市" data-index="city" :width="120" />
<bp-table-column title="职业" data-index="job" :width="150" />
<bp-table-column title="邮箱" data-index="email" :width="200" />
<bp-table-column title="电话" data-index="phone" :width="150" />
<bp-table-column title="地址" data-index="address" :width="250" />
<bp-table-column title="公司" data-index="company" :width="180" />
<bp-table-column title="薪资" data-index="salary" :width="120" />
</template>
</bp-table>
<!-- 同时设置横向和纵向滚动 -->
<h4>横向 + 纵向滚动</h4>
<bp-table
:data="bigList"
border
row-key="id"
:scroll="{ x: 1000, y: 300 }"
style="width: 700px;"
>
<template #columns>
<bp-table-column title="ID" data-index="id" :width="80" />
<bp-table-column title="姓名" data-index="name" :width="120" />
<bp-table-column title="年龄" data-index="age" :width="80" />
<bp-table-column title="城市" data-index="city" :width="120" />
<bp-table-column title="职业" data-index="job" :width="150" />
<bp-table-column title="邮箱" data-index="email" :width="200" />
<bp-table-column title="电话" data-index="phone" :width="150" />
<bp-table-column title="地址" data-index="address" :width="250" />
</template>
</bp-table>
</div>
</template>
<script setup lang="ts">
import { ref } from "vue";
const list = ref([
{
id: 1,
name: "张三",
age: 28,
city: "北京",
job: "前端开发工程师",
email: "zhangsan@example.com",
phone: "13800138001",
address: "北京市朝阳区建国门外大街1号",
company: "科技有限公司",
salary: "15000"
},
{
id: 2,
name: "李四",
age: 32,
city: "上海",
job: "后端开发工程师",
email: "lisi@example.com",
phone: "13800138002",
address: "上海市浦东新区世纪大道100号",
company: "互联网公司",
salary: "18000"
},
{
id: 3,
name: "王五",
age: 26,
city: "深圳",
job: "UI设计师",
email: "wangwu@example.com",
phone: "13800138003",
address: "深圳市南山区科技园南区",
company: "设计工作室",
salary: "12000"
},
{
id: 4,
name: "赵六",
age: 30,
city: "广州",
job: "产品经理",
email: "zhaoliu@example.com",
phone: "13800138004",
address: "广州市天河区珠江新城",
company: "创业公司",
salary: "20000"
}
]);
// 生成更多数据用于测试纵向滚动
const bigList = ref([
...list.value,
...Array.from({ length: 20 }, (_, i) => ({
id: i + 5,
name: `用户${i + 5}`,
age: 25 + (i % 10),
city: ["北京", "上海", "深圳", "广州", "杭州"][i % 5],
job: ["开发工程师", "设计师", "产品经理", "测试工程师", "运营"][i % 5],
email: `user${i + 5}@example.com`,
phone: `1380013800${i + 5}`,
address: `地址${i + 5}号`,
company: `公司${i + 5}`,
salary: `${10000 + i * 1000}`
}))
]);
</script>
<style scoped>
h3, h4 {
margin: 20px 0 10px 0;
color: #333;
}
p {
margin: 10px 0;
color: #666;
font-size: 14px;
}
.bp-table {
margin-bottom: 30px;
}
</style>