我有一个列表,其中包含我想通过打开/关闭(选择、单选、复选框)过滤并按字段(名称、日期、...)排序的项目。
下面的代码可以用于基本过滤,但这不是一个好方法。我想知道如何以专业的方式做到这一点。有什么好的 jQuery 插件吗?我不想要一个小部件,只是它背后的过滤/排序引擎。
# this code is pseudocode and not running! #
<script>
// ready
$(function() {
// get items
var items = jQuery.parseJSON("items_json.php");
// items filtered
var items_filtered = new Array();
// conditions/relations for filtering
var filter1_relations = jQuery.parseJSON("filter1_relations.php");
var filter2_relations = jQuery.parseJSON("filter2_relations.php");
// build unfiltered list
build_list(items);
// build filtered list
$(".filter").on("change", function() {
items_filtered = run_filter1(items, filter1_relations);
items_filtered = run_filter2(items_filtered, filter2_relations);
build_list(items_filtered);
});
});
// filters the 'items' by checking if a 'relation' exists
function run_filter1(items, relations) {
items_filterd = new Array();
$.each(items, function(key, item) {
for (var i = 0; i < relations.length; i++) {
if (...) {
items_filtered.push(item);
}
}
});
return items_filtered;
}
// build list and add to #content
function build_list(items) {
html = '<ul>';
for (var i = 0; i < items.length; i++) {
html += '<li>'+ items[i].name +'</li>';
}
html += '</ul>';
$("#content").html(html);
}
</script>
Filter 1.1: <input type="checkbox" name="filter1_condition1" value="1" class="filter"/><br/>
Filter 1.2: <input type="checkbox" name="filter1_condition1" value="1" class="filter"/><br/>
Filter 2.1: <input type="checkbox" name="filter1_condition1" value="1" class="filter"/><br/>
Filter 2.1: <input type="checkbox" name="filter1_condition1" value="1" class="filter"/><br/>
<div id="content"></div>
我通过搜索 stackoverflow找到了这段代码,但它并不那么广泛。
提前致谢!