Struggling to organize and paginate numbers in angularjs and facing filtering and sorting issues

I'm running into some issues with getting the paging feature to work properly while applying filters.

Currently, when the filters are active, the paging numbers do not display correctly and only filter the first page of results. What I'm aiming for is:

  • considering all items initially
  • applying text and category filters
  • ordering the filtered results
  • showing only the current page of results
  • updating the page number to reflect the correct number of pages based on the applied filters

This is the ng-repeat statement I am utilizing:

item in items | 
filter: { name: filters.name, category: filters.category } | 
orderBy: predicate: reverse | 
startFrom: currentPage * pageSize | 
limitTo: pageSize

And here is the full HTML code:

<table class="table table-striped">
        <thead>
            <tr>
                <th><a href="" ng-click="predicate='id';reverse=!reverse">#</a></th>
                <th><a href="" ng-click="predicate='name';reverse=!reverse">Name</a></th>
                <th><a href="" ng-click="predicate='category';reverse=!reverse">Category</a></th>
                <th><a href="" ng-click="predicate='date';reverse=!reverse">Date</a></th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="item in items | filter:{name:filters.name,category:filters.category} | orderBy:predicate:reverse | startFrom:currentPage*pageSize | limitTo:pageSize">
                <td>{{ item.id || 'None' }}</td>
                <td>{{ item.name || 'None' }}</td>
                <td>{{ item.category || 'None' }}</td>
                <td>{{ item.date || 'None' }}</td>
            </tr>
        </tbody>
    </table>
    <pagination total-items="totalItems" ng-model="currentPage"></pagination>

This is the custom startFrom filter I've created to handle pagination:

.filter('startFrom', function () {
    return function (input, start) {
        return input.slice(start);
    };
});

You can view a live demonstration here:

Answer №1

For more information, please visit: http://plnkr.co/edit/yVDZR6Zwo9P8OHT0G2Hr?p=preview

I have made some modifications

JS:

$scope.updatefilters = function(category)
          {
       $scope.filters.category = category;
       $scope.totalItems = $filter('filter')($scope.items,$scope.filters).length;
        }

        $scope.setPage = function (num) {
            $scope.totalItems = $filter('filter')($scope.items,$scope.filters).length;
            $scope.currentPage = num;
            $scope.pageSize = 5;
            console.log($scope.currentPage, $scope.totalItems);
        };

        $scope.setPage(1);
    })

html:

<div ui-view="sidebar">
            <div class="filters">
                <h1>Filters</h1>

                <h3>Category</h3>
                <p><a href="" ng-click="updatefilters('')">All Categories</a></p>
                <p><a href="" ng-click="updatefilters('music')">Music</a></p>
                <p><a href="" ng-click="updatefilters('film')">Film</a></p>
                <p><a href="" ng-click="updatefilters('tv')">TV</a></p>
            </div>
        </div>

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

Encountered an issue while trying to implement CORS using yarn

Currently experiencing the following issue: Error message: An unexpected error occurred "/home/vagrant/.cache/yarn/v1/npm-cors-2.8.4-2bd381f2eb201020105cd50ea59da63090694686/.yarn-metadata.json: Unexpected end of JSON input". Some important points to c ...

Loop through an array of arrays in JavaScript. If a match is found, add it to an existing inner array. If not, create a new

I am currently extracting data from a database, and here is a simplified representation of the information: var example = [ {'start': 1966, 'end': 1970}, {'start': 1969, 'end': 1971}, {'start&ap ...

When setting the Content-Type of an S3 object to 'image/jpeg' in NodeJS, it may appear as 'application/octet' in the S3 console

I am facing an issue with the Content-Type of an image stored in my JPEG buffer. While it uploads and downloads successfully from S3, I encounter errors when trying to send it via the Messenger API programmatically. The S3 console indicates that the actual ...

Unlimited scrolling feature on a pre-filled div container

Looking for a way to implement infinite scroll on a div with a large amount of data but struggling to find the right solution? I've tried various jQuery scripts like JScroll, MetaFizzy Infinite Scroll, and more that I found through Google search. Whi ...

Two interconnected queries with the second query relying on the results of the first

I am currently facing a challenge in my Phonegap (Cordova) application where I need to display a list of items, each requiring an additional query. Let me simplify it with an example scenario. Imagine a student can be enrolled in multiple courses and a co ...

Pass the JavaScript variable and redirect swiftly

One of the functionalities I am working on for my website involves allowing users to submit a single piece of information, such as their name. Once they input their name, it is sent to the server via a post request, and in return, a unique URL is generated ...

Attempt to resend email if unsuccessful

I have implemented nodemailer in my node application for sending emails. However, I am encountering an issue where the email sometimes fails to send and generates an error message. Typically, it takes two or three attempts before the email is successfully ...

What is the best way to incorporate an expression into a package.json file?

Query: Is there a way to automatically increase the version in a script message? I need my release message to always be one version higher than the previous. Aim: For example, if I have version **0.1.2**, I would like to update my commit message to 0.1.3 ...

Stop HTML audio playback upon clicking on a specific element

I'm looking to add background music to my website with a twist - a music video that pops up when the play button is clicked. But, I need help figuring out how to pause the background music once the user hits play. Play Button HTML - The play button t ...

How can I remove unnecessary components from an API result in discord.js before sending it?

The particular API being discussed is a pun-themed one, specifically this one. Here is the code snippet I am currently utilizing: const superagent = require("superagent") module.exports = { run: async(client, message, args) => { const pun = a ...

Tips for organizing your JSON Structure within ReactJs

In the given example, I have a JSON structure with information about different airlines. The Airline Name is dynamic and we need to separate the JSON into an expected array format. const arr = [ { Airline: "Goair", Departure: "01:50" ...

I have the ability to see HTTP-only cookies within my web browser

So, I always believed that httpOnly cookies could only be accessed during a http request. But the other day, while inspecting Firefox dev tools, I noticed that I could actually view the cookies' values. Is this standard behavior? ...

Is there a way to develop a login form that retrieves information from a Google Sheet database?

Please help me with a solution. I have developed a registration form which effectively saves users' information in a Google Sheet. However, now I am yearning to create a login form that verifies the stored data and redirects the user to a different pa ...

Using an AngularJS directive that has a dynamic ID may cause issues with d3's ability to append

I encountered a situation where my angularJS directive works fine when I specify a hardcoded ID for appending elements, but fails to append when the ID is dynamically generated. Surprisingly, there are no errors reported by d3 or the browser. However, upon ...

Communication between the register service worker and the client page begins with the dispatch of a

Looking to pass a boolean variable to app.js when the registration onupdatefound function is triggered. This way, whenever a new update is received, app.js will be notified and I can display a popup with a refresh button. I have most of it implemented alr ...

Sending the format of the display value to an Angular component

Looking for a way to pass display value formatting to an Angular component. Here are a couple of examples: format="'(utc, offset) location (tz)'" === '(UTC -04:00) New York (EDT)' or format="'(tz, offset) location'" === &a ...

Looking to display an alert message upon scrolling down a specific division element in HTML

In the midst of the body tag, I have a div element. <div id="place"> </div> I'm trying to achieve a scenario where upon scrolling down and encountering the div with the ID "place", an alert is displayed. My current approach involves che ...

"Troubleshooting the failure of the alert function to work properly when loading content

I am working on a webpage named index.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Co ...

Multer is successfully retrieving images, but unfortunately, it is failing to save the files in the intended directory

I am currently facing an issue with my Express server. The problem arises when a user attempts to make a post request for their profile, including a profile picture submission. I have set up Multer to handle the image upload process and store the photo in ...

``Is there a way to retrieve the file path from an input field without having to submit the form

Currently, I am looking for a way to allow the user to select a file and then store the path location in a JavaScript string. After validation, I plan to make an AJAX call to the server using PHP to upload the file without submitting the form directly. Thi ...