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

What purpose do controller.$viewValue and controller.$modelValue serve in the Angular framework?

I'm confused about the connection between scope.ngModel and controller.$viewValue/controller.$modelValue/controller.$setViewValue(). I'm specifically unsure of the purpose of the latter three. Take a look at this jsfiddle: <input type="text" ...

Is it possible to extract an attribute value from a parent element using ReactJS?

https://i.stack.imgur.com/OXBB7.png Whenever I select a particular button, my goal is to capture the {country} prop that is linked to it. I attempted the following approach import React, { useState, useEffect } from 'react' import axios from &ap ...

"Nested AngularJS controllers: a deep dive into the world

Recently, I've been delving into the world of AngularJS and I can't shake the feeling that my approach to the paradigm might be a bit off. I have a controller for managing panes (linked to an ng-repeat) which tracks which panes the user has open ...

Encountering an issue with Vue JS axios request and filter: the function this.XX.filter is not operational

I am encountering challenges while trying to implement a basic search feature on a JSON file obtained through an API. Each component works independently: I can successfully retrieve data from the API, perform searches on non-API data, and even search cert ...

Difficulties with managing button events in a Vue project

Just getting started with Vue and I'm trying to set up a simple callback function for button clicks. The callback is working, but the name of the button that was clicked keeps showing as "undefined." Here's my HTML code: <button class="w ...

Unable to Retrieve Stripe Connect Account Balance

I've been attempting to retrieve the balance for my connected accounts in Stripe, but every time I make an API call, it keeps returning the platform's account balance instead of the connected account balance. This is happening while in test mode. ...

Is there a way to verify if the password entered by the user matches the input provided in the old password field?

I am trying to compare the user's password with the one entered in the "oldPassword" input field. The challenge is hashing the input from the "oldPassword" field for comparison. How can I achieve this? Please review my ejs file and suggest improvement ...

Navigating with React-router can sometimes cause confusion when trying

I'm having trouble with my outlet not working in react-router-dom. Everything was fine with my components until I added the Outlet, and now my navigation component isn't showing even though other components are rendering. import Home from ". ...

Click the "Add" button to dynamically generate textboxes and copy the contents from each

I am working on a project where I have an Add button and 6 columns. Clicking on the Add button generates rows dynamically, which can also be deleted. My challenge is to copy the content of one textbox into another in 2 of the columns. This copying function ...

The POST request is not functioning. An error has occurred: Unable to retrieve response data - no content is available for the preflight request

Having trouble making a post request from my client side to the back-end. Even with the new movie hardcoded, it keeps returning an error stating that there was a failure in loading the response data. Below is the code for the front-end: //Fetch request t ...

Tips for retrieving the distance from the top of a specific div element and transferring it to another div

How can I add margin-top based on the tab that is clicked? Specifically, when TAB 4 is selected, I want the content to remain in the same position from the top. ...

Communication through HTTP requests is not supported between docker containers

I currently have two applications running as services within a docker-compose environment. My React App A Node.js server In an attempt to make an HTTP request from my React app to the Node.js server, I am using: fetch("http://backend:4000/") However, w ...

Dart Manual Injection Guide

What is the recommended method for manually injecting an instance in Angular Dart? Can you demonstrate with an example similar to the one below in AngularJS: var myInjector = angular.injector(["ng"]); var $http = myInjector.get("$http"); ...

Activate night mode in ElementPlus using CDN

Is there a way to set the theme to dark in ElementUI + Vue when using CDNs instead of npm? <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width,initial-scale=1.0" /> ...

Clicking on the menu in mobile view will cause it to slide upward

I have implemented sticky.js on my website and it is working well. However, when I resize the browser to mobile view and click the main menu button, it goes up and I am unable to close it. I have to scroll up to see it again. How can I make it stick to the ...

Output the word 'Days', 'Months', or 'Years' depending on the value of N, which represents the number of days

I am currently utilizing moment.js in conjunction with vue 3. I've implemented a function that calculates the disparity between two dates. The functionality works as expected, but here's where my query comes in. The function is structured like so ...

Performing HTTP requests within a forEach loop in Node.js

I have a collection of spreadsheets containing data that needs to be uploaded through an API. Once the data is extracted from the spreadsheet, I create individual objects and store them in an array. My initial approach involved iterating over this array, ...

Invoking a nested method within Vue.js

I am facing an issue in my vuejs application with 2 methods. When I define method1 a certain way, it does not run as expected. method1: function(param1, param2){ // I can log param1 here thirdLib.debounce(function(param1, params2){ // It d ...

Discovering the specific object ID that triggered an event in JavaScript

I am developing a webpage that includes JavaScript functionality. There is a specific function in the Javascript code which gets triggered by two different elements when clicked: 1. When a checkbox is clicked: $('#chkShowAll').click( functi ...

Why does the UseEffect hook in next.js result in 2 fetch requests instead of the expected 1?

I am encountering an issue where my code is triggering two requests to my API using the GET endpoint. Unfortunately, my understanding of useEffect() is not deep enough to pinpoint where the problem lies. I want to avoid putting unnecessary strain on the ...