我正在尝试过滤我的太妃糖数据库,并且我有一些下拉菜单允许用户选择过滤器:
<select name="selectOPtions" id="typeSelect" >
<option value="all">All Types</option>
</select>
<select name="selectOPtions" id="countrySelect" >
<option value="all" >All Countries</option>
</select>
<select name="selectOPtions" id="provinceSelect">
<option value="all" >All Provinces</option>
</select>
<select name="selectOPtions" id="citiesSelect" >
<option value="all">All Cities</option>
</select>
假设 ajax 响应包含我们的数据库,所以我们有:
商店=TAFFY(响应);
我已经根据这个逻辑填写了下拉列表:
allCountries=Stores().distinct("country");
var $countries=$('#countrySelect');
$.each(allCountries, function(id, cc){
if(cc!=null && cc!=''){
$countries.append('<option value="'+cc+'"> '+paese.toUpperCase()+'</option>');
}
});
然后我收集了用户可能在数组中选择的所有值作为过滤查询:
$('select[name=selectOPtions]').on('change', function(){
myFilter=[];
chosenType=$('#typeSelect').children("option:selected").val();
chosenCountry=$('#countrySelect').children("option:selected").val();
chosenProvince=$('#provinceSelect').children("option:selected").val();
chosenCity=$('#citiesSelect').children("option:selected").val();
if(chosenType!=null && chosenType !="" && chosenType!="all"){
myFilter.push({type:chosenType});
}
if(chosenCountry!=null && chosenCountry!="" && chosenCountry!="all"){
myFilter.push({country:chosenCountry});
}
if(chosenProvince!=null && chosenProvince!="" && chosenProvince!="all"){
myFilter.push({province:chosenProvince});
}
if(chosenCity!=null && chosenCity!="" && chosenCity!="all"){
myFilter.push({city:chosenCity});
}
list=Stores(myFilter).get();
显示结果:
var $storesShow=$('#storesSelect');
$storesShow.empty();
$.each(list, function(id, rec){
if(rec !=''){
$storesShow.append('<option value="'+rec.store_code+'"> '+rec.type+"-"+rec.country+ "-"+rec.city+'</option>');
}
});
});
当我选择一些过滤器时,它不会返回正确的结果。我可以举个例子:如果我想要太妃糖返回 country="Italy" AND type="DEMO" 的所有记录,它会返回“OR”结果。谢谢!