【问题标题】:dynamic "and" query in taffy DBtaffy DB 中的动态“和”查询
【发布时间】:2021-06-18 17:54:21
【问题描述】:

我正在尝试过滤我的太妃糖数据库,我有一些下拉菜单允许用户选择过滤器:

<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”结果。 谢谢!

【问题讨论】:

    标签: javascript jquery ajax taffydb taffy


    【解决方案1】:

    好的,我找到了结果。问题是太妃糖中的数组意味着 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>');
                         }
                        });
                 });
                
    
         
    

    【讨论】:

      猜你喜欢
      • 2016-10-03
      • 2020-12-01
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多