js 상품 사례 조회 실현
<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-prro"> </button>
</div>
<table>
<thead>
<tr>
<th>id</th>
<th> </th>
<th> </th>
</tr>
</thead>
<tbody>
<!-- <tr>
<td>1</td>
<td> </td>
<td>2000</td>
</tr>
<tr>
<td>2</td>
<td>oppo</td>
<td>999</td>
</tr>
<tr>
<td>3</td>
<td> </td>
<td>1299</td>
</tr>
<tr>
<td>4</td>
<td> </td>
<td>1999</td>
</tr> -->
</tbody>
</table>
css:
*{
margin: 0;
padding: 0;
}
body{
width: 1000px;
margin: 0 auto;
}
.search{
text-align: center;
/* margin: 0 auto; */
}
table{
padding-top: 20px;
width: 1000px;
height: 100px;
/* border: 1px solid #ccc; */
margin:0 auto ;
}
th,tr,td{
border: 1px solid #ccc;
text-align: center;
height: 50px;
}
js:
<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');
//2.
//forEach()。 , , ,
// , ,
setDate(data);
// ,
function setDate(mydata){
// , tbody
tbody.innerHTML = '';
mydata.forEach(function(value){
//
// console.log(value);
// tbody , tbody tr
var tr = document.createElement('tr');
// value, :value.id
tr.innerHTML = '<td>'+value.id +'</td> <td>'+value.pname +'</td> <td>'+value.price +'</td>';
tbody.appendChild(tr);
});
}
//3.
//filter()
//
var search_price = document.querySelector('.search-price');
var start = document.querySelector('.start');
var end = document.querySelector('.end');
search_price.addEventListener('click',function(){
// alert(1) //
//
var newDate = data.filter(function(value){
// console.log(value);
// : <= <=
return value.price >= start.value && value.price <= end.value;
});
//
// console.log(newDate);//
//
//
setDate(newDate);
})
//4. , filter() ,
// some , some , , some
//
var product = document.querySelector('.product');//
var search_pro = document.querySelector('.search-prro'); //
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); // value.price value arr
})
</script>
구현 효과:왜 마지막 으로 some 를 이용 하여 조회 해 야 합 니까?
배열 의 some 방법 을 이용 하여 검색 을 옮 겨 다 니 기 때문에 값 결과 가 true 이면 프로그램 을 끝 낼 수 있 습 니 다.너무 많은 사건 을 낭비 하지 않 아 도 되 고 업무 효율 을 크게 강화 할 수 있 습 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[2022.04.19] 자바스크립트 this - 생성자 함수와 이벤트리스너에서의 this18일에 this에 대해 공부하면서 적었던 일반적인 함수나 객체에서의 this가 아닌 오늘은 이벤트리스너와 생성자 함수 안에서의 this를 살펴보기로 했다. new 키워드를 붙여 함수를 생성자로 사용할 때 this는...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.