当前位置:实例文章 » HTML/CSS实例» [文章]<j-editable-table 根据 下拉框值 来隐藏 列

<j-editable-table 根据 下拉框值 来隐藏 列

发布人:shili8 发布时间:2025-02-13 13:19 阅读次数:0

**可编辑表格(Editable Table)基于下拉框值的隐藏列**

在实际应用中,我们经常需要根据用户选择的条件来动态调整数据显示。例如,在一个订单管理系统中,根据客户类型(个人或企业),我们可能需要展示不同的信息项。在这种情况下,使用可编辑表格(Editable Table)结合下拉框值来控制列的显示是非常合适的。

本文将介绍如何基于下拉框值来隐藏表格中的某些列。我们将使用 HTML、CSS 和 JavaScript 来实现这个功能。

###1. HTML 结构首先,我们需要定义一个基本的表格结构,包括一个下拉框和一个可编辑表格。

html<!-- 下拉框 -->
<select id="type" onchange="changeTable()">
 <option value="all">全部</option>
 <option value="personal">个人</option>
 <option value="enterprise">企业</option>
</select>

<!-- 可编辑表格 -->
<table id="editable-table">
 <thead>
 <tr>
 <th>序号</th>
 <th>名称</th>
 <!-- 根据下拉框值决定是否显示的列 -->
 <th id="type-th" style="display: none;">类型</th>
 <th id="address-th">地址</th>
 </tr>
 </thead>
 <tbody>
 <!-- 表格内容 -->
 </tbody>
</table>

###2. JavaScript 脚本接下来,我们需要编写 JavaScript 脚本来根据下拉框的值改变表格列的显示。
javascript// 根据下拉框值改变表格列的显示function changeTable() {
 const type = document.getElementById('type').value;
 const typeTh = document.getElementById('type-th');
 const addressTh = document.getElementById('address-th');

 // 根据类型决定是否显示类型列和地址列 if (type === 'all') {
 typeTh.style.display = '';
 addressTh.style.display = '';
 } else if (type === 'personal') {
 typeTh.style.display = '';
 addressTh.style.display = 'none';
 } else if (type === 'enterprise') {
 typeTh.style.display = 'none';
 addressTh.style.display = '';
 }
}

###3. CSS 样式最后,我们需要在 CSS 中定义一些样式来美化我们的表格。
css#editable-table {
 border-collapse: collapse;
}

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

th {
 background-color: #f0f0f0;
}

### 总结通过以上的步骤,我们成功地实现了一个基于下拉框值来控制表格列显示的功能。这个例子可以在实际应用中广泛使用,例如根据客户类型展示不同的信息项等。

**示例代码**

html<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <style>
 #editable-table {
 border-collapse: collapse;
 }

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

 th {
 background-color: #f0f0f0;
 }
 </style>
</head>
<body>
 <select id="type" onchange="changeTable()">
 <option value="all">全部</option>
 <option value="personal">个人</option>
 <option value="enterprise">企业</option>
 </select>

 <table id="editable-table">
 <thead>
 <tr>
 <th>序号</th>
 <th>名称</th>
 <!-- 根据下拉框值决定是否显示的列 -->
 <th id="type-th" style="display: none;">类型</th>
 <th id="address-th">地址</th>
 </tr>
 </thead>
 <tbody>
 <!-- 表格内容 -->
 <tr>
 <td>1</td>
 <td>张三</td>
 <td>个人</td>
 <td>北京市</td>
 </tr>
 <tr>
 <td>2</td>
 <td>李四</td>
 <td>企业</td>
 <td>上海市</td>
 </tr>
 </tbody>
 </table>

 <script>
 // 根据下拉框值改变表格列的显示 function changeTable() {
 const type = document.getElementById('type').value;
 const typeTh = document.getElementById('type-th');
 const addressTh = document.getElementById('address-th');

 // 根据类型决定是否显示类型列和地址列 if (type === 'all') {
 typeTh.style.display = '';
 addressTh.style.display = '';
 } else if (type === 'personal') {
 typeTh.style.display = '';
 addressTh.style.display = 'none';
 } else if (type === 'enterprise') {
 typeTh.style.display = 'none';
 addressTh.style.display = '';
 }
 }
 </script>
</body>
</html>

其他信息

其他资源

Top