当前位置:网站首页>查询商品案例-页面渲染数据
查询商品案例-页面渲染数据
2022-07-03 01:17:00 【Blizzard前端】
<!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中 -->
</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 }, ]; // 1. 获取相应的元素 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. 把数据渲染到页面中 function setDate(mydata) {
// 先清空原来tbody 里面的数据 tbody.innerHTML = ''; mydata.forEach(function(value) {
// console.log(value); var tr = document.createElement('tr'); //先为每个数组元素创建一个行,在把这个行追加到tbody当中去 tr.innerHTML = '<td>' + value.id + '</td><td>' + value.pname + '</td><td>' + value.price + '</td>'; tbody.appendChild(tr); }); } // 3. 根据价格查询商品 // 当我们点击了按钮,就可以根据我们的商品价格去筛选数组里面的对象 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); // 把筛选完之后的对象渲染到页面中 setDate(newDate); }); // 4. 根据商品名称查找商品 // 如果查询数组中唯一的元素, 用some方法更合适,因为它找到这个元素,就不在进行循环,效率更高] 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 后面必须写true } }); // 把拿到的数据渲染到页面中 setDate(arr); }) </script>
</body>
</html>



some和forEach区别
<script>
var arr = ['red', 'green', 'blue', 'pink'];
// 1. forEach迭代 遍历
console.log('forEach:');
arr.forEach(function(value) {
if (value == 'green') {
console.log('找到了该元素');
return true; // 在forEach 里面 return 不会终止迭代
}
console.log(11);
})
// 如果查询数组中唯一的元素, 用some方法更合适,
console.log('some:');
arr.some(function(value) {
if (value == 'green') {
console.log('找到了该元素');
return true; // 在some 里面 遇到 return true 就是终止遍历 迭代效率更高
}
console.log(11);
});
// arr.filter(function(value) {
// if (value == 'green') {
// console.log('找到了该元素');
// return true; // // filter 里面 return 不会终止迭代
// }
// console.log(11);
// });
</script>

边栏推荐
- [data mining] task 2: mimic-iii data processing of medical database
- Is there anything in common between spot gold and spot silver
- [data mining] task 5: k-means/dbscan clustering: double square
- The meaning of wildcard, patsubst and notdir in makefile
- Give you an array numbers that may have duplicate element values. It was originally an array arranged in ascending order, and it was rotated once according to the above situation. Please return the sm
- NCTF 2018 part Title WP (1)
- 音程的知识的总结
- 【數據挖掘】任務6:DBSCAN聚類
- Mathematical knowledge: Nim game game theory
- PS去除水印详解
猜你喜欢

STM32 - introduction of external interrupts exti and NVIC
![[机缘参悟-36]:鬼谷子-飞箝篇 - 面对捧杀与诱饵的防范之道](/img/c6/9aee30cb935b203c7c62b12c822085.jpg)
[机缘参悟-36]:鬼谷子-飞箝篇 - 面对捧杀与诱饵的防范之道

【QT】自定义控件的封装

传输层 TCP主要特点和TCP连接

【面试题】1369- 什么时候不能使用箭头函数?

STM32 - Application of external interrupt induction lamp

View of MySQL

C application interface development foundation - form control (2) - MDI form

Three core issues of concurrent programming - "deep understanding of high concurrent programming"

C application interface development foundation - form control (3) - file control
随机推荐
Why can't the start method be called repeatedly? But the run method can?
Druid database connection pool
网络安全-浅谈安全威胁
Related concepts of GDB in embedded system
Scheme and practice of cold and hot separation of massive data
[principles of multithreading and high concurrency: 2. Solutions to cache consistency]
Mathematical knowledge: divisible number inclusion exclusion principle
网络安全-钓鱼
Telecom Customer Churn Prediction challenge
音程的知识的总结
Work experience of a hard pressed programmer
Function definition and call, this, strict mode, higher-order function, closure, recursion
Wordinsert formula /endnote
openresty 缓存
[shutter] animation animation (animatedbuilder animation use process | create animation controller | create animation | create components for animation | associate animation with components | animatio
Virtual list
[my advanced journey of OpenGL learning] collation of Euler angle, rotation order, rotation matrix, quaternion and other knowledge
小程序开发的部分功能
数学知识:能被整除的数—容斥原理
Give you an array numbers that may have duplicate element values. It was originally an array arranged in ascending order, and it was rotated once according to the above situation. Please return the sm