当前位置:网站首页>Query product cases - page rendering data
Query product cases - page rendering data
2022-07-03 01:50:00 【Blizzard front end】
<!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>
<!-- Data rendering to tbody in -->
</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 }, ]; // 1. Get the corresponding element var tbody = document.querySelector('tbody'); var search_price = document.querySelector('.search-price'); var start = document.querySelector('.start'); var end = document.querySelector('.end'); var product = document.querySelector('.product'); var search_pro = document.querySelector('.search-pro'); setDate(data); // 2. Render the data to the page function setDate(mydata) {
// First empty the original tbody The data in it tbody.innerHTML = ''; mydata.forEach(function(value) {
// console.log(value); var tr = document.createElement('tr'); // First create a row for each array element , Append this line to tbody Go to of tr.innerHTML = '<td>' + value.id + '</td><td>' + value.pname + '</td><td>' + value.price + '</td>'; tbody.appendChild(tr); }); } // 3. Query goods by price // When we hit the button , We can filter the objects in the array according to our commodity price search_price.addEventListener('click', function() {
// alert(11); var newDate = data.filter(function(value) {
return value.price >= start.value && value.price <= end.value; }); console.log(newDate); // Render the filtered objects to the page setDate(newDate); }); // 4. Find products by product name // If you query the only element in the array , use some More appropriate , Because it finds this element , It's not a cycle , More efficient ] search_pro.addEventListener('click', function() {
var arr = []; data.some(function(value) {
if (value.pname === product.value) {
// console.log(value); arr.push(value); return true; // return It must be written after true } }); // Render the data to the page setDate(arr); }) </script>
</body>
</html>
some and forEach difference
<script>
var arr = ['red', 'green', 'blue', 'pink'];
// 1. forEach iteration Traverse
console.log('forEach:');
arr.forEach(function(value) {
if (value == 'green') {
console.log(' Found the element ');
return true; // stay forEach Inside return The iteration will not be terminated
}
console.log(11);
})
// If you query the only element in the array , use some More appropriate ,
console.log('some:');
arr.some(function(value) {
if (value == 'green') {
console.log(' Found the element ');
return true; // stay some Inside encounter return true Is to stop traversal Iteration is more efficient
}
console.log(11);
});
// arr.filter(function(value) {
// if (value == 'green') {
// console.log(' Found the element ');
// return true; // // filter Inside return The iteration will not be terminated
// }
// console.log(11);
// });
</script>
边栏推荐
- 网络安全-病毒
- 串口抓包/截断工具的安装及使用详解
- [技术发展-23]:DSP在未来融合网络中的应用
- [shutter] animation animation (basic process of shutter animation | create animation controller | create animation | set value listener | set state listener | use animation values in layout | animatio
- Network security - Information Collection
- Uniapp component -uni notice bar notice bar
- Network security - phishing
- QTableWidget懒加载剩内存,不卡!
- How to refresh the opening amount of Oracle ERP
- 网络安全-动态路由协议RIP
猜你喜欢
Network security - vulnerabilities and Trojans
[机缘参悟-36]:鬼谷子-飞箝篇 - 面对捧杀与诱饵的防范之道
In the face of difficult SQL requirements, HQL is not afraid
leetcode刷题_两数之和 II - 输入有序数组
LeetCode 987. Vertical order transverse of a binary tree - Binary Tree Series Question 7
Why can't the start method be called repeatedly? But the run method can?
[data mining] task 3: decision tree classification
[QT] encapsulation of custom controls
Scheme and practice of cold and hot separation of massive data
Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
随机推荐
【数据挖掘】任务2:医学数据库MIMIC-III数据处理
网络安全-钓鱼
[data mining] task 6: DBSCAN clustering
STM32 - Application of external interrupt induction lamp
Pytest learning notes (12) -allure feature · @allure Step () and allure attach
Comment le chef de file gère - t - il l'équipe en cas d'épidémie? Contributions communautaires
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance o
Some functions of applet development
Why is it not recommended to use BeanUtils in production?
网络安全-漏洞与木马
串口抓包/截断工具的安装及使用详解
Vant implements a simple login registration module and a personal user center
Uniapp component -uni notice bar notice bar
Network security - Trojan horse
Network security - virus
函数的定义和调用、this、严格模式、高阶函数、闭包、递归
2022-02-15 reading the meta module inspiration of the influxdb cluster
[error record] navigator operation requested with a context that does not include a naviga
Summary of interval knowledge
疫情当头,作为Leader如何进行团队的管理?| 社区征文