1、提取公共方法common.js
function $(key){
if(key){
if(key.startsWith("#")){
key = key.substring(1)
return document.getElementById(key)
}else{
let nodeList = document.getElementsByName(key)
return Array.from(nodeList)
}
}
}
2、 给库存名称添加超链接
2.1、index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="style/index.css">
<script src="script/axios.min.js"></script>
<script src="script/index.js"></script>
<script src="script/common.js"></script>
</head>
<body>
<div id="div0">
<div id="div_title">
<p>欢迎使用水果库存系统</p>
</div>
<div id="div_fruit_table">
<table id="fruit_tbl">
<tr>
<th class="w25">名称</th>
<th class="w25">单价</th>
<th class="w25">库存</th>
<th>操作</th>
</tr>
<!--
<tr>
<td><a href='edit.html?fid=1'>苹果</a></td>
<td>5</td>
<td>100</td>
<td><img class="delImg" src="imgs/del.png" onclick='delFruit(1)'/></td>
</tr>
-->
</table>
</div>
</div>
</body>
</html>
2.2、index.js
window.onload=function(){
loadData();
}
loadData=function(){
axios({
method:'get',
url:'/index'
}).then(response=>{
debugger
let fruitList = response.data.data
// 此处使用的是axios,那么响应回来的数据自动就是json,
// 不需要再进行parse(如果是原始的ajax操作,那么一定需要parse)
// let fruitArr = JSON.parse(fruitList)
let fruitArr = fruitList
for(let i = 0 ; i<fruitArr.length; i++){
let fruitTbl = $("#fruit_tbl")
let tr = fruitTbl.insertRow()
let fnameTD = tr.insertCell()
let priceTD = tr.insertCell()
let fcountTD = tr.insertCell()
let operTD = tr.insertCell()
let fruit = fruitArr[i]
//fnameTD.innerText = fruit.fname
fnameTD.innerHTML = '<a href="edit.html?fid='+fruit.fid+'">'+fruit.fname+'</a>';
priceTD.innerText = fruit.price
fcountTD.innerText = fruit.fcount
operTD.innerHTML="<img class=\"delImg\" src=\"imgs/del.png\"/>"
}
})
}
3、创建编辑水果库存页面edit.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="style/index.css">
<script src="script/axios.min.js"></script>
<script src="script/edit.js"></script>
<script src="script/common.js"></script>
</head>
<body>
<div id="div0">
<div id="div_title">
<p>编辑水果库存信息</p>
</div>
<div id="div_fruit_table">
<input type="hidden" id="fid" value="0"/>
<table id="fruit_tbl">
<tr>
<th class="w25">名称:</th>
<td><input type="text" id="fname"/></td>
</tr>
<tr>
<th class="w25">单价:</th>
<td><input type="text" name="price" id="price"/> </td>
</tr>
<tr>
<th class="w25">库存:</th>
<td><input type="text" name="fcount" id="fcount"/> </td>
</tr>
<tr>
<th class="w25">备注:</th>
<td><input type="text" name="remark" id="remark"/> </td>
</tr>
<tr>
<th colspan="2">
<input type="button" value="修改" onclick="update()"/>
<input type="button" value="取消"/>
</th>
</tr>
</table>
</div>
</div>
</body>
</html>
4、获取URL参数中的queryString查询字符串edit.js
let queryString = window.location.search.substring(1)