过滤对象匹配细节

时间:2014-03-28 12:11:15

标签: javascript django angularjs

功能正常,它们按条形码和制造商过滤库存。我想让它像默认的angularjs过滤。如果我选择制造商 - LG和条形码 - 112,如果它们不匹配,那么它不应该显示任何东西。

但是现在它单独显示,当我点击过滤功能时,它会在filter2功能过滤器制造商上过滤条形码

$scope.filter = function(barcode) {
            var filtered = [];
            for(var i = 0; i < $scope.inventories.length; i++){
                if($scope.inventories[i].barcode == barcode){
                    filtered.push($scope.inventories[i]);
                } 
            }
            $scope.filtered_inventories = filtered;
        };

        $scope.filter2 = function(manufacturer) {
            var filtered = [];
            for(var i = 0; i < $scope.inventories.length; i++){
                if($scope.inventories[i].manufacturer == manufacturer){
                    filtered.push($scope.inventories[i]);
                } 
            }
            $scope.filtered_inventories = filtered;
        };

2 个答案:

答案 0 :(得分:0)

您可以创建更通用的过滤方式:

$scope.filterByBarcode = function(list, key, value) {
    return list.filter(function(item){
        return item[key] === value;
    });
};

当您过滤时,您可以致电:

$scope.filtered_inventories = $scope.filter($scope.filter($scope.inventories, 'barcode', 112), 'manufacturer', 'LG')

但为了简化你可以创建一个函数:

$scope.filterByBarcodeAndManufacturer = function(barcode, manufacturer) {
    $scope.filtered_inventories = 
        $scope.filter($scope.filter($scope.inventories, 'barcode', barcode), 'manufacturer', manufacturer);  
}

答案 1 :(得分:0)

试试这个

    $scope.filter = function (barcode, manufacturer) {
        var filtered = [];
        for(var i = 0; i < $scope.inventories.length; i++){
            if($scope.inventories[i].barcode == barcode || $scope.inventories[i].manufacturer == manufacturer){
                filtered.push($scope.inventories[i]);
            } 
        }
        $scope.filtered_inventories = filtered;
    };