商品查询筛选案例

<!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>
    </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');
                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>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个Vue商品输入查询案例: HTML代码: ```html <div id="app"> <input type="text" v-model="keyword" placeholder="请输入商品名称"> <button @click="search">搜索</button> <ul> <li v-for="(item, index) in itemList" :key="index">{{ item.name }} - ¥{{ item.price }}</li> </ul> </div> ``` Vue代码: ```javascript new Vue({ el: '#app', data: { keyword: '', itemList: [ { name: '苹果', price: 5.00 }, { name: '香蕉', price: 3.00 }, { name: '橙子', price: 4.00 }, { name: '鸭梨', price: 6.00 }, { name: '芒果', price: 8.00 }, { name: '柚子', price: 5.50 }, { name: '柠檬', price: 4.50 }, { name: '荔枝', price: 10.00 }, { name: '火龙果', price: 15.00 }, { name: '榴莲', price: 20.00 } ] }, methods: { search() { if (this.keyword.trim() !== '') { this.itemList = this.itemList.filter(item => item.name.indexOf(this.keyword) !== -1); } else { this.itemList = [ { name: '苹果', price: 5.00 }, { name: '香蕉', price: 3.00 }, { name: '橙子', price: 4.00 }, { name: '鸭梨', price: 6.00 }, { name: '芒果', price: 8.00 }, { name: '柚子', price: 5.50 }, { name: '柠檬', price: 4.50 }, { name: '荔枝', price: 10.00 }, { name: '火龙果', price: 15.00 }, { name: '榴莲', price: 20.00 } ]; } } } }); ``` 说明: 1. 输入框使用v-model双向绑定数据,实现输入查询功能。 2. 搜索按钮使用@click监听点击事件,当点击搜索按钮时调用search方法。 3. search方法根据输入的关键字筛选商品列表,并重新渲染页面。如果关键字为空,则显示所有商品

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值