数据表和jquery范围滑块来过滤数据



我使用的是jquery数据表和jqueryui范围滑块。

Datatables内置了一个范围过滤器,但使用输入框来输入您的范围。我想用滑块代替。请有人帮我修改下面的代码,使用滑块。

当前代码为:

<script type="text/javascript" charset="utf-8"> 
/* Custom filtering function which will filter data in column four between two values */
$.fn.dataTableExt.afnFiltering.push(
function( oSettings, aData, iDataIndex ) {
var iMin = document.getElementById('min').value * 1;
var iMax = document.getElementById('max').value * 1;
var iVersion = aData[3] == "-" ? 0 : aData[3]*1;
if ( iMin == "" && iMax == "" )
{
return true;
}
else if ( iMin == "" && iVersion <= iMax )
{
return true;
}
else if ( iMin <= iVersion && "" == iMax )
{
return true;
}
else if ( iMin <= iVersion && iVersion <= iMax )
{
return true;
}
return false;
}
);
$(document).ready(function() {
    /* Initialise datatables */
    var oTable = $('#example').dataTable();
    /* Add event listeners to the two range filtering inputs */
        $('#min').keyup( function() { oTable.fnDraw(); } );
        $('#max').keyup( function() { oTable.fnDraw(); } );
} );
        </script> 
        <script type="text/javascript">
$(function() {
    $slider = $("#slider");//Caching slider object
    $amount = $("#amount");//Caching amount object
    $slider.slider({
        range: true, // necessary for creating a range slider
        min: 0, // minimum range of slider
        max: 50, //maximimum range of slider
        values: [0, 50], //initial range of slider
        step: 0.2,
        slide: function(event, ui) { // This event is triggered on every mouse move during slide.
            $amount.html('$' + ui.values[0] + ' - $' + ui.values[1]);//set value of  amount span to current slider values
        },
        stop: function(event, ui){//This event is triggered when the user stops sliding.
        //alert($slider.slider("values", 0));

        }
    });
    $amount.html('$' + $slider.slider("values", 0) + ' - $' + $slider.slider("values", 1));
});
</script>

我无法在这里显示易读的代码,所以我也将其添加到了jsfiddle中:

http://jsfiddle.net/ny32j/

感谢

首先,您的范围过滤器必须将滑块的值设置为iMin e iMax,所以我认为您应该这样做(确保您在正确的列上过滤数据:在本例中,aData[3]在4列上过滤):

$.fn.dataTableExt.afnFiltering.push(
function( oSettings, aData, iDataIndex ) {
//min value
var iMin = $slider.slider("values", 0)
//max value
var iMax = $slider.slider("values", 1)
var iVersion = aData[3] == "-" ? 0 : aData[3]*1;
if ( iMin == "" && iMax == "" )
{
return true;
}
else if ( iMin == "" && iVersion <= iMax )
{
return true;
}
else if ( iMin <= iVersion && "" == iMax )
{
return true;
}
else if ( iMin <= iVersion && iVersion <= iMax )
{
return true;
}
return false;
}
);

当滑块停止时,您必须重新绘制表格:

    stop: function(event, ui){//This event is triggered when the user stops sliding.
             oTable.fnDraw();
    }

我当然没有测试它,让我知道你得到了什么

相关内容

  • 没有找到相关文章

最新更新