* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.w {
width: 1200px;
margin: 0 auto;
}
.box .search {
width: 1000px;
height: 50px;
line-height: 50px;
margin: 0 auto;
background-color: purple;
}
.box .start,.end,.proname{
width: 200px;
height: 35px;
outline: none;
}
.box .pricesearch,.search_btn {
width: 80px;
height: 35px;
border: 0;
outline: none;
vertical-align: middle;
cursor: pointer;
}
.tablelist {
width: 1100px;
height: 400px;
margin: 0 auto;
padding: 10px 50px;
background-color: pink;
}
.tablelist table {
width: 100%;
border-spacing: 0;
border-collapse: collapse;
}
.tablelist table thead tr {
width: 100%;
height: 30px;
font-size: 16px;
border: 1px solid #ccc;
}
.tablelist table thead tr th {
border: 2px solid #ccc;
}
.tablelist table tbody {
width: 100%;
}
.tablelist table tbody tr {
height: 30px;
}
.tablelist table tbody tr td {
border: 2px solid #ccc;
height: 30px;
line-height: 30px;
text-align: center;
}
价格范围:
-
搜索
商品名称:
确定
id商品名称价格
var data = [
{id:1,
name:'小米手机',
price:1999
},
{id:2,
name:'小米手机',
price:3000
},
{id:3,
name:'小米手机',
price:600
},
{id:4,
name:'蓝牙耳机',
price:1699
},
]
var tbody = document.querySelector('tbody');
//遍历数组
data.forEach(function(value,index) {
//创建节点
var tr = '
' + value.id+ '' + value.name+ '' + value.price+ '';//追加到tbody里面
tbody.insertAdjacentHTML('beforeend',tr);
});
//搜索价格范围
var pricebtn = document.querySelector('.pricesearch');
var start = document.querySelector('.start');
var end = document.querySelector('.end');
pricebtn.addEventListener('click',function() {
//筛选符合条件的
var newArr = data.filter(function(value,index) {
return value.price >= start.value && value.price <= end.value;
})
//将原来的tbody置空
tbody.innerHTML = '';
//遍历结果
newArr.forEach(function(value,index) {
var tr = '
' + value.id + '' + value.name + '' + value.price + '';tbody.insertAdjacentHTML('beforeend',tr);
})
})
//根据商品名称查询
var search_btn = document.querySelector('.search_btn');
var proname = document.querySelector('.proname');
search_btn.addEventListener('click',function() {
var arr = []
data.some(function(value,index) {
if(value.name == proname.value) {
arr.push(value);
return true;
}
})
tbody.innerHTML = ''
//渲染页面
arr.forEach(function(value,index) {
var tr = '
' + value.id + '' + value.name + '' + value.price + '';tbody.insertAdjacentHTML('beforeend',tr);
})
})
一键复制
编辑
Web IDE
原始数据
按行查看
历史