
2023年Q4销售业绩
| 区域 |
产品A |
产品B |
总销售额 |
| 华东区 |
120000 |
80000 |
200000 |
| 华南区 |
95000 |
110000 |
205000 |
| 总计 |
405000 |
| 数据来源:内部销售系统 |
/* 针对小屏幕设备的响应式样式 */
@media screen and (max-width: 768px) {
.responsive-table-container {
overflow-x: auto; /* 简单粗暴但有效的方法 */
}
/* 更复杂的卡片式布局尝试 */
.card-table table,
.card-table thead,
.card-table tbody,
.card-table th,
.card-table td,
.card-table tr {
display: block;
}
.card-table thead tr {
position: absolute;
top: -9999px; /* 隐藏表头 */
left: -9999px;
}
.card-table tr {
border: 1px solid #ccc;
margin-bottom: 10px;
}
.card-table td {
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%; /* 为伪元素留出空间 */
text-align: right;
}
.card-table td:before {
position: absolute;
left: 6px;
content: attr(data-label); /* 显示data-label属性作为伪表头 */
font-weight: bold;
white-space: nowrap;
text-align: left;
}
}
| 区域 |
产品A |
产品B |
总销售额 |
| 华东区 |
120000 |
80000 |
200000 |
/* 基础表格样式 */
table {
width: 100%;
border-collapse: collapse; /* 合并边框 */
margin: 20px 0;
font-family: Arial, sans-serif;
box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1); /* 添加一些阴影效果 */
}
/* 表格标题样式 */
caption {
font-size: 1.5em;
font-weight: bold;
margin-bottom: 10px;
text-align: left;
color: #333;
}
/* 表头样式 */
th {
background-color: #4CAF50; /* 醒目的背景色 */
color: white;
padding: 12px 15px;
text-align: left;
border: 1px solid #ddd;
}
/* 单元格样式 */
td {
padding: 10px 15px;
border: 1px solid #ddd;
text-align: left;
vertical-align: middle; /* 垂直居中 */
}
/* 隔行变色 */
tbody tr:nth-child(even) {
background-color: #f2f2f2;
}
/* 鼠标悬停效果 */
tbody tr:hover {
background-color: #ddd;
cursor: pointer; /* 提示可交互 */
}
/* 数字列右对齐示例 */
td:nth-child(2), /* 假设第二列是数字 */
td:nth-child(3), /* 假设第三列是数字 */
td:nth-child(4) { /* 假设第四列是数字 */
text-align: right;
}document.addEventListener('DOMContentLoaded', () => {
const table = document.querySelector('table');
const headers = table.querySelectorAll('th');
const tbody = table.querySelector('tbody');
let sortDirection = {}; // 记录每列的排序方向
headers.forEach((header, index) => {
header.addEventListener('click', () => {
const rows = Array.from(tbody.querySelectorAll('tr'));
const isAsc = sortDirection[index] === 'asc'; // 判断当前列是否是升序
rows.sort((rowA, rowB) => {
const cellA = rowA.children[index].textContent.trim();
const cellB = rowB.children[index].textContent.trim();
// 尝试转换为数字进行比较
const numA = parseFloat(cellA);
const numB = parseFloat(cellB);
if (!isNaN(numA) && !isNaN(numB)) {
return isAsc ? numA - numB : numB - numA;
} else {
// 否则按字符串比较
return isAsc ? cellA.localeCompare(cellB) : cellB.localeCompare(cellA);
}
});
// 清空并重新添加排序后的行
while (tbody.firstChild) {
tbody.removeChild(tbody.firstChild);
}
rows.forEach(row => tbody.appendChild(row));
// 更新排序方向
sortDirection = {}; // 重置所有列的排序方向
sortDirection[index] = isAsc ? 'desc' : 'asc';
});
});
});