当前位置:网站首页>Project exercise: the function of checking and modifying tables
Project exercise: the function of checking and modifying tables
2022-07-27 17:01:00 【sl105105】
We got a background data json The documents are as follows :
{
"data":[{
"id":1,
"price":38.00,
"goodsName":" Bear electric ",
"status":0,
"desc":" A small appliance that makes people love cooking "
},{
"id":2,
"price":58.01,
"goodsName":" Midea air-conditioning ",
"status":1,
"desc":" A life-saving straw that makes people fall in love with summer "
},{
"id":3,
"price":78.02,
"goodsName":" Gree air conditioning ",
"status":0,
"desc":" Gree air conditioner is very good, very awesome "
},{
"id":4,
"price":98.10,
"goodsName":" Filet steak ",
"status":1,
"desc":" The taste is super tender and delicious "
},{
"id":5,
"price":200.00,
"goodsName":" rolex ",
"status":1,
"desc":" I want to buy a real Rolex at this price ?"
}]
}Requirements are as follows :
first : We show in a table json The data in the file , And according to his status You can change the contents of our form .
the second : Click for details , Be able to make desc The content of is displayed on our page ( We can use our ID To call data ).
Third : Total required price , We can change the price according to our selection
Try to write , This exercise can try to use us ES6 To achieve , I'll post the implementation code below , If you can't think of it, you can also refer to , Very exercise the scenes we use in our work , In order to better simulate , Make a request to relatives , Be sure to update the latest data we get , How to get the latest data ? these , When typing code, you must consider .
The implementation code is as follows :
html Code section :
<body>
<select name="" id="" onchange="changeFn(this)">
<option value="2"> All goods </option>
<option value="1"> Shipped </option>
<option value="0"> Not delivered </option>
</select>
<table border="1" cellpadding="10">
<thead>
<tr>
<th> Name of commodity </th>
<th> commodity price </th>
<!-- If status yes 1 Show shipped 0 Represents undelivered -->
<th> Whether to ship the goods or not </th>
<th> operation </th>
</tr>
</thead>
<tbody>
<!-- <tr>
<td> Bear biscuits </td>
<td>100.00</td>
<td> Shipped </td>
<td><button> Check the details </button></td>
</tr> -->
</tbody>
</table>
<h1 id="allPrice"></h1>
<div id="tk" style="display: none;">
<!-- <h2> Name of commodity : Bear Bean Curd </h2>
<h2> commodity price :89.00</h2>
<h2> Whether to ship the goods or not : Shipped </h2>
<h2> Commodity Description : I'm a community security guard , I like bear dried beans , Hug the owner Xiao an , To safeguard the safety of one party !</h2> -->
</div>
js Part of the code , In order to exercise our ability to use native code, please review , So use native code to write :
<script>
/* Global variables */
/* Get table data */
getList();
function look(id) {
console.log(id)
getList("3",id);
}
function getList(s,id) {
var xhr = new XMLHttpRequest();
xhr.open("get", "./goods.json", true);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var res = JSON.parse(xhr.responseText).data;
console.log(res);
if (s == "0") {
let arr = res.filter(function (item) {
return item.status == 0
})
showPage(arr)
}else if (s == "1") {
let arr = res.filter(function (item) {
return item.status == 1
})
showPage(arr)
}else if(s=="3"){
let obj = res.find(function (v) {
return v.id == id;
})
console.log(obj);
document.querySelector("#tk").innerHTML = `
<h2> Name of commodity :${obj.goodsName}</h2>
<h2> commodity price :${obj.price.toFixed(2)}</h2>
<h2> Whether to ship the goods or not :${obj.status == 0 ? " Not delivered " : obj.status == 1 ? " Shipped " : " Items lost "}</h2>
<h2> Commodity Description :${obj.desc}</h2>
`;
document.querySelector("#tk").style.display = "block";
}
else{
showPage(res);
}
}
}
}
function showPage(res) {
var str = "";
res.forEach(function (item, index) {
str += `
<tr>
<td>${item.goodsName}</td>
<td>${item.price.toFixed(2)}</td>
<td>${item.status == 0 ? " Not delivered " : item.status == 1 ? " Shipped " : " Items lost "}</td>
<td><button onclick="look(${item.id})"> Check the details </button></td>
</tr>
`
})
document.querySelector("tbody").innerHTML = str;
var priceArr = res.map(function (v) {
return v.price
})
console.log(priceArr)
var sum = priceArr.reduce(function (a, b) {
console.log(a, b);
return a + b;
})
console.log(sum)
document.querySelector("#allPrice").innerHTML = " Total price :" + sum.toFixed(2);
}
function statusFn(s) {
if (s == 0) {
return " Not delivered "
} else if (s == 1) {
return " Shipped "
} else {
return " Items lost "
}
}
/* Selection status */
function changeFn(that) {
console.log(that.value);
switch (that.value) {
case "0":
getList("0");
break;
case "1":
getList("1");
break;
case "2":
getList("2");
break;
}
}
</script>
The above is a simple exercise , You learned. ??
How to improve your ability in code , There is no shortcut. , Only keep rolling inside , Practice more , come on. !!!
边栏推荐
- 合工大苍穹战队视觉组培训Day8——视觉,目标识别
- Process control statement
- Jerry's book can't find Bluetooth solutions [article]
- 合工大苍穹战队视觉组培训Day7——视觉,jetson naon与D435i
- Getting started with nvida CUDA dirverapi
- MPC_ ORCA
- 2021-05-30
- Niuke topic -- Realizing queues with two stacks, stacks containing min functions, and valid bracket sequences
- 数据采集之:巧用布隆过滤器提取数据摘要
- JDBC程序实现完整步骤
猜你喜欢

Apache

C语言之分支循环语句

为媒体资产构建一个云原生的文件系统

C语言之动态内存分配

D3.js create a cool arc
![[paper reading] single- and cross modality near duplicate image pairsdetection via spatial transformer compare](/img/33/8af12d58f4afeb807ebf9a90a2ea47.png)
[paper reading] single- and cross modality near duplicate image pairsdetection via spatial transformer compare
![[paper reading] transformer with transfer CNN for remote sensing imageobject detection](/img/a2/8ee85e81133326afd86648d9594216.png)
[paper reading] transformer with transfer CNN for remote sensing imageobject detection

UML diagram introduction

选 择 结 构

What is JSP?
随机推荐
Flex弹性盒布局2
Servlet Chinese garbled setcontenttype setting is invalid
C语言之文件操作
Three level cache of pictures in Android
md 中超链接的解析问题:解析`this.$set()`,`$`前要加空格或转义符 `\`
LOJ 576 - "libreoj noi round 2" sign in game [line segment tree]
Dynamic memory allocation in C language
Natural sorting: comparable interface, customized sorting: the difference between comparator interface
Gurobi——GRBLinExpr
day07 作业
高精度定时器
Bean: the difference between model and entity
JSON data parsing
Niuke topic -- binary search tree and bidirectional linked list
牛客题目——二叉搜索树与双向链表
C语言之数组
Cvxpy - latest issue
CDQ divide and conquer and whole dichotomy learning notes
Interpretation of C basic syntax: summarize some commonly used but easily confused functions (i++ and ++i) in the program (bit field)
2021-06-18 SSM项目中自动装配错误