背景
笔者公司的一个项目大量使用el-table组件,并做出一些魔改的效果
多列显示
废话不多讲,直接上效果
使用el-table组件的多级表头,不存在滴
核心代码如下
<script setup lang="ts">
import { ref, computed } from 'vue'
import { Search, Refresh, Edit, Delete, View } from '@element-plus/icons-vue'
interface User {
id: number
avatar: string
username: string
realName: string
email: string
phone: string
gender: 'male' | 'female' | 'unknown'
age: number
department: string
position: string
status: 'active' | 'inactive' | 'banned'
registerTime: string
lastLoginTime: string
province: string
city: string
address: string
salary: number
education: string
workYears: number
}
const loading = ref(false)
const searchText = ref('')
const statusFilter = ref('')
const departmentFilter = ref('')
const currentPage = ref(1)
const pageSize = ref(10)
const departments = ['技术部', '产品部', '设计部', '市场部', '运营部', '人事部', '财务部']
const positions = ['工程师', '高级工程师', '技术经理', '产品经理', '设计师', '运营专员', 'HR专员', '财务专员']
const educations = ['高中', '大专', '本科', '硕士', '博士']
const provinces = ['北京', '上海', '广东', '浙江', '江苏', '四川', '湖北']
const generateMockData = (): User[] => {
const data: User[] = []
for (let i = 1; i <= 100; i++) {
data.push({
id: i,
avatar: `https://api.dicebear.com/7.x/avataaars/svg?seed=${i}`,
username: `user${i}`,
realName: `用户${i}`,
email: `user${i}@example.com`,
phone: `138${String(i).padStart(8, '0')}`,
gender: ['male', 'female', 'unknown'][i % 3] as User['gender'],
age: 20 + (i % 30),
department: departments[i % departments.length],
position: positions[i % positions.length],
status: ['active', 'inactive', 'banned'][i % 3] as User['status'],
registerTime: `2023-${String((i % 12) + 1).padStart(2, '0')}-${String((i % 28) + 1).padStart(2, '0')} 10:30:00`,
lastLoginTime: `2024-${String((i % 12) + 1).padStart(2, '0')}-${String((i % 28) + 1).padStart(2, '0')} 14:20:00`,
province: provinces[i % provinces.length],
city: '市区',
address: `街道${i}号`,
salary: 8000 + (i % 20) * 1000,
education: educations[i % educations.length],
workYears: i % 15,
})
}
return data
}
const allUsers = ref<User[]>(generateMockData())
const filteredUsers = computed(() => {
let result = allUsers.value
if (searchText.value) {
const search = searchText.value.toLowerCase()
result = result.filter(
(user) =>
user.username.toLowerCase().includes(search) ||
user.realName.toLowerCase().includes(search) ||
user.email.toLowerCase().includes(search) ||
user.phone.includes(search)
)
}
if (statusFilter.value) {
result = result.filter((user) => user.status === statusFilter.value)
}
if (departmentFilter.value) {
result = result.filter((user) => user.department === departmentFilter.value)
}
return result
})
const paginatedUsers = computed(() => {
const start = (currentPage.value - 1) * pageSize.value
const end = start + pageSize.value
return filteredUsers.value.slice(start, end)
})
const total = computed(() => filteredUsers.value.length)
const getGenderText = (gender: string) => {
const map: Record<string, string> = {
male: '男',
female: '女',
unknown: '未知',
}
return map[gender] || '未知'
}
const getStatusType = (status: string) => {
const map: Record<string, string> = {
active: 'success',
inactive: 'warning',
banned: 'danger',
}
return map[status] || 'info'
}
const getStatusText = (status: string) => {
const map: Record<string, string> = {
active: '正常',
inactive: '未激活',
banned: '已禁用',
}
return map[status] || '未知'
}
const handleSearch = () => {
currentPage.value = 1
}
const handleReset = () => {
searchText.value = ''
statusFilter.value = ''
departmentFilter.value = ''
currentPage.value = 1
}
const handleView = (row: User) => {
console.log('查看用户:', row)
}
const handleEdit = (row: User) => {
console.log('编辑用户:', row)
}
const handleDelete = (row: User) => {
console.log('删除用户:', row)
}
const handleSizeChange = (val: number) => {
pageSize.value = val
currentPage.value = 1
}
const handleCurrentChange = (val: number) => {
currentPage.value = val
}
const formatSalary = (salary: number) => {
return `¥${salary.toLocaleString()}`
}
</script>
<template>
<div class="user-list-container">
<el-card class="search-card">
<el-form :inline="true" class="search-form">
<el-form-item label="关键词">
<el-input
v-model="searchText"
placeholder="用户名/姓名/邮箱/手机"
clearable
:prefix-icon="Search"
@keyup.enter="handleSearch"
/>
</el-form-item>
<el-form-item label="状态">
<el-select v-model="statusFilter" placeholder="全部" clearable style="width: 120px">
<el-option label="正常" value="active" />
<el-option label="未激活" value="inactive" />
<el-option label="已禁用" value="banned" />
</el-select>
</el-form-item>
<el-form-item label="部门">
<el-select v-model="departmentFilter" placeholder="全部" clearable style="width: 120px">
<el-option v-for="dept in departments" :key="dept" :label="dept" :value="dept" />
</el-select>
</el-form-item>
<el-form-item>
<el-button type="primary" :icon="Search" @click="handleSearch">搜索</el-button>
<el-button :icon="Refresh" @click="handleReset">重置</el-button>
</el-form-item>
</el-form>
</el-card>
<el-card class="table-card">
<el-table
:data="paginatedUsers"
v-loading="loading"
border
stripe
highlight-current-row
style="width: 100%"
:header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
>
<el-table-column type="selection" width="50" fixed="left" />
<el-table-column prop="id" label="ID" width="70" fixed="left" sortable>
<template #default="{ row, $index }">
{{ $index === 0 ? '' : row.id }}
</template>
</el-table-column>
<el-table-column label="头像" width="80">
<template #default="{ row, $index }">
<el-avatar v-if="$index !== 0" :size="40" :src="row.avatar" />
</template>
</el-table-column>
<el-table-column prop="username" label="用户名" width="120" show-overflow-tooltip>
<template #default="{ row, $index }">
{{ $index === 0 ? '' : row.username }}
</template>
</el-table-column>
<el-table-column prop="realName" label="姓名" width="100" show-overflow-tooltip>
<template #default="{ row, $index }">
{{ $index === 0 ? '' : row.realName }}
</template>
</el-table-column>
<el-table-column prop="gender" label="性别" width="80">
<template #default="{ row, $index }">
{{ $index === 0 ? '' : getGenderText(row.gender) }}
</template>
</el-table-column>
<el-table-column prop="age" label="年龄" width="70" sortable>
<template #default="{ row, $index }">
{{ $index === 0 ? '' : row.age }}
</template>
</el-table-column>
<el-table-column prop="phone" label="手机号" width="130">
<template #default="{ row, $index }">
{{ $index === 0 ? '' : row.phone }}
</template>
</el-table-column>
<el-table-column prop="email" label="邮箱" width="180" show-overflow-tooltip>
<template #default="{ row, $index }">
{{ $index === 0 ? '' : row.email }}
</template>
</el-table-column>
<el-table-column prop="department" label="部门" width="100">
<template #default="{ row, $index }">
{{ $index === 0 ? '' : row.department }}
</template>
</el-table-column>
<el-table-column prop="position" label="职位" width="120">
<template #default="{ row, $index }">
{{ $index === 0 ? '' : row.position }}
</template>
</el-table-column>
<el-table-column prop="education" label="学历" width="80">
<template #default="{ row, $index }">
{{ $index === 0 ? '' : row.education }}
</template>
</el-table-column>
<el-table-column prop="workYears" label="工龄" width="70" sortable>
<template #default="{ row, $index }">
{{ $index === 0 ? '' : `${row.workYears}年` }}
</template>
</el-table-column>
<el-table-column prop="salary" label="薪资" width="100" sortable>
<template #default="{ row, $index }">
{{ $index === 0 ? '' : formatSalary(row.salary) }}
</template>
</el-table-column>
<el-table-column prop="status" label="状态" width="100">
<template #default="{ row, $index }">
<span v-if="$index === 0">
{{ '' }}
</span>
<el-tag v-else :type="getStatusType(row.status) as any">
{{ getStatusText(row.status) }}
</el-tag>
</template>
</el-table-column>
<el-table-column prop="province" label="" width="80">
<template #default="{ row, $index }">
{{ $index === 0 ? '省份' : row.province }}
</template>
</el-table-column>
<el-table-column prop="city" label="地址" width="80">
<template #default="{ row, $index }">
{{ $index === 0 ? '市' : row.city }}
</template>
</el-table-column>
<el-table-column prop="address" label="" width="120" show-overflow-tooltip>
<template #default="{ row, $index }">
{{ $index === 0 ? '街道' : row.address }}
</template>
</el-table-column>
<el-table-column prop="registerTime" label="注册时间" width="170" sortable>
<template #default="{ row, $index }">
{{ $index === 0 ? '' : row.registerTime }}
</template>
</el-table-column>
<el-table-column prop="lastLoginTime" label="最后登录" width="170" sortable>
<template #default="{ row, $index }">
{{ $index === 0 ? '' : row.lastLoginTime }}
</template>
</el-table-column>
<el-table-column label="操作" width="180" fixed="right">
<template #default="{ row, $index }">
<template v-if="$index !== 0">
<el-button type="primary" link :icon="View" @click="handleView(row)">查看</el-button>
<el-button type="warning" link :icon="Edit" @click="handleEdit(row)">编辑</el-button>
<el-popconfirm title="确定删除该用户吗?" @confirm="handleDelete(row)">
<template #reference>
<el-button type="danger" link :icon="Delete">删除</el-button>
</template>
</el-popconfirm>
</template>
</template>
</el-table-column>
</el-table>
<div class="pagination-container">
<el-pagination
v-model:current-page="currentPage"
v-model:page-size="pageSize"
:page-sizes="[10, 20, 50, 100]"
:total="total"
layout="total, sizes, prev, pager, next, jumper"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
</el-card>
</div>
</template>
<style scoped>
.user-list-container {
padding: 20px;
}
.search-card {
margin-bottom: 20px;
}
.search-form {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.table-card {
width: 100%;
}
.pagination-container {
display: flex;
justify-content: flex-end;
margin-top: 20px;
}
:deep(.el-table__header-wrapper thead tr th:nth-of-type(16)) {
border-right: 0;
}
:deep(.el-table__header-wrapper thead tr th:nth-of-type(17)) {
border-right: 0;
}
</style>
表格行按钮
点击Table中间的行按钮,展开所有数据
核心代码如下
<script setup lang="ts">
import { ref, computed } from 'vue'
// 客服人员接口
interface ServiceAgent {
id: number
name: string
csm: string
workType: string
channel: string
serviceHours: number
omConfirmHours: number
internalHours: number
customerHours: number
autoMove: string
csmBonus: string
location: string
language: string
jobType: string
shiftType: string
status: string
}
// 公司分组接口
interface CompanyGroup {
id: number
companyName: string
agentCount: number
mealBreakBilling: string
serviceHoursTotal: number
omConfirmHoursTotal: number
internalHoursTotal: number
customerHoursTotal: number
agents: ServiceAgent[]
}
// 展开状态管理
const expandedGroups = ref<Set<number>>(new Set())
const expandedAgents = ref<Map<number, number>>(new Map()) // groupId -> 显示的agent数量
const DEFAULT_VISIBLE_COUNT = 3 // 默认显示的客服数量
// 模拟数据
const generateMockData = (): CompanyGroup[] => {
const csms = ['Lisa Liu', 'Reachel Kuang', 'John Smith']
const channels = ['中国直聘', '地聘+菲律宾', '海外招聘', '内部推荐']
const locations = ['中国', '菲律宾', '美国']
const languages = ['中文(普通话)', '英语', '日语', '韩语']
const jobTypes = ['语音', '在线', '邮件']
const shiftTypes = ['白班', '晚班', '轮班']
const companies: CompanyGroup[] = [
{
id: 1,
companyName: 'LUCHAR INC',
agentCount: 1,
mealBreakBilling: '计费',
serviceHoursTotal: 144,
omConfirmHoursTotal: 144,
internalHoursTotal: 0,
customerHoursTotal: 0,
agents: [
{
id: 101,
name: 'Christine Ramirez',
csm: 'Lisa Liu',
workType: '全职',
channel: '地聘+菲律宾',
serviceHours: 144,
omConfirmHours: 144,
internalHours: 144,
customerHours: 144,
autoMove: '未确认',
csmBonus: '$ +300.00',
location: '菲律宾',
language: '英语',
jobType: '语音',
shiftType: '白班',
status: '在职',
},
],
},
{
id: 2,
companyName: 'ReliableInsuranceAgency',
agentCount: 4,
mealBreakBilling: '不计费',
serviceHoursTotal: 561.1,
omConfirmHoursTotal: 574,
internalHoursTotal: 574,
customerHoursTotal: 0,
agents: [
{
id: 201,
name: 'Flyta Wu',
csm: 'Reachel Kuang',
workType: '全职',
channel: '中国直聘',
serviceHours: 135,
omConfirmHours: 144,
internalHours: 144,
customerHours: 144,
autoMove: '未确认',
csmBonus: '—',
location: '中国',
language: '中文(普通话)',
jobType: '语音',
shiftType: '晚班',
status: '在职',
},
{
id: 202,
name: 'GiGi Li',
csm: 'Reachel Kuang',
workType: '全职',
channel: '中国直聘',
serviceHours: 144,
omConfirmHours: 144,
internalHours: 144,
customerHours: 144,
autoMove: '未确认',
csmBonus: '—',
location: '中国',
language: '中文(普通话)',
jobType: '语音',
shiftType: '晚班',
status: '在职',
},
{
id: 203,
name: 'Ke Xu',
csm: 'Reachel Kuang',
workType: '全职',
channel: '中国直聘',
serviceHours: 144,
omConfirmHours: 144,
internalHours: 144,
customerHours: 144,
autoMove: '未确认',
csmBonus: '—',
location: '中国',
language: '中文(普通话)',
jobType: '语音',
shiftType: '白班',
status: '在职',
},
{
id: 204,
name: 'Tiffany Ge',
csm: 'Reachel Kuang',
workType: '全职',
channel: '中国直聘',
serviceHours: 138.1,
omConfirmHours: 142,
internalHours: 142,
customerHours: 142,
autoMove: '未确认',
csmBonus: '—',
location: '中国',
language: '中文(普通话)',
jobType: '语音',
shiftType: '白班',
status: '在职',
},
],
},
{
id: 3,
companyName: 'MTO Moving Inc.',
agentCount: 1,
mealBreakBilling: '不计费',
serviceHoursTotal: 217,
omConfirmHoursTotal: 217,
internalHoursTotal: 217,
customerHoursTotal: 0,
agents: [
{
id: 301,
name: 'Mike Chen',
csm: 'John Smith',
workType: '全职',
channel: '中国直聘',
serviceHours: 217,
omConfirmHours: 217,
internalHours: 217,
customerHours: 217,
autoMove: '已确认',
csmBonus: '$ +150.00',
location: '中国',
language: '中文(普通话)',
jobType: '在线',
shiftType: '白班',
status: '在职',
},
],
},
]
return companies
}
const companyGroups = ref<CompanyGroup[]>(generateMockData())
// 获取分组的可见客服列表
const getVisibleAgents = (group: CompanyGroup) => {
const visibleCount = expandedAgents.value.get(group.id) || DEFAULT_VISIBLE_COUNT
return group.agents.slice(0, visibleCount)
}
// 判断是否有更多客服可以展开
const hasMoreAgents = (group: CompanyGroup) => {
const visibleCount = expandedAgents.value.get(group.id) || DEFAULT_VISIBLE_COUNT
return group.agents.length > visibleCount
}
// 获取剩余客服数量
const getRemainingCount = (group: CompanyGroup) => {
const visibleCount = expandedAgents.value.get(group.id) || DEFAULT_VISIBLE_COUNT
return group.agents.length - visibleCount
}
// 判断是否已展开全部
const isFullyExpanded = (group: CompanyGroup) => {
const visibleCount = expandedAgents.value.get(group.id) || DEFAULT_VISIBLE_COUNT
return visibleCount >= group.agents.length
}
// 展开显示更多客服
const expandMore = (group: CompanyGroup) => {
expandedAgents.value.set(group.id, group.agents.length)
}
// 收起客服列表
const collapseAgents = (group: CompanyGroup) => {
expandedAgents.value.set(group.id, DEFAULT_VISIBLE_COUNT)
}
// 处理详情点击
const handleDetail = (agent: ServiceAgent) => {
console.log('查看详情:', agent)
}
// 选中状态
const selectedRows = ref<Set<number>>(new Set())
const selectedAgents = ref<Set<number>>(new Set())
const toggleGroupSelection = (group: CompanyGroup) => {
if (selectedRows.value.has(group.id)) {
selectedRows.value.delete(group.id)
group.agents.forEach((agent) => selectedAgents.value.delete(agent.id))
} else {
selectedRows.value.add(group.id)
group.agents.forEach((agent) => selectedAgents.value.add(agent.id))
}
}
const toggleAgentSelection = (agent: ServiceAgent) => {
if (selectedAgents.value.has(agent.id)) {
selectedAgents.value.delete(agent.id)
} else {
selectedAgents.value.add(agent.id)
}
}
const isGroupSelected = (group: CompanyGroup) => {
return selectedRows.value.has(group.id)
}
const isAgentSelected = (agent: ServiceAgent) => {
return selectedAgents.value.has(agent.id)
}
</script>
<template>
<div class="grouped-table-container">
<div class="table-toolbar">
<el-button type="primary" size="small">批量导出</el-button>
</div>
<div class="custom-table">
<!-- 表头 -->
<div class="table-header">
<div class="header-cell checkbox-cell">
<el-checkbox />
</div>
<div class="header-cell company-cell">名称</div>
<div class="header-cell">负责人</div>
<div class="header-cell">类型</div>
<div class="header-cell">来源</div>
<div class="header-cell">工时A(h)</div>
<div class="header-cell">工时B(h)</div>
<div class="header-cell">工时C(h)</div>
<div class="header-cell">工时D(h)</div>
<div class="header-cell">全勤</div>
<div class="header-cell">备注</div>
<div class="header-cell">地区</div>
<div class="header-cell">分类</div>
<div class="header-cell">方式</div>
<div class="header-cell">时段</div>
<div class="header-cell">状态</div>
<div class="header-cell action-cell">操作</div>
</div>
<!-- 表格内容 -->
<div class="table-body">
<template v-for="group in companyGroups" :key="group.id">
<!-- 分组行 -->
<div class="group-row">
<div class="body-cell checkbox-cell">
<el-checkbox
:model-value="isGroupSelected(group)"
@change="toggleGroupSelection(group)"
/>
</div>
<div class="body-cell company-cell">
<span class="company-name">{{ group.companyName }}</span>
<span class="agent-count">员工人数: {{ group.agentCount }}</span>
</div>
<div class="body-cell"></div>
<div class="body-cell billing-info">休息时间是否计费: {{ group.mealBreakBilling }}</div>
<div class="body-cell"></div>
<div class="body-cell">{{ group.serviceHoursTotal }}</div>
<div class="body-cell"></div>
<div class="body-cell">{{ group.omConfirmHoursTotal }}</div>
<div class="body-cell">{{ group.internalHoursTotal }}</div>
<div class="body-cell"></div>
<div class="body-cell"></div>
<div class="body-cell"></div>
<div class="body-cell"></div>
<div class="body-cell"></div>
<div class="body-cell"></div>
<div class="body-cell"></div>
<div class="body-cell action-cell"></div>
</div>
<!-- 客服行 -->
<div
v-for="agent in getVisibleAgents(group)"
:key="agent.id"
class="agent-row"
>
<div class="body-cell checkbox-cell">
<el-checkbox
:model-value="isAgentSelected(agent)"
@change="toggleAgentSelection(agent)"
/>
</div>
<div class="body-cell company-cell agent-name">{{ agent.name }}</div>
<div class="body-cell">{{ agent.csm }}</div>
<div class="body-cell">{{ agent.workType }}</div>
<div class="body-cell">{{ agent.channel }}</div>
<div class="body-cell">{{ agent.serviceHours }}</div>
<div class="body-cell">
<span class="confirm-status">{{ agent.omConfirmHours }}</span>
</div>
<div class="body-cell">
<span class="unconfirmed">{{ agent.autoMove }}</span>
</div>
<div class="body-cell">
<span class="confirm-status">{{ agent.internalHours }}</span>
</div>
<div class="body-cell">{{ agent.customerHours }}</div>
<div class="body-cell">
<span class="unconfirmed">{{ agent.autoMove }}</span>
</div>
<div class="body-cell bonus-cell">{{ agent.csmBonus }}</div>
<div class="body-cell">{{ agent.location }}</div>
<div class="body-cell">{{ agent.language }}</div>
<div class="body-cell">{{ agent.jobType }}</div>
<div class="body-cell">{{ agent.shiftType }}</div>
<div class="body-cell">
<el-tag type="success" size="small">{{ agent.status }}</el-tag>
</div>
<div class="body-cell action-cell">
<el-button type="primary" link size="small" @click="handleDetail(agent)">
详情
</el-button>
</div>
</div>
<!-- 展开/收起链接 -->
<div v-if="group.agents.length > DEFAULT_VISIBLE_COUNT" class="expand-row">
<div class="expand-content">
<span
v-if="!isFullyExpanded(group)"
class="expand-link"
@click="expandMore(group)"
>
查看剩余 {{ getRemainingCount(group) }} 个客服 ∨
</span>
<span
v-else
class="expand-link"
@click="collapseAgents(group)"
>
收起 ∧
</span>
</div>
</div>
</template>
</div>
</div>
</div>
</template>
<style scoped>
.grouped-table-container {
padding: 16px;
background: #fff;
}
.table-toolbar {
margin-bottom: 12px;
}
.custom-table {
border: 1px solid #ebeef5;
border-radius: 4px;
overflow: hidden;
overflow-x: auto;
}
.table-header {
display: flex;
background: #f5f7fa;
border-bottom: 1px solid #ebeef5;
font-weight: 500;
color: #606266;
font-size: 13px;
min-width: fit-content;
}
.header-cell {
padding: 8px 6px;
min-width: 80px;
flex: 1;
text-align: center;
border-right: 1px solid #ebeef5;
white-space: nowrap;
}
.header-cell:last-child {
border-right: none;
}
.checkbox-cell {
min-width: 40px;
max-width: 40px;
flex: none;
}
.company-cell {
min-width: 160px;
flex: 1.5;
text-align: left;
padding-left: 12px;
}
.action-cell {
min-width: 60px;
max-width: 60px;
flex: none;
}
.table-body {
max-height: 500px;
overflow-y: auto;
min-width: fit-content;
}
.group-row {
display: flex;
background: #fafafa;
border-bottom: 1px solid #ebeef5;
font-weight: 500;
}
.group-row .body-cell {
color: #303133;
}
.agent-row {
display: flex;
border-bottom: 1px solid #ebeef5;
}
.agent-row:hover {
background: #f5f7fa;
}
.body-cell {
padding: 8px 6px;
min-width: 80px;
flex: 1;
display: flex;
align-items: center;
justify-content: center;
font-size: 13px;
color: #606266;
border-right: 1px solid #ebeef5;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.body-cell:last-child {
border-right: none;
}
.company-cell.agent-name {
padding-left: 24px;
}
.company-name {
font-weight: 600;
color: #303133;
margin-right: 12px;
}
.agent-count {
color: #909399;
font-size: 12px;
font-weight: normal;
}
.billing-info {
color: #606266;
white-space: nowrap;
}
.confirm-status {
color: #67c23a;
}
.unconfirmed {
color: #67c23a;
}
.bonus-cell {
color: #67c23a;
}
.expand-row {
border-bottom: 1px solid #ebeef5;
padding: 6px 12px;
background: #fff;
}
.expand-content {
text-align: center;
}
.expand-link {
color: #409eff;
cursor: pointer;
font-size: 12px;
}
.expand-link:hover {
text-decoration: underline;
}
/* 自定义复选框样式 */
:deep(.el-checkbox) {
height: auto;
}
:deep(.el-tag--success) {
background-color: #f0f9eb;
border-color: #e1f3d8;
color: #67c23a;
}
:deep(.el-tag--small) {
padding: 0 6px;
height: 20px;
line-height: 18px;
}
</style>