我想让我的表格过滤器更优雅。基本上,此表将在底部有链接,以根据表中的属性隐藏和显示行。它以我想要的方式工作,但我觉得我可以使用更少的 jQuery 行编写......有人有什么想法吗?
这是jsfiddle:http: //jsfiddle.net/vC88q/1/
和代码:
<!--*******************************************************
jquery that needs cleaned up appears at the end of this file!
Code is online at JSFIDDLE: http://jsfiddle.net/vC88q/1/
*********************************************************-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<table>
<thead>
<tr>
<th>Color Table</th>
</tr>
</thead>
<tbody>
<tr>
<td data-color="red">red</td>
</tr>
<tr>
<td data-color="white">white</td>
</tr>
<tr>
<td data-color="blue">blue</td>
</tr>
<tr>
<td data-color="bluewhite">bluewhite</td>
</tr>
<tr>
<td data-color="red">red</td>
</tr>
<tr>
<td data-color="red">red</td>
</tr>
<tr>
<td data-color="red">red</td>
</tr>
<tr>
<td data-color="blue">blue</td>
</tr>
<tr>
<td data-color="blue">blue</td>
</tr>
</tbody>
</table>
<br />
<br />
<div class="filtertable">
<a href="#" data-color="all">All</a>
<a href="#" data-color="red">Red</a>
<a href="#" data-color="white">White</a>
<a href="#" data-color="blue">Blue</a>
<a href="#" data-color="bluewhite">Blue and White</a>
</div>
<script type="text/javascript">
/*******************************************************
Here is the jquery I need help with, how can I reduce the code
I've written? Code is online at JSFIDDLE: http://jsfiddle.net/vC88q/1/
*********************************************************/
$(document).ready(function()
{
/*****************************************************************
when users clicks the "all" filter, show all the hidden table rows
*****************************************************************/
$('.filtertable a[data-color=all]').click(function() {
$('td[data-color]').parent().show();
});
/****************************************************************
when users clicks the "red" filter, hide all but red table rows
*****************************************************************/
$('.filtertable a[data-color=red]').click(function() {
$('td[data-color=red]').parent().show();
$('td[data-color=white]').parent().hide();
$('td[data-color=blue]').parent().hide();
$('td[data-color=bluewhite]').parent().hide();
});
/*****************************************************************
when users clicks the "white" filter, hide all but white table rows
****************************************************************/
$('.filtertable a[data-color=white]').click(function() {
$('td[data-color=white]').parent().show();
$('td[data-color=red]').parent().hide();
$('td[data-color=blue]').parent().hide();
$('td[data-color=bluewhite]').parent().hide();
});
/****************************************************************
when users clicks the "blue" filter, hide all but blue table rows
*****************************************************************/
$('.filtertable a[data-color=blue]').click(function() {
$('td[data-color=blue]').parent().show();
$('td[data-color=white]').parent().hide();
$('td[data-color=red]').parent().hide();
$('td[data-color=bluewhite]').parent().hide();
});
/*****************************************************************
when users clicks the "blue and white" filter, hide all but blue+white table rows
*****************************************************************/
$('.filtertable a[data-color=bluewhite]').click(function() {
$('td[data-color=bluewhite]').parent().show();
$('td[data-color=white]').parent().hide();
$('td[data-color=blue]').parent().hide();
$('td[data-color=red]').parent().hide();
});
}
);
</script>
</body>
</html>