JS高级table栏的简单检索

样式的填充

​
 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;
    }

​

页面的骨架

<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>

逻辑功能的实现

 //数据集定义
        const arr  = [
            {
                id:1,
                name:'小米',
                price:888
            },
            {
                id:2,
                name:'苹果',
                price:8000
            },
            {
                id:3,
                name:'大米',
                price:8848
            },
            {
                id:4,
                name:'小麦',
                price:1688
            },
        ];
       
        function setData(mydata){
            const tbody  =document.querySelector('tbody')
            tbody.innerHTML = ''
            mydata.forEach(
                i => {
                let tr = `<td>${i.id}</td><td>${i.name}</td><td>${i.price}</td>`
                tbody.insertAdjacentHTML('beforeend',tr)
            });
        }
        let that
        class Search{
            "use strict";
            constructor(search){
                that = this
                this.search = document.querySelector(search)
                this.start = this.search.querySelector('.start')//开始价格范围
                this.end = this.search.querySelector('.end')//结束价格范围
                this.search_price = this.search.querySelector('.search-price')//价格检索按钮
                this.search_pro = this.search.querySelector('.search-pro')//名字检索按钮
                this.ptoduct = this.search.querySelector('.product')
                this.init()//主函数
            }
            init(){
                this.search_price.addEventListener('click',this.searchPrice)
                this.search_pro.addEventListener('click',this.searchName)
                setData(arr)//视图渲染
            }
            // 价格检索
            searchPrice(){
                let newData = arr.filter(
                    i=>{
                        return i.price >= that.start.value&&i.price< that.end.value
                        //不建议将return后面的判断语句分开
                    })
                    console.log(newData);
                    setData(newData)
            }
            //名字检索
            searchName(){
                let data = [];
                // console.log(that.ptoduct.value)
                arr.some(i=>{
                    if(i.name === that.ptoduct.value){
                        data.push(i)
                        return true
                    }
                })
                setData(data)
            }
        }
        new Search(`.search`)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码到无能为力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值