0

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$ ?

3
  • can you share sample response. Commented Nov 8, 2016 at 9:14
  • Please share the response JSON Commented Nov 8, 2016 at 9:17
  • We have "min_price": 50 (for example) for Hotel in json Commented Nov 8, 2016 at 9:18

2 Answers 2

2

Try adding this onto the end of your statement:

// Assuming you're doing some kind of promise resolution;
let deferred = $q.defer();

.success((data) => {
  deferred.resolve(data);
});

The resulting data from this you can then filter:

// assume 'data' is your real resolved data;
let filteredData = data.filter((hotel) => hotel.min_price < 50);

That's ES6 syntax but you get the gist.

Sign up to request clarification or add additional context in comments.

Comments

1

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

7 Comments

Awesome, It is working (But without this line: min_price: 50)
Do have any idea how can make it dynamic and changing the min_price value with select option by user?
@Mohammad.Gh Just add the ng-model value of the select ?
And how can filtering min_price between two price? 50 < min_price< 80
@Mohammad.Gh hotel > min_price && hotel < max_price
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.