Mohammad.Gh Mohammad.Gh - 27 days ago 7
Javascript Question

Filter results from API with angularjs

I am receiving data with API in Angular and trying to filter the result for the hotels with the min_price less than for example 50$!

$http.get($rootScope.baseurl + 'api/hotels/', {
params: {
page_size: $scope.page_size,
page: $scope.page,
goingTo: goingTo,
ordering: $scope.sortBy,
star: $scope.filter.star,
min_price: ????
}}).then(
function(data, status, headers, config) {
$scope.Hotels = data.data.results;
$scope.count = data.data.count;
$scope.loading = false;
PageService.setContentReady();
},
function(data, status, headers, config) {
$scope.loading = false;
PageService.contentStatus = 'ready';
}
);


How to show hotels under 50$ ?

Answer

Find below your updated code

$http.get($rootScope.baseurl + 'api/hotels/', {
    params: {
        page_size: $scope.page_size,
        page: $scope.page,
        goingTo: goingTo,
        ordering: $scope.sortBy,
        star: $scope.filter.star,
        min_price: 50
    }
}).then(
    function(data, status, headers, config) {
        $scope.Hotels = data.data.results;
        $scope.Hotels = $scope.Hotels.filter(function(hotel){ return hotel.min_price < 50}); //filter hotels
        $scope.count = data.data.count;
        $scope.loading = false;
        PageService.setContentReady();
    },
    function(data, status, headers, config) {
        $scope.loading = false;
        PageService.contentStatus = 'ready';
    }
);
Comments