Utilize AngularJS to refine and sort through data retrieved from an API

I have an Angular application that is fetching hotel data from an API. I want to filter the results based on the minimum price of the hotels being less than $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 can I display hotels with prices under $50?

Answer №1

Here is a suggestion to enhance your code:

// If you are working with promises;
let deferred = $q.defer();

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

You can then filter the resulting data like this:

// Assuming 'data' represents the resolved data;
let filteredData = data.filter((hotel) => hotel.min_price < 50);

Although it's written in ES6, it should give you an idea of what needs to be done.

Answer №2

Here is the updated version of your 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';
    }
);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Append a constant string to the conclusion of the route parameter

Using React Router v6.4.1, I am aiming for a consistent conclusion to a series of dynamic routes. Specifically, I want my product detail routes to always end with "-product". For example, if the path is "/shaver-900-product", it should activate my Produc ...

Tips for configuring jQtree to initially display the tree structure from the HTML source

Up to this point, I have utilized jQuery TreeView for the navigation menus on my website. However, as the main navigation menu has grown significantly in size (40869 bytes out of 67054 bytes), I am seeking a way to streamline it by using AJAX calls to fetc ...

Error encountered in MVC 5: When using jQuery to assign a value to an object, the

Currently tackling a web development project as a newbie in the field and running into an issue with this snippet of code (which sets the end date to tomorrow if left blank): var date = new Date($('#txtStartDate').val()); var tomorrow = date.ge ...

Collect all the attribute values of the checkboxes that have been checked and convert them

Is there a way to retrieve the attribute values of all checked checkboxes as an XML string? <input type="checkbox" id="chkDocId1" myattribute="myval1"/> <input type="checkbox" id="chkDocId2" myattribute="myval43"/> <input type="checkbox ...

Retrieving the specific value of an input from a group of inputs sharing a common class

After extensive research, I have not found a suitable solution to my specific issue. I am creating a block of HTML elements such as <div>, <span>, <i>, and <input> multiple times using a for loop (the number of times depends on the ...

Maintaining a reliable and efficient way to update the userlist in a chatroom using PHP, AJAX, and SQL

I've successfully created a chatroom using PHP, JavaScript, AJAX, and SQL without the use of JQuery or any other tools. I maintain user persistence through session variables to keep users visible on the front page of my website (www.chatbae.com). How ...

Managing code requiring document.title in Next.js with Static Site Generation (SSG)

Currently, I have 2 dynamic SSG pages located under /blog/[slug]. Within these pages, I am rendering a component using next/link. When I click on these links to navigate to another slug, I encounter an issue. The problem arises when I want to execute some ...

How can information be seamlessly transferred between two HTML pages using a script?

For my listview, I need to pass values like "name", "info", "hap", "lat," and "lon" to page2.html. What is the best way to achieve this? In PHP, I typically use POST or GET methods, but is there a more efficient approach? If using GET is recommended, how ...

Unable to make a jQuery Ajax HTTP Request within a Chrome extension

I'm facing an issue with sending a jQuery Ajax HTTP Request within google chrome extensions. I have already imported the jQuery library and used the following code: $.ajax({ method: "PUT", url: "https://spreadsheets.google ...

The pinFileToIPFS method in Pinata IPFS is currently unable to accept files uploaded by users

Currently, I am immersed in a project where I am utilizing React.js, Express.js, and Node.js to transform a user-provided image into an NFT on the Ethereum blockchain. To achieve this, I must first upload the image to IPFS (Pinata being my choice of servic ...

What is the best way to insert images into a database?

I am currently working on an internship project that requires me to include an image when adding an employee to my table. https://i.stack.imgur.com/xif58.png https://i.stack.imgur.com/wDkY7.png We are using AngularJS on the front end and ASP.NET Core 3. ...

Choosing between cjs and esm for a React components library

I'm working on a components library that will soon be published to npm for use in a razzle app. My main query revolves around the best practices for building these packages - should they be built with CommonJS (cjs) or ECMAScript Modules (esm)? And wh ...

Struggling to develop a Singleton AngularJS Service?

I am in the process of creating a Singleton AngularJS service using the 4th method. The structure of my service is as follows: provider('SocketService', { socket: null, // methods used within the config() of a module connect: functi ...

Saving components locally (vue2)

Looking for help with passing data from Props to Data in vue.js? Check out this Stack Overflow discussion. If you're running into issues, take a look at this sandbox example: https://codesandbox.io/s/u3mr8. Concerned about side effects of copying ob ...

Steps for displaying a post's image when hovering over its link in WordPress

One section of my website displays the latest 6 posts, and here is the HTML code for it: <div class="latest-posts"> <div id="latest-posts-title"> <p>Latest Posts</p> </div> <div id="latest-posts-pictures" ...

Is it possible to have two unique keys in a single MongoDB schema?

Currently attempting to achieve this functionality using mongoose: const newContent = new Schema({ heading: { type: String, unique: true }, url: { type: String, unique: true }, //... }); However, an error is being thrown: MongoError: E11000 dupl ...

Unable to reach the array file

Having trouble accessing the elements of the file array const p_page = postP.new_pages; const p_page_o = postP.new_pages_order; const p_page_o_len = p_page_o.length; if (p_page_o_len > 0) { for (let i = 0; i < p_page_o_len; i++) { c ...

Is there a way to ensure that the ng-repeat finishes before the parent directive is executed?

Is there a way to ensure that the extend-item directive runs only after the ng-repeat has finished rendering in the DOM? In the provided example, the ^ symbol is added only to the static ul elements because the dynamic ul elements generated by ng-repea ...

Extra spaces within the source code visible after the page has been processed by Angular

Within my angular application, I am retrieving a list of addresses from a service that provides an object structured like this: { Flat: "10B", BuildingName: "Some Building", Line: "10B Some Building Road Town POST CODE", Postcode: "POST CODE", ...