生成一个h5页面,有两个筛选项和一个列表,列表支持分页
时间: 2023-11-27 08:51:53 浏览: 196
H5菜品推荐(粤菜).docx
好的,生成一个h5页面需要用到HTML、CSS和JavaScript技术,我可以给你提供一个简单的示例代码,你可以在此基础上进行修改和完善。
以下是示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>列表筛选</title>
<style>
/* 样式表 */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.filter {
background-color: #f2f2f2;
padding: 10px;
}
.filter select {
font-size: 16px;
padding: 5px;
margin-right: 10px;
}
.list {
margin: 10px;
}
.list table {
border-collapse: collapse;
width: 100%;
}
.list th, .list td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.list th {
background-color: #f2f2f2;
}
.pagination {
display: flex;
justify-content: center;
align-items: center;
margin-top: 10px;
}
.pagination button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px;
margin: 5px;
cursor: pointer;
font-size: 16px;
}
.pagination button:hover {
background-color: #3e8e41;
}
.pagination button:disabled {
background-color: #ddd;
cursor: default;
}
</style>
</head>
<body>
<div class="filter">
<label for="category">分类:</label>
<select id="category">
<option value="">全部</option>
<option value="1">分类1</option>
<option value="2">分类2</option>
<option value="3">分类3</option>
</select>
<label for="status">状态:</label>
<select id="status">
<option value="">全部</option>
<option value="1">状态1</option>
<option value="2">状态2</option>
<option value="3">状态3</option>
</select>
</div>
<div class="list">
<table>
<thead>
<tr>
<th>编号</th>
<th>名称</th>
<th>分类</th>
<th>状态</th>
</tr>
</thead>
<tbody id="listBody">
<!-- 列表内容动态生成 -->
</tbody>
</table>
<div class="pagination">
<button id="btnPrev" disabled>上一页</button>
<button id="btnNext">下一页</button>
</div>
</div>
<script>
// JavaScript代码
var currentPage = 1; // 当前页码
var pageSize = 10; // 每页显示的记录数
var filterCategory = ""; // 筛选的分类
var filterStatus = ""; // 筛选的状态
// 模拟数据,用于生成列表
var data = [
{id: 1, name: "商品1", category: 1, status: 1},
{id: 2, name: "商品2", category: 2, status: 2},
{id: 3, name: "商品3", category: 1, status: 3},
{id: 4, name: "商品4", category: 3, status: 1},
{id: 5, name: "商品5", category: 2, status: 2},
{id: 6, name: "商品6", category: 1, status: 3},
{id: 7, name: "商品7", category: 3, status: 1},
{id: 8, name: "商品8", category: 2, status: 2},
{id: 9, name: "商品9", category: 1, status: 3},
{id: 10, name: "商品10", category: 3, status: 1},
{id: 11, name: "商品11", category: 2, status: 2},
{id: 12, name: "商品12", category: 1, status: 3},
{id: 13, name: "商品13", category: 3, status: 1},
{id: 14, name: "商品14", category: 2, status: 2},
{id: 15, name: "商品15", category: 1, status: 3},
{id: 16, name: "商品16", category: 3, status: 1},
{id: 17, name: "商品17", category: 2, status: 2},
{id: 18, name: "商品18", category: 1, status: 3},
{id: 19, name: "商品19", category: 3, status: 1},
{id: 20, name: "商品20", category: 2, status: 2},
{id: 21, name: "商品21", category: 1, status: 3},
{id: 22, name: "商品22", category: 3, status: 1},
{id: 23, name: "商品23", category: 2, status: 2},
{id: 24, name: "商品24", category: 1, status: 3},
{id: 25, name: "商品25", category: 3, status: 1},
];
// 根据筛选条件过滤数据
function filterData() {
var filteredData = data;
if (filterCategory) {
filteredData = filteredData.filter(function(item) {
return item.category == filterCategory;
});
}
if (filterStatus) {
filteredData = filteredData.filter(function(item) {
return item.status == filterStatus;
});
}
return filteredData;
}
// 生成列表
function renderList(currentPage) {
var filteredData = filterData();
var totalPage = Math.ceil(filteredData.length / pageSize); // 总页数
var start = (currentPage - 1) * pageSize; // 当前页的起始位置
var end = start + pageSize; // 当前页的结束位置
var currentData = filteredData.slice(start, end); // 当前页的数据
// 生成列表内容
var html = "";
for (var i = 0; i < currentData.length; i++) {
var item = currentData[i];
html += "<tr>";
html += "<td>" + item.id + "</td>";
html += "<td>" + item.name + "</td>";
html += "<td>" + item.category + "</td>";
html += "<td>" + item.status + "</td>";
html += "</tr>";
}
document.getElementById("listBody").innerHTML = html;
// 更新分页控件状态
document.getElementById("btnPrev").disabled = (currentPage == 1);
document.getElementById("btnNext").disabled = (currentPage == totalPage);
}
// 绑定筛选条件改变事件
document.getElementById("category").addEventListener("change", function() {
filterCategory = this.value;
currentPage = 1;
renderList(currentPage);
});
document.getElementById("status").addEventListener("change", function() {
filterStatus = this.value;
currentPage = 1;
renderList(currentPage);
});
// 绑定分页控件事件
document.getElementById("btnPrev").addEventListener("click", function() {
currentPage--;
renderList(currentPage);
});
document.getElementById("btnNext").addEventListener("click", function() {
currentPage++;
renderList(currentPage);
});
// 初始化页面
renderList(currentPage);
</script>
</body>
</html>
```
以上代码中,通过HTML生成了一个包含两个筛选条件和一个列表的页面。通过CSS设置了一些基本的样式,使页面更加美观。
在JavaScript中,定义了一些变量和函数,用于生成列表、处理筛选条件和分页控件等操作。其中,通过模拟数据生成了一个包含25个商品的列表,每页显示10条记录,可以根据分类和状态进行筛选。
你可以将以上代码保存为一个HTML文件,并在浏览器中打开查看效果。通过修改和完善代码,你可以实现更加复杂和实用的列表筛选功能。
阅读全文