jQuery价格滑块过滤器 [英] jQuery price slider filter

查看:59
本文介绍了jQuery价格滑块过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了我的jquery价格滑块,但是我不确定如何过滤结果,以便在您滑动时只能看到该值在范围内的产品.

HTML:

<div class="demo">

<p>
    <label for="amount">Price range:</label>
    <input type="text" id="amount" style="border:0; color:#f6931f; font-weight:bold;" />
</p>

<div id="slider-range"></div>
<ul>
        <li> product - £10 </li>
        <li> product - £50 </li>
        <li> product - £100 </li>
        <li> product - £150 </li>
        <li> product - £200 </li>
    </ul>
</div>​

JavaScript:

$(function() {
    var options = 
        {
            range: true,
            min: 0,
            max: 500,
            values: [ 50, 300 ],
            slide: function( event, ui ) {
                $( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
            }
        };

        $( "#slider-range" ).slider(
            options
        );
        $( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
            " - $" + $( "#slider-range" ).slider( "values", 1 ) );
    });

我的小提琴是 http://jsfiddle.net/ktcle/uvJ8F/

解决方案

我会做一些更改:

  1. 为您的产品列表提供id属性,并为每个产品提供与商品价格相同的data-price属性:

    <ul id="products">
        <li data-price="10"> product - £10 </li>
        <li data-price="50"> product - £50 </li>
        <li data-price="100"> product - £100 </li>
        <li data-price="150"> product - £150 </li>
        <li data-price="200"> product - £200 </li>
    </ul>
    

  2. 添加一个函数,该函数将在发生slide事件时显示或隐藏那些产品:

    function showProducts(minPrice, maxPrice) {
        $("#products li").hide().filter(function() {
            var price = parseInt($(this).data("price"), 10);
            return price >= minPrice && price <= maxPrice;
        }).show();
    }
    

  3. slide事件处理程序中调用该函数:

    slide: function(event, ui) {
        var min = ui.values[0],
            max = ui.values[1];
    
        $("#amount").val("$" + min + " - $" + max);
        showProducts(min, max);
    }
    

  4. 还应在初始化滑块后立即调用该函数,以便最初隐藏正确的产品:

    var options = {
       /* snip */
    }, min, max;
    
    $("#slider-range").slider(options);
    
    min = $("#slider-range").slider("values", 0);
    max = $("#slider-range").slider("values", 1);
    
    $("#amount").val("$" + min + " - $" + max);
    
    showProducts(min, max);
    

整个代码段:

function showProducts(minPrice, maxPrice) {
    $("#products li").hide().filter(function() {
        var price = parseInt($(this).data("price"), 10);
        return price >= minPrice && price <= maxPrice;
    }).show();
}

$(function() {
    var options = {
        range: true,
        min: 0,
        max: 500,
        values: [50, 300],
        slide: function(event, ui) {
            var min = ui.values[0],
                max = ui.values[1];

            $("#amount").val("$" + min + " - $" + max);
            showProducts(min, max);
        }
    }, min, max;

    $("#slider-range").slider(options);

    min = $("#slider-range").slider("values", 0);
    max = $("#slider-range").slider("values", 1);

    $("#amount").val("$" + min + " - $" + max);

    showProducts(min, max);
});​

示例: http://jsfiddle.net/5aPg7/

I have created my jquery price slider but I am not sure how to filter my results so that as you slide you only see the products with that value in range.

HTML:

<div class="demo">

<p>
    <label for="amount">Price range:</label>
    <input type="text" id="amount" style="border:0; color:#f6931f; font-weight:bold;" />
</p>

<div id="slider-range"></div>
<ul>
        <li> product - £10 </li>
        <li> product - £50 </li>
        <li> product - £100 </li>
        <li> product - £150 </li>
        <li> product - £200 </li>
    </ul>
</div>​

JavaScript:

$(function() {
    var options = 
        {
            range: true,
            min: 0,
            max: 500,
            values: [ 50, 300 ],
            slide: function( event, ui ) {
                $( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
            }
        };

        $( "#slider-range" ).slider(
            options
        );
        $( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
            " - $" + $( "#slider-range" ).slider( "values", 1 ) );
    });

​ My fiddle is http://jsfiddle.net/ktcle/uvJ8F/

解决方案

I would make a few changes:

  1. Give your list of products an id attribute and give each product a data-price attribute that is equal to the price of the item:

    <ul id="products">
        <li data-price="10"> product - £10 </li>
        <li data-price="50"> product - £50 </li>
        <li data-price="100"> product - £100 </li>
        <li data-price="150"> product - £150 </li>
        <li data-price="200"> product - £200 </li>
    </ul>
    

  2. Add a function that will show or hide those products when a slide event occurs:

    function showProducts(minPrice, maxPrice) {
        $("#products li").hide().filter(function() {
            var price = parseInt($(this).data("price"), 10);
            return price >= minPrice && price <= maxPrice;
        }).show();
    }
    

  3. Call that function from the slide event handler:

    slide: function(event, ui) {
        var min = ui.values[0],
            max = ui.values[1];
    
        $("#amount").val("$" + min + " - $" + max);
        showProducts(min, max);
    }
    

  4. Also call that function right after the slider is initialized so that the correct products are hidden initially:

    var options = {
       /* snip */
    }, min, max;
    
    $("#slider-range").slider(options);
    
    min = $("#slider-range").slider("values", 0);
    max = $("#slider-range").slider("values", 1);
    
    $("#amount").val("$" + min + " - $" + max);
    
    showProducts(min, max);
    

The entire snippet:

function showProducts(minPrice, maxPrice) {
    $("#products li").hide().filter(function() {
        var price = parseInt($(this).data("price"), 10);
        return price >= minPrice && price <= maxPrice;
    }).show();
}

$(function() {
    var options = {
        range: true,
        min: 0,
        max: 500,
        values: [50, 300],
        slide: function(event, ui) {
            var min = ui.values[0],
                max = ui.values[1];

            $("#amount").val("$" + min + " - $" + max);
            showProducts(min, max);
        }
    }, min, max;

    $("#slider-range").slider(options);

    min = $("#slider-range").slider("values", 0);
    max = $("#slider-range").slider("values", 1);

    $("#amount").val("$" + min + " - $" + max);

    showProducts(min, max);
});​

Example: http://jsfiddle.net/5aPg7/

这篇关于jQuery价格滑块过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆