当前位置:网站首页>ES6 advanced - query commodity cases
ES6 advanced - query commodity cases
2022-07-26 15:45:00 【setTimeout()】
First, let's look at the layout :
Functions that need to be realized to query commodity cases :
1. Render the data to the page
2. Display data according to price
3. Display data according to product name
Function one : Dynamically render data to the page
// obtain tbody
var tb = document.querySelector('tbody');
// Render the data to the page
datas(data)
function datas(data) {
// Empty tbody What's in it
tb.innerHTML = '';
data.forEach(function(value) {
// Create a line
var tr = document.createElement('tr');
// Create three columns
tr.innerHTML = '<td>' + value.id + '</td><td>' + value.pname + '</td><td>' + value.price + '</td>';
// Append this line to tbody Inside
tb.appendChild(tr);
})
}Function 2 : Query goods by price
// Query goods by price
// When we hit the button , We can screen the objects according to our commodity prices
// Get elements
var star = document.querySelector('.start');
var end = document.querySelector('.end');
var searchbtn = document.querySelector('.search-price');
searchbtn.addEventListener('click', function() {
var starval = star.value;
var endval = end.value;
console.log(starval);
console.log(endval);
var newdata = data.filter(function(value) {
return value.price >= starval && value.price <= endval
})
// Render new data to the page
datas(newdata)
})Function three : Query the commodity according to the commodity name
// Display data according to product name
// If the only element in the query data , use some The method is appropriate , Because she found this element, she stopped cycling , More efficient
// Get elements
var pro = document.querySelector('.product');
var probtn = document.querySelector('.search-pro');
probtn.addEventListener('click', function() {
var arr = [];
data.some(function(value) {
if (value.pname === pro.value) {
// datas(value)
arr.push(value);
return true; // Must write true
}
})
// Render to page
datas(arr)
}) The complete code is as follows :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
table {
width: 400px;
border: 1px solid #000;
border-collapse: collapse;
margin: 0 auto;
}
td,
th {
border: 1px solid #000;
text-align: center;
}
input {
width: 50px;
}
.search {
width: 600px;
margin: 20px auto;
}
</style>
</head>
<body>
<div class="search">
Inquire according to the price : <input type="text" class="start"> - <input type="text" class="end"> <button class="search-price"> Search for </button> Query by commodity name : <input type="text" class="product"> <button class="search-pro"> Inquire about </button>
</div>
<table>
<thead>
<tr>
<th>id</th>
<th> The product name </th>
<th> Price </th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
// Use the new array method to manipulate data
var data = [{
id: 1,
pname: ' millet ',
price: 3999
}, {
id: 2,
pname: 'oppo',
price: 999
}, {
id: 3,
pname: ' glory ',
price: 1299
}, {
id: 4,
pname: ' Huawei ',
price: 1999
}, ]
// obtain tbody
var tb = document.querySelector('tbody');
// Render the data to the page
datas(data)
function datas(data) {
// Empty tbody What's in it
tb.innerHTML = '';
data.forEach(function(value) {
// Create a line
var tr = document.createElement('tr');
// Create three columns
tr.innerHTML = '<td>' + value.id + '</td><td>' + value.pname + '</td><td>' + value.price + '</td>';
// Append this line to tbody Inside
tb.appendChild(tr);
})
}
// Query goods by price
// When we hit the button , We can screen the objects according to our commodity prices
// Get elements
var star = document.querySelector('.start');
var end = document.querySelector('.end');
var searchbtn = document.querySelector('.search-price');
searchbtn.addEventListener('click', function() {
var starval = star.value;
var endval = end.value;
console.log(starval);
console.log(endval);
var newdata = data.filter(function(value) {
return value.price >= starval && value.price <= endval
})
// Render new data to the page
datas(newdata)
})
// Display data according to product name
// If the only element in the query data , use some The method is appropriate , Because she found this element, she stopped cycling , More efficient
// Get elements
var pro = document.querySelector('.product');
var probtn = document.querySelector('.search-pro');
probtn.addEventListener('click', function() {
var arr = [];
data.some(function(value) {
if (value.pname === pro.value) {
// datas(value)
arr.push(value);
return true; // Must write true
}
})
// Render to page
datas(arr)
})
</script>
</body>
</html>边栏推荐
- 单例模式
- 2023 catering industry exhibition, China catering supply chain exhibition and Jiangxi catering Ingredients Exhibition were held in February
- 桌面应用布局图
- 认识JS基础与浏览器引擎
- 【EXPDP导出数据】expdp导出23行记录,且不包含lob字段的表,居然用时48分钟,请大家帮忙看看
- 中金财富证券安全吗 开户要多久
- R language ggplot2 visualization: visual line graph, visual line graph for different groups using the group parameter in AES function
- [leetcode daily question] - 268. Missing numbers
- 组件化开发基本规范、localStorage 和 sessionStorage、对象数据转基本值、原型链使用
- Can the parameterized view get SQL with different rows according to the characteristics of the incoming parameters? For example, here I want to use the column in the transmission parameter @field
猜你喜欢

白话详解决策树模型之使用信息熵构建决策树

数智转型,管理先行|JNPF全力打造“全生命周期管理”平台

kalibr标定realsenseD435i --多相机标定

Digital warehouse: iqiyi digital warehouse platform construction practice

81.(cesium之家)cesium修改灰色背景(默认蓝色)

C # set different text watermarks for each page of word

TI C6000 TMS320C6678 DSP+ Zynq-7045的PS + PL异构多核案例开发手册(3)

Digital commerce cloud: lead the digital upgrading of chemical industry and see how Mobei can quickly open up the whole scene of mutual integration and interoperability

Deep packet inspection using cuckoo filter paper summary

【EXPDP导出数据】expdp导出23行记录,且不包含lob字段的表,居然用时48分钟,请大家帮忙看看
随机推荐
Reflection, enumeration, and lambda expressions
R language ggplot2 visualization: visual line graph, visual line graph for different groups using the group parameter in AES function
promise,async-await 和 跨域问题的解决--代理服务器的原理
教程篇(7.0) 05. 通过FortiClient EMS发放FortiClient * FortiClient EMS * Fortinet 网络安全专家 NSE 5
阿里巴巴一面 :十道经典面试题解析
OSPF综合实验
Tutorial (7.0) 05. Issue forticlient * forticlient EMS * Fortinet network security expert NSE 5 through forticlient EMS
Data warehouse: Data Modeling and log system in data warehouse construction
深度学习中图像增强技术的综合综述
03 common set security classes under JUC
OSPF comprehensive experiment
FOC学习笔记-坐标变换以及仿真验证
[leetcode daily question] - 268. Missing numbers
Data preprocessing of data mining
777. Exchange adjacent characters in LR string
The R language uses the histogram function in the lattice package to visualize the histogram (histogram plot), the col parameter to customize the fill color, and the type parameter to customize the hi
Digital warehouse: iqiyi digital warehouse platform construction practice
中金财富证券安全吗 开户要多久
API 版本控制【 Eolink 翻译】
Can't you see the withdrawal? Three steps to prevent withdrawal on wechat.