当前位置:网站首页>ES6高级-查询商品案例
ES6高级-查询商品案例
2022-07-26 15:25:00 【setTimeout()】
首先来看布局:
查询商品案例需要实现的功能:
1.把数据渲染到页面中
2.根据价格显示数据
3.根据商品名称显示数据
功能一:动态渲染数据到页面
// 获取tbody
var tb = document.querySelector('tbody');
// 把数据渲染到页面中
datas(data)
function datas(data) {
// 清空tbody里面的内容
tb.innerHTML = '';
data.forEach(function(value) {
// 创建一个行
var tr = document.createElement('tr');
// 创建三个列
tr.innerHTML = '<td>' + value.id + '</td><td>' + value.pname + '</td><td>' + value.price + '</td>';
// 把这个行追加到tbody里面
tb.appendChild(tr);
})
}功能二:根据价格查询商品
// 根据价格查询商品
// 当我们点击了按钮,就可以根据我们的商品价格去筛选里面的对象
// 获取元素
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
})
// 把新的数据渲染到页面
datas(newdata)
})功能三:根据商品名称查询商品
// 根据商品名称显示数据
// 如果查询数据中唯一的元素,用some方法合适,因为她找到这个元素就不再进行循环,效率更高
// 获取元素
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; //必须写true
}
})
// 渲染到页面
datas(arr)
})完整代码如下:
<!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">
按照价格查询: <input type="text" class="start"> - <input type="text" class="end"> <button class="search-price">搜索</button> 按照商品名称查询: <input type="text" class="product"> <button class="search-pro">查询</button>
</div>
<table>
<thead>
<tr>
<th>id</th>
<th>产品名称</th>
<th>价格</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
// 利用新增数组方法操作数据
var data = [{
id: 1,
pname: '小米',
price: 3999
}, {
id: 2,
pname: 'oppo',
price: 999
}, {
id: 3,
pname: '荣耀',
price: 1299
}, {
id: 4,
pname: '华为',
price: 1999
}, ]
// 获取tbody
var tb = document.querySelector('tbody');
// 把数据渲染到页面中
datas(data)
function datas(data) {
// 清空tbody里面的内容
tb.innerHTML = '';
data.forEach(function(value) {
// 创建一个行
var tr = document.createElement('tr');
// 创建三个列
tr.innerHTML = '<td>' + value.id + '</td><td>' + value.pname + '</td><td>' + value.price + '</td>';
// 把这个行追加到tbody里面
tb.appendChild(tr);
})
}
// 根据价格查询商品
// 当我们点击了按钮,就可以根据我们的商品价格去筛选里面的对象
// 获取元素
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
})
// 把新的数据渲染到页面
datas(newdata)
})
// 根据商品名称显示数据
// 如果查询数据中唯一的元素,用some方法合适,因为她找到这个元素就不再进行循环,效率更高
// 获取元素
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; //必须写true
}
})
// 渲染到页面
datas(arr)
})
</script>
</body>
</html>边栏推荐
- Complete MySQL commands
- Familiarize you with the "phone book" of cloud network: DNS
- R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize dot plot, set the add parameter to add the mean and standard deviation vertical lines, and set the error.plo
- Bluetooth ble4.0-hm-10 device pairing Guide
- 数据中台、BI业务访谈(四)—— 十个问题看本质
- 使用verdaccio搭建自己的npm私有库
- 2023 catering industry exhibition, China catering supply chain exhibition and Jiangxi catering Ingredients Exhibition were held in February
- Quanzhi a40i industrial core board, 100% domestic 4-core arm cortex-a7, supports "dual screen abnormal display" [display interface capability, preferred scheme for industrial HMI]
- DICOM learning materials collection
- 81. (cesium home) cesium modifies the gray background (default blue)
猜你喜欢

Quanzhi a40i industrial core board, 100% domestic 4-core arm cortex-a7, supports "dual screen abnormal display" [display interface capability, preferred scheme for industrial HMI]

Enterprise digital transformation needs in-depth research, and it cannot be transformed for the sake of transformation

cs224w(图机器学习)2021冬季课程学习笔记5

什么是传输层协议TCP/UDP???

筑牢生态安全防线,广州开展突发环境事件应急演练

教程篇(7.0) 05. 通过FortiClient EMS发放FortiClient * FortiClient EMS * Fortinet 网络安全专家 NSE 5

Chapter 08_ Principles of index creation and design

What is the transport layer protocol tcp/udp???
![[leetcode daily question] - 121. The best time to buy and sell stocks](/img/51/ae7c4d903a51d97b70d5e69c6fffaa.png)
[leetcode daily question] - 121. The best time to buy and sell stocks

阿里巴巴一面 :十道经典面试题解析
随机推荐
About the selection of industrial control gateway IOT serial port to WiFi module and serial port to network port module
Basic specification of component development, localstorage and sessionstorage, object data to basic value, prototype chain use
Sqldeveloper tools quick start
什么是虚拟摄像头
请问参数化视图可以根据传入参数的特点得到不同行数的SQL吗?比如这里我想根据传输参数@field中列
Tool skill learning (I): pre skills -makfile, make,.Mk
PS + PL heterogeneous multicore case development manual for Ti C6000 tms320c6678 DSP + zynq-7045 (2)
Detailed explanation of nat/napt address translation (internal and external network communication) technology [Huawei ENSP]
sklearn clustering聚类
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
企业数字化转型需要深入研究,不能为了转型而转型
工具技能学习(一):前置技能-makfile、make、.mk
【LeetCode】33、 搜索旋转排序数组
原来卡布奇诺信息安全协会是干这个的呀,一起来看看吧。
Cs224w (Figure machine learning) 2021 winter course learning notes 5
持续集成(二)Jenkins基本使用介绍
Is there any need for livedata to learn—— Jetpack series (2)
04 callable and common auxiliary classes
R language ggplot2 visualization: use the ggballoonplot function of ggpubr package to visualize the balloon graph (visualize the contingency table composed of two classification variables), and config
单例模式