你是一名前端工程师,现在我需要写一个展示水果库存的系统:要求1、html css 和 js页面要完全分离。2、要求页面上展示水果名称,水果单价,水果库存和一个空白的输入框。3在展示框的上方有添加选项,当点击该选项时,跳转到添加页面,要求页面上有水果名称,水果单价,水果库存和说明。4、当点击水果名称时,跳转到修改水果信息的页面,要求页面上展示内容与添加页面一致。5、每个页面下方要有上翻页,下翻页,首页,尾页和页码展示
时间: 2024-03-07 10:47:18 浏览: 72
好的,这是一个比较详细的需求,需要分步骤来完成。首先,我们需要创建一个展示水果库存的页面,该页面要求完全分离html、css和js。下面是一个简单的实现过程:
1. 创建一个 html 文件,命名为 index.html,添加以下内容:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水果库存</title>
<link rel="stylesheet" href="./css/style.css">
</head>
<body>
<div class="header">
<h1>水果库存</h1>
<button class="add-btn">添加</button>
</div>
<div class="content">
<table>
<thead>
<tr>
<th>水果名称</th>
<th>水果单价</th>
<th>水果库存</th>
</tr>
</thead>
<tbody>
<!-- 这里用 js 生成表格内容 -->
</tbody>
</table>
<div class="pagination">
<button class="prev-btn">上一页</button>
<button class="next-btn">下一页</button>
<button class="first-btn">首页</button>
<button class="last-btn">尾页</button>
<span class="page-num">1/10</span>
</div>
</div>
<script src="./js/app.js"></script>
</body>
</html>
```
2. 创建一个 css 文件,命名为 style.css,添加以下内容:
```css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
background-color: #eee;
}
.add-btn {
padding: 5px 10px;
background-color: #333;
color: #fff;
border: none;
cursor: pointer;
}
.content {
padding: 10px;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 10px;
}
th, td {
border: 1px solid #ccc;
padding: 5px;
text-align: center;
}
.pagination {
display: flex;
justify-content: center;
align-items: center;
margin-top: 10px;
}
.pagination button, .page-num {
margin: 0 5px;
padding: 5px 10px;
border: none;
cursor: pointer;
background-color: #eee;
}
.pagination button:hover, .page-num.active {
background-color: #333;
color: #fff;
}
```
3. 创建一个 js 文件,命名为 app.js,添加以下内容:
```js
// 模拟数据
const data = [
{ name: '苹果', price: 5.0, stock: 50 },
{ name: '香蕉', price: 3.0, stock: 100 },
{ name: '橙子', price: 4.0, stock: 80 },
{ name: '葡萄', price: 8.0, stock: 30 },
{ name: '草莓', price: 15.0, stock: 20 },
{ name: '芒果', price: 12.0, stock: 40 },
{ name: '榴莲', price: 25.0, stock: 10 },
{ name: '椰子', price: 20.0, stock: 15 },
{ name: '西瓜', price: 6.0, stock: 60 },
{ name: '哈密瓜', price: 10.0, stock: 40 }
];
// 获取元素
const tableBody = document.querySelector('tbody');
const pagination = document.querySelector('.pagination');
const prevBtn = document.querySelector('.prev-btn');
const nextBtn = document.querySelector('.next-btn');
const firstBtn = document.querySelector('.first-btn');
const lastBtn = document.querySelector('.last-btn');
const pageNum = document.querySelector('.page-num');
const addBtn = document.querySelector('.add-btn');
// 定义常量
const pageSize = 5; // 每页显示的数据条数
let currentPage = 1; // 当前页码
// 渲染表格
function renderTable(data) {
let html = '';
for (let i = 0; i < data.length; i++) {
html += `
<tr>
<td>${data[i].name}</td>
<td>${data[i].price}</td>
<td>${data[i].stock}</td>
</tr>
`;
}
tableBody.innerHTML = html;
}
// 渲染页码
function renderPageNum(data) {
const totalPage = Math.ceil(data.length / pageSize); // 总页数
const pageArr = [];
for (let i = 1; i <= totalPage; i++) {
pageArr.push(i);
}
const start = (currentPage - 1) * pageSize + 1;
const end = currentPage * pageSize < data.length ? currentPage * pageSize : data.length;
pageNum.innerHTML = `${start}-${end}/${data.length}`;
html = '';
if (currentPage > 1) {
html += `<button class="prev-btn">上一页</button>`;
}
if (totalPage <= 7) {
for (let i = 1; i <= totalPage; i++) {
html += `<span class="page-num ${i === currentPage ? 'active' : ''}">${i}</span>`;
}
} else {
if (currentPage < 5) {
for (let i = 1; i <= 5; i++) {
html += `<span class="page-num ${i === currentPage ? 'active' : ''}">${i}</span>`;
}
html += `<span class="ellipsis">...</span>`;
html += `<span class="page-num">${totalPage}</span>`;
} else if (currentPage >= 5 && currentPage < totalPage - 3) {
html += `<span class="page-num">1</span>`;
html += `<span class="ellipsis">...</span>`;
for (let i = currentPage - 2; i <= currentPage + 2; i++) {
html += `<span class="page-num ${i === currentPage ? 'active' : ''}">${i}</span>`;
}
html += `<span class="ellipsis">...</span>`;
html += `<span class="page-num">${totalPage}</span>`;
} else {
html += `<span class="page-num">1</span>`;
html += `<span class="ellipsis">...</span>`;
for (let i = totalPage - 4; i <= totalPage; i++) {
html += `<span class="page-num ${i === currentPage ? 'active' : ''}">${i}</span>`;
}
}
}
if (currentPage < totalPage) {
html += `<button class="next-btn">下一页</button>`;
}
pagination.innerHTML = html;
}
// 初始化页面
function init() {
renderTable(data.slice(0, pageSize));
renderPageNum(data);
}
// 绑定事件
prevBtn.addEventListener('click', function() {
currentPage--;
renderTable(data.slice((currentPage - 1) * pageSize, currentPage * pageSize));
renderPageNum(data);
});
nextBtn.addEventListener('click', function() {
currentPage++;
renderTable(data.slice((currentPage - 1) * pageSize, currentPage * pageSize));
renderPageNum(data);
});
firstBtn.addEventListener('click', function() {
currentPage = 1;
renderTable(data.slice(0, pageSize));
renderPageNum(data);
});
lastBtn.addEventListener('click', function() {
currentPage = Math.ceil(data.length / pageSize);
renderTable(data.slice((currentPage - 1) * pageSize));
renderPageNum(data);
});
addBtn.addEventListener('click', function() {
// 跳转到添加页面
window.location.href = './add.html';
});
// 初始化页面
init();
```
这样我们就完成了一个简单的展示水果库存的页面,包括分离的html、css和js,以及分页和添加按钮的功能。接下来,我们继续实现添加、修改和删除功能。
阅读全文