angular js多个复选框在多级JSON上过滤

时间:2015-03-23 21:33:15

标签: javascript json angularjs checkbox filter

嘿伙计们,我在多个复选框上找到了这个:angular js multiple checkbox with custom filters

我有类似的问题但是多级JSON。 这样的事情:http://jsfiddle.net/u9a1oLp6/5/

特别是这部分是我的问题:

$scope.searchFilter = function(row){
        var mercChecked = getChecked($scope.merchantCheckboxes);
        var brandChecked = getChecked($scope.brandCheckboxes);
        if(mercChecked.length == 0 && brandChecked.length == 0)
            return true;
        else{
            if($scope.merchantCheckboxes[row.MerchantName])
                return true;
            else{
                return row.BrandList.split(/,\s*/).some(function(brand){
                    return $scope.brandCheckboxes[brand];
                });
            }
        }
    };

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

这个问题非常简单,你在BrandMerchant之后错过了你的json中的逗号我通过查看浏览器的控制台发现了。

{
        "MerchantName": "amazon",
        "BrandMerchant":[
            {
        "BrandList": " pepe jeans, peter england, red tape"
            }
         ], // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< THIS COMMA HERE
         "Description": "amazon Store"
}

另外,不确定是否要将Brandlist更改为数组? "BrandList": [item1, item2, etc]

答案 1 :(得分:0)

您只需更正访问BrandList的代码即可。

通过BrandMerchant数组访问它:

$scope.searchFilter = function(row){
    var mercChecked = getChecked($scope.merchantCheckboxes);
    var brandChecked = getChecked($scope.brandCheckboxes);
    if(mercChecked.length == 0 && brandChecked.length == 0)
        return true;
    else{
        if($scope.merchantCheckboxes[row.MerchantName])
            return true;
        else{
            return row.BrandMerchant[0].BrandList.split(/,\s*/).some(function(brand){
                return $scope.brandCheckboxes[brand];
            });
        }
    }
};

当您为正确的过滤器构建BrandList数组时:

_.each($scope.records, function(i){
   if(i.BrandMerchant[0].BrandList)   
$scope.BrandList=_.union($scope.BrandList,i.BrandMerchant[0].BrandList.split(','       ));
});

选中fiddle