太妃糖数据库中的动态"and"查询



我正在尝试过滤我的taffy数据库,我有一些下拉列表,允许用户选择过滤器:

<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响应包含我们的数据库,因此我们有:

Stores=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>');
}
});
});

当我选择一些过滤器时,它不会返回正确的结果。我可以举一个例子:如果我想要taffy,则返回country="的所有记录;意大利";AND类型="AND";DEMO",它正在返回";OR";后果非常感谢。

好的,我找到了结果。问题是taffy中的array表示OR。我分享的部分结果有一个主要的概念:

$('select[name=selectOPtions]').on('change', function(){
var object = {};
var operator = "===";
var chosenType=$('#typeSelect').children("option:selected").val();
var chosenCountry=$('#countrySelect').children("option:selected").val();
var chosenProvince=$('#provinceSelect').children("option:selected").val();
var chosenCity=$('#citiesSelect').children("option:selected").val();

if(chosenType!=null && chosenType !="" && chosenType!="all"){
var column ="type";
object[column]={};
object[column][operator]=chosenType;
console.log(object);
list=Stores(object).get();
}

//。。。//其他下拉也是如此

console.log(list);
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+" "+rec.addressLine1+'</option>');
}
});
});


最新更新