jQuery 滑块范围:将范围作为过滤器应用于 table 行
jQuery slider range: apply range as a filter on table rows
对于我的实习,我必须为 table 创建一个过滤器,它必须只显示您给它的值之间的行。
我用 jQuery UI 作为 range slider
我有一个正常的 HTML table.
我无法让它工作,我尝试了很多不同的方法。
这是我的代码:
$(function() {
$( "#slider-range" ).slider({
range: true,
min: 0,
max: 500,
values: [ 75, 300 ],
slide: function( event, ui ) {
$( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
$( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
" - $" + $( "#slider-range" ).slider( "values", 1 ) );
$("#ADC_DAC").find("td:nth-child(0)").filter(function () {
return parseInt($(this).text()) < $( "#slider-range" ).slider( "values", 0 );
}).parent().hide();
$("#ADC_DAC").find("td:nth-child(0)").filter(function () {
return parseInt($(this).text()) > $( "#slider-range" ).slider( "values", 1 );
}).parent().hide();
}
});
});
滑块的 ID slider-range
和 table ID ADC_DAC
。
我的 table 是这样组成的:
<table id="ADC_DAC">
<tr>
<td>h1</td>
<td>h2</td>
<td>h3</td>
</tr>
<tr>
<td>23</td>
<td>test</td>
<td>test2</td>
</tr>
</table>
但随后有更多行并且第一行的值介于 0 和 500 之间(需要过滤)
您尝试更改 slide: function() {}
中的 table 属性是正确的。
但是,函数中的代码利用了 find
和其他不利的 select。
最简单的方法是简单地 select table 并遍历每一行和每一列,如下所示:
var table = document.getElementById("theTable");
for (var i = 1, row; row = table.rows[i]; i++) {
//iterate through rows (we SKIP the first row: counter starts at 1!)
for (var j = 0, col; col = row.cells[j]; j++) {
//iterate through columns: if first column not in range: HIDE, else SHOW
if (j == 0) { // if first column
if ($(col).html() >= ui.values[ 0 ] && $(col).html() <= ui.values[ 1 ]) {
// if in interval
$(row).show();
} else {
$(row).hide();
}
}
}
}
这应该可以满足您的要求。此解决方案比您的解决方案简单得多,因为您不必处理 .parent
和 .children
select 或。特别是对于像 tables 这样的二维结构,for loops
通常更容易掌握并保持良好的可读性。但是,它可能不是最短的代码。
这是有效的 jsFiddle 演示:
DEMO
对于我的实习,我必须为 table 创建一个过滤器,它必须只显示您给它的值之间的行。
我用 jQuery UI 作为 range slider
我有一个正常的 HTML table.
我无法让它工作,我尝试了很多不同的方法。 这是我的代码:
$(function() {
$( "#slider-range" ).slider({
range: true,
min: 0,
max: 500,
values: [ 75, 300 ],
slide: function( event, ui ) {
$( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
$( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
" - $" + $( "#slider-range" ).slider( "values", 1 ) );
$("#ADC_DAC").find("td:nth-child(0)").filter(function () {
return parseInt($(this).text()) < $( "#slider-range" ).slider( "values", 0 );
}).parent().hide();
$("#ADC_DAC").find("td:nth-child(0)").filter(function () {
return parseInt($(this).text()) > $( "#slider-range" ).slider( "values", 1 );
}).parent().hide();
}
});
});
滑块的 ID slider-range
和 table ID ADC_DAC
。
我的 table 是这样组成的:
<table id="ADC_DAC">
<tr>
<td>h1</td>
<td>h2</td>
<td>h3</td>
</tr>
<tr>
<td>23</td>
<td>test</td>
<td>test2</td>
</tr>
</table>
但随后有更多行并且第一行的值介于 0 和 500 之间(需要过滤)
您尝试更改 slide: function() {}
中的 table 属性是正确的。
但是,函数中的代码利用了 find
和其他不利的 select。
最简单的方法是简单地 select table 并遍历每一行和每一列,如下所示:
var table = document.getElementById("theTable");
for (var i = 1, row; row = table.rows[i]; i++) {
//iterate through rows (we SKIP the first row: counter starts at 1!)
for (var j = 0, col; col = row.cells[j]; j++) {
//iterate through columns: if first column not in range: HIDE, else SHOW
if (j == 0) { // if first column
if ($(col).html() >= ui.values[ 0 ] && $(col).html() <= ui.values[ 1 ]) {
// if in interval
$(row).show();
} else {
$(row).hide();
}
}
}
}
这应该可以满足您的要求。此解决方案比您的解决方案简单得多,因为您不必处理 .parent
和 .children
select 或。特别是对于像 tables 这样的二维结构,for loops
通常更容易掌握并保持良好的可读性。但是,它可能不是最短的代码。
这是有效的 jsFiddle 演示: