我有两个选项,我现在要尝试过滤,但是会添加第三个,也许是后来的第四个(例如:现在我在价格和评论之间进行过滤,但是会想要添加更多价格列表和另一个过滤器类别,如“评级”,包括3,4或5星。)
使用我在下面列出的逻辑工作正常,但我觉得它会变得非常冗长和复杂(并且不必要)。我知道有一种重构代码的方法我只是想知道最好的方法是什么? HTML:
<select class="deals">
<option value="deals-all">All deals</option>
<option value="50">$50</option>
<option value="25">$25</option>
</select>
<select class="reviews">
<option value="reviews-all">All reviews</option>
<option value="reviews-positive">Positive reviews</option>
<option value="reviews-negative">Negative reviews</option>
</select>
的jQuery
$('.reviews, .deals').change(function() {
var reviewsVal = $('.reviews :selected').val();
var dealsVal = $('.deals :selected').val();
if((reviewsVal == 'reviews-all') && (dealsVal == 'deals-all')) {
$('.review-positive').show();
$('.review-negative').show();
$('.deals-25').show();
$('.deals-50').show();
}
else if((dealsVal == '50') && (reviewsVal == 'reviews-positive')) {
$('.review-negative.deals-50').hide();
$('.review-positive.deals-50').show();
$('.review-negative.deals-25').hide();
$('.review-positive.deals-25').hide();
}
else if((dealsVal == '50') && (reviewsVal == 'reviews-negative')) {
$('.review-negative.deals-50').show();
$('.review-positive.deals-50').hide();
$('.review-negative.deals-25').hide();
$('.review-positive.deals-25').hide();
}
else if((dealsVal == '25') && (reviewsVal == 'reviews-positive')) {
$('.review-negative.deals-50').hide();
$('.review-positive.deals-50').hide();
$('.review-negative.deals-25').hide();
$('.review-positive.deals-25').show();
}
else if((dealsVal == '25') && (reviewsVal == 'reviews-negative')) {
$('.review-negative.deals-50').hide();
$('.review-positive.deals-50').hide();
$('.review-negative.deals-25').show();
$('.review-positive.deals-25').hide();
}
else if((dealsVal == 'deals-all') && (reviewsVal == 'reviews-positive')) {
$('.review-negative.deals-50').hide();
$('.review-positive.deals-50').show();
$('.review-negative.deals-25').hide();
$('.review-positive.deals-25').show();
}
else if((dealsVal == 'deals-all') && (reviewsVal == 'reviews-negative')) {
$('.review-negative.deals-50').show();
$('.review-positive.deals-50').hide();
$('.review-negative.deals-25').show();
$('.review-positive.deals-25').hide();
}
else if((dealsVal == '50') && (reviewsVal == 'reviews-all')) {
$('.review-negative.deals-50').show();
$('.review-positive.deals-50').show();
$('.review-negative.deals-25').hide();
$('.review-positive.deals-25').hide();
}
else if((dealsVal == '25') && (reviewsVal == 'reviews-all')) {
$('.review-negative.deals-50').hide();
$('.review-positive.deals-50').hide();
$('.review-negative.deals-25').show();
$('.review-positive.deals-25').show();
}
else {
$('.review-positive').show();
$('.review-negative').show();
$('.deals-25').show();
$('.deals-50').show();
}
});
$('.reviews-positive').click(function() {
$('.review-negative').hide();
$('.review-positive').show();
});
$('.reviews-negative').click(function() {
$('.review-positive').hide();
$('.review-negative').show();
});
});
希望你能看到我想要的东西,谢谢你的任何意见。
*编辑:jsfiddle:http://jsfiddle.net/TXywp/1/
答案 0 :(得分:2)
我可以想到两种不同的方法来简化代码并使其更容易维护和增长:
您可以根据条件以算法方式推导出应隐藏或显示的项目。
您可以创建条件和操作表,以便添加新条件和操作只是向表中添加新项目。
更改您的HTML,使其能够自我描述它应显示的内容。
这是表驱动方法的样子:
$('.reviews, .deals').change(function() {
var allDeals = '.review-negative.deals-50, .review-positive.deals-50, .review-positive.deals-25, .review-negative.deals-25';
var table = [
{rv: 'reviews-all', dv: 'deals-all', show: '.review-positive, .review-negative, .deals-25, .deals-50'},
{rv: 'reviews-positive', dv: '50', show: '.review-positive.deals-50'},
{rv: 'reviews-negative', dv: '50', show: '.review-negative.deals-50'},
{rv: 'reviews-positive', dv: '25', show: '.review-positive.deals-25'}
{rv: 'reviews-negative', dv: '25', show: '.review-negative.deals-25'},
{rv: 'reviews-positive', dv: 'deals-all', show: '.review-positive.deals-50, .review-positive.deals-25'},
{rv: 'reviews-negative', dv: 'deals-all', show: '.review-negative.deals-50, .review-negative.deals-25'},
{rv: 'reviews-all', dv: '50', show: '.review-negative.deals-50, .review-positive.deals-50'},
{rv: 'reviews-all', dv: '25', show: '.review-negative.deals-25, .review-positive.deals-25'}
];
var reviewsVal = $('.reviews :selected').val();
var dealsVal = $('.deals :selected').val();
$(allDeals).hide();
var item, found = false;
for (var i = 0, len = table.length; i < len; i++) {
item = table[i];
if (dealsVal == item.dv && reviewsVal == item.rv) {
$(item.show).show();
found = true;
break;
}
}
if (!found) {
$('.review-positive, .review-negative, .deals-25, .deals-50').show();
}
});
而且,这是一种算法方法,它允许您通过向数组添加一个新条目来添加新的交易值。只有两笔交易,这可能比表驱动方法更多,但如果您有4个或更多交易级别,这将更容易维护。
$('.reviews, .deals').change(function() {
var deals = ['25', '50'];
function addAllDeals(base, prefix) {
for (var i = 0; i < deals.length; i++) {
if (base) base += ", ";
base += prefix + deals[i];
}
return(base);
}
function addSingleDeal(prefixes, deal) {
var sel = [];
for (var i = 0; i < prefixes.length; i++) {
sel.push(prefixes[i] + deal);
}
return(sel.join(", ");
}
var reviewsVal = $('.reviews :selected').val();
var dealsVal = $('.deals :selected').val();
var itemsToShow = "";
// hide everything to start
var initialHide = addAllDeals("", ".review-positive.deals-");
initialHide = addAllDeals(initialHide, ".review-negative.deals-");
$(initialHide).hide();
if (reviewsVal == 'reviews-all') {
if (dealsVal == 'deals-all') {
itemsToShow = addAllDeals(".review-positive, .review-negative", ".deals-");
} else {
itemsToShow = addSingleDeal([".review-negative.deals-", ".review-negative.deals-"], dealsVal);
}
} else if (reviewsVal == 'reviews-positive') {
itemsToShow = '.review-positive.deals-' + dealsVal;
} else if (reviewVal == 'reviews-negative') {
itemsToShow = '.review-negative.deals-' + dealsVal;
} else {
itemsToShow = addAllDeals(".review-positive, .review-negative", ".deals-");
}
});
如果您可以将HTML(如上面的第三个选项中)更改为:
<select class="deals">
<option value="deals-all" data-base=".review-positive, .review-negative">All deals</option>
<option value="50" data-base=".deals-50">$50</option>
<option value="25" data-base=".deals-25">$25</option>
</select>
<select class="reviews">
<option value="reviews-all" data-filter="">All reviews</option>
<option value="reviews-positive" data-filter=".review-positive">Positive reviews</option>
<option value="reviews-negative" data-filter=".review-negative">Negative reviews</option>
</select>
然后,你可以使用这个简单的javascript:
$('.reviews, .deals').change(function() {
var dealsBase = $('.deals :selected').data("base");
var reviewsFilter = $('.reviews :selected').data("filter");
// hide all
$(".review-negative, .review-positive").hide();
// show the desired ones
var base = $(dealsBase);
if (reviewsFilter) {
base = base.filter(reviewsFilter);
}
base.show();
});
警告:由于您没有提供代码/ HTML的有效jsFiddle示例来尝试此操作,因此尚未运行此代码或检查输入错误。但是,希望你能理解这两种方法。
答案 1 :(得分:1)
要获得动态结果,标准化命名是最佳方式。 首先,标准化标准选择器,为它们提供通用类并区分id属性。在建议使用值的命名对流之后。这会产生一些
的内容 <select id="deal" class="criteriaSelector">
<option value="all">All deals</option>
<option value="50">$50</option>
<option value="25">$25</option>
</select>
<select id="review" class="criteriaSelector">
<option value="all">All reviews</option>
<option value="positive">Positive reviews</option>
<option value="negative">Negative reviews</option>
</select>
然后,除了过滤类之外,您还要提供要过滤泛型类的所有项目。我将使用 .item 作为示例。您还希望能够从选定的值和条件名称构造类,因此使用。{id} - {value} 系统将完美运行。这将给我们留下一些符合
的内容 <div class="item review-positive deal-25"></div>
<div class="item review-negative deal-50"></div>
此设置将允许我们构建动态和可扩展的代码:
$('.criteriaSelector').change(function() {
// Initialize criteria string
var criteria = '';
// Set value for all selector
var showAll = true;
// Iterate over all criteriaSelectors
$('.criteriaSelector').each(function(){
// Get value
var val = $(this).children(':selected').val();
// Check if this limits our results
if(val !== 'all'){
// Append selector to criteria
criteria += '.' + $(this).attr('id') + '-' + val;
// We don't want to show all results anymore
showAll = false;
}
});
// Check if results are limited somehow
if(showAll){
// No criterias were set so show all
$('.item').show();
} else {
// Hide all items
$('.item').hide();
// Show the ones that were selected
$(criteria).show();
}
});
现在,只要遵循命名约定,添加新的条件选择器就不需要对这段代码进行更改。