The AngularJS page consistently reverts back to the first page upon loading

After I select a page number, the localStorage stores this information. However, when I navigate to another page and return back, it always defaults to page 1. This is because the function vm.loadAll is called again after the page is initiated.

(function() {
'use strict';

angular
    .module('eTransportApp')
    .controller('ImprestBillController', ImprestBillController);

ImprestBillController.$inject = ['$state', 'ImprestBill', 'ParseLinks', 'AlertService', '$scope', 'eTransportAppConstants', '$localStorage', 'DateUtils'];

function ImprestBillController($state, ImprestBill, ParseLinks, AlertService, $scope, eTransportAppConstants, $localStorage, DateUtils) {

    var vm = this;

    vm.predicate = 'id';
    vm.reverse = true;
    vm.itemsPerPage = eTransportAppConstants.itemsPerPage;
    vm.page = 1;

    vm.loadAll = function() {

        ImprestBill.query({
            page: vm.page > 0? vm.page - 1: vm.page,
            size: vm.itemsPerPage,
            sort: sort(),
            fromDate: DateUtils.convertLocalDateToServer($localStorage.searchForImprestBill.fromDate),
            toDate: DateUtils.convertLocalDateToServer($localStorage.searchForImprestBill.toDate),
            fromCost: $localStorage.searchForImprestBill.fromCost,
            toCost: $localStorage.searchForImprestBill.toCost,
            status: $localStorage.searchForImprestBill.status,
            clientId: $localStorage.searchForImprestBill.clientId
        }, onSuccess, onError);
        function sort() {
            return [vm.predicate + ',' + (vm.reverse ? 'desc' : 'asc')];
        }
        function onSuccess(data, headers) {
            vm.links = ParseLinks.parse(headers('link'));
            vm.totalItems = headers('X-Total-Count');
            vm.queryCount = vm.totalItems;
            vm.imprestBills = data;
            $localStorage.pageForImprestBill.page = vm.page;
            $localStorage.pageForImprestBill.predicate = vm.predicate;
            $localStorage.pageForImprestBill.reverse = vm.reverse
        }
        function onError(error) {
            AlertService.error(error.data.message);
        }
    }

    vm.clear = function() {
        $localStorage.searchForImprestBill = {};
        $localStorage.pageForImprestBill.page = 1;
        $localStorage.pageForImprestBill.reverse = true;
        $localStorage.pageForImprestBill.predicate = 'id';
        vm.reverse = true;
        vm.predicate = 'id';
        vm.loadAll();
    }

    /*$scope.$on(eTransportAppConstants.EVT_LOAD_RESULT_IMPREST_BILL, function(event){
        vm.loadAll();
    });*/

    function init() {
        if (!$localStorage.searchForImprestBill) {
            $localStorage.searchForImprestBill = {};
        }
        if (!$localStorage.pageForImprestBill) {
            $localStorage.pageForImprestBill = {};
        }
        vm.predicate = $localStorage.pageForImprestBill.predicate? $localStorage.pageForImprestBill.predicate: 'id';
        vm.reverse = $localStorage.pageForImprestBill.reverse? $localStorage.pageForImprestBill.reverse: true;
        vm.page = $localStorage.pageForImprestBill.page? $localStorage.pageForImprestBill.page: 1;
        vm.loadAll();
    }

    vm.showClearBtn = function() {
         return ($localStorage.searchForImprestBill && JSON.stringify($localStorage.searchForImprestBill) !== '{}');
    }

    init();
}

})();

<div>
<h2 data-translate="eTransportApp.imprestBill.home.title">Imprest Bills</h2>
<jhi-alert></jhi-alert>
<div class="container-fluid">
    <div class="row">
        <div class="col-xs-12 no-padding-right no-padding-left">
            <div class="input-group no-padding-left">
                <button class="btn btn-info" ui-sref="imprest-bill.search" >
                    <span class="glyphicon glyphicon-search"></span>
                    <span  data-translate="eTransportApp.imprestBill.home.searchLabel">
                        Search new Imprest Bill
                    </span>
                </button>
                    <button class="btn btn-info " ng-click="vm.clear()" ng-if="vm.showClearBtn()">
                        <span class="glyphicon glyphicon-trash"></span>
                    </button>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-xs-12 no-padding-left">
            <button class="btn btn-primary" ui-sref="imprest-bill.new" >
                <span class="glyphicon glyphicon-plus"></span>
                <span  data-translate="eTransportApp.imprestBill.home.createLabel">
                    Create new Imprest Bill
                </span>
            </button>
        </div>
    </div>
</div>
<br/>
<div class="table-responsive">
    <table class="jh-table table table-striped">
        <thead>
            <tr jh-sort="vm.predicate" ascending="vm.reverse" callback="vm.loadAll()">
                <th jh-sort-by="id"><span data-translate="global.field.id">ID</span> <span class="glyphicon glyphicon-sort"></span></th>
                <th jh-sort-by="date"><span data-translate="eTransportApp.imprestBill.date">Date</span> <span class="glyphicon glyphicon-sort"></span></th>
                <th jh-sort-by="totalCost"><span data-translate="eTransportApp.imprestBill.totalCost">Total Cost</span> <span class="glyphicon glyphicon-sort"></span></th>
                <th jh-sort-by="status"><span data-translate="eTransportApp.imprestBill.status">Status</span> <span class="glyphicon glyphicon-sort"></span></th>
                <th jh-sort-by="paidDate"><span data-translate="eTransportApp.imprestBill.paidDate">Paid Date</span> <span class="glyphicon glyphicon-sort"></span></th>
                <th jh-sort-by="note"><span data-translate="eTransportApp.imprestBill.note">Note</span> <span class="glyphicon glyphicon-sort"></span></th>
                <th jh-sort-by="client.name"><span data-translate="eTransportApp.imprestBill.client">Client</span> <span class="glyphicon glyphicon-sort"></span></th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="imprestBill in vm.imprestBills track by imprestBill.id">
                <td><a ui-sref="imprest-bill.edit({id:imprestBill.id})">{{imprestBill.id}}</a></td>
                    <td>{{imprestBill.date | date:'dd/MM/yyyy'}}</td>
                <td>{{imprestBill.totalCost | number}}</td>
                <td data-translate="{{'eTransportApp.ImprestBillStatus.' + imprestBill.status}}">{{imprestBill.status}}</td>
                <td>{{imprestBill.paidDate | date:'dd/MM/yyyy'}}</td>
                <td>{{imprestBill.note}}</td>
                <td>
                    <a ui-sref="client-detail({id:imprestBill.client.id})">{{imprestBill.client.nickName}}</a>
                </td>
                <td class="text-right">
                    <div class="btn-group flex-btn-group-container">
                        <button type="submit"
                                ui-sref="imprest-bill.edit({id:imprestBill.id})"
                                class="btn btn-primary btn-sm">
                            <span class="glyphicon glyphicon-pencil"></span>
                            <span class="hidden-sm-down" data-translate="entity.action.edit"></span>
                        </button>
                        <button type="submit"
                                ui-sref="imprest-bill.delete({id:imprestBill.id})"
                                class="btn btn-danger btn-sm">
                            <span class="glyphicon glyphicon-remove-circle"></span>
                            <span class="hidden-sm-down" data-translate="entity.action.delete"></span>
                        </button>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</div>
<div class="text-center">
    <uib-pagination boundary-links="true" 
        class="pagination-sm" 
        total-items="vm.totalItems" 
        ng-model="vm.page"
        items-per-page="vm.itemsPerPage"
        max-size="20" 
        rotate="false" 
        ng-change="vm.loadAll()">
    </uib-pagination>
</div>

Answer №1

<div class="text-center" ng-show="vm.totalItems">
    <uib-pagination boundary-links="true" 
        class="pagination-sm" 
        total-items="vm.totalItems" 
        ng-model="vm.page"
        items-per-page="vm.itemsPerPage"
        max-size="20" 
        rotate="false" 
        ng-change="vm.loadAll()">
    </uib-pagination>
</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

Data merging in Firebase 9 and Vue 3 is not functioning properly

I am facing an issue with merging data in my firebase database. I consulted the documentation at https://firebase.google.com/docs/firestore/manage-data/add-data for guidance. After attempting to merge data using setDoc, I encountered an error (Uncaught Ty ...

Tips for preventing control click events from interfering with modal dialog interactions

From what I understand, when I open a modal dialog in jQuery, all input controls will be disabled. However, I am still able to click on buttons, divs, and other controls. Is there a way in jQuery to disable all interactions once the modal dialog is opene ...

Deliver a numerical input to the component on an equivalent hierarchical tier

I need to find a way to pass the values of the "things" array to another component on the same level in my app. The structure of my app is as follows: sidebar.component data.service body.component In the sidebar component, I have a button that triggers a ...

Both the client and server sides are in sync with the latest data, yet the rendering fails to display the recent updates

The technologies I am currently utilizing include Nodejs, Express, MySQL, and EJS. My current task involves: Creating an app.get function that retrieves necessary data (posts) from MySQL, then renders a file using that data app.get("/" + element.name, f ...

How to use v-if in Quasar framework to hide a Vuejs component on the signup page

Currently, I am utilizing the Quasar drawer feature in my application. On the signup view, I would like to hide the drawer. While my current code successfully hides the drawer, an issue arises when I reload the signup page as it briefly renders in the DOM ...

The smooth scroll feature is not functioning properly on the animated mouse-scroll down button

I've recently added an Animated Mouse Scroll Down button on my website. However, when I click the button, the smooth scroll feature is not working as expected. Important Note: I already have a separate button for navigating to the next section where ...

Why does a black model appear when loading a GLTF model with materials?

I am currently attempting to load a 3D model in glb format. Below is the code snippet: Expected Outcome: Image Current Outcome: Image var renderer = new THREE.WebGLRenderer(); renderer.setSize(1000, 1000); renderer.setPixelRatio(window.devicePixelRati ...

In an effort to bring some flair to my React Hangman App, I am working on animating a collection

Challenge In my Hangman App, I am attempting to implement letter animations using react-spring. I want the letters from an array to fade in when loaded and fade out when removed by clicking on them. However, my previous attempts have resulted in laggy per ...

Tips for expanding the HTTP header size within Next.js

Can someone provide assistance for increasing the HTTP header size in my Next.js app? The current size is set at 16384 and I am unable to execute the necessary command node --max-HTTP-header-size=24576 server.js. Looking for help from someone with more e ...

Angular first renders a component before removing another one using ng-If

I have two components that are displayed one at a time using an ngif directive. <app-root> <first-Comp *ngIf="showFirst"></first-Comp> <second-Comp *ngIf="!showFirst"></second-Comp> </app-root> Here are some key ...

concerning the FoodMe initiative on GitHub

Hello, I have a query about the FoodMe Express example on GitHub: Code snippet: var express = require('express'); var fs = require('fs'); var open = require('open'); var RestaurantRecord = require('./model').Resta ...

Trouble with REGEX functionality in AngularJS?

I have a code snippet where I am attempting to validate an email and phone number. However, for some reason it is not functioning as expected. I am wondering if there is a specific file that needs to be included in my code. <html ng-app="myApp"> < ...

Ajax fails to provide a response

Hey there, I'm trying to save the response from an HTML page and store the content of that page. However, every time I try, I keep getting an error message. What am I doing incorrectly? <script type="text/jscript"> var page = ""; $.aj ...

Background PHP/JS authentication through HTTP

Recently, I developed a PHP website that includes embedded web-cam snapshots which refresh every 2 seconds using JavaScript. For the first camera, I can easily log in using URL parameters like this: cam1-url?usr=usr&pwd=pwd. However, the second camer ...

What is the most efficient way to switch between different views in AngularJS while preserving the data in the views'

Every time I navigate from page1.html to page2.html in AngularJS and then return back to page1.html, my page model data disappears. Can someone please help me figure out how to preserve this data? I've looked at other questions on this topic but coul ...

Issue with RouterLink not recognizing QueryParams

I have encountered an issue where dynamically generated URLs with queryParams inside [routerLink] are breaking routes. For example: this.url = '/question/ask?details=1' <a [routerLink]="url"> {{ data.name }}</a> Upon mouseover, the ...

What is the best way to create a list from a matrix using JavaScript?

I have an array structured as follows: const input_array= [ ["red", "green"], ["small", "medium"], ["x", "y", "z"] //... can have any number of rows added dynamically ...

Multiple requests were made by Ajax

I am facing an issue with my ajax code where it is being called multiple times. Below is my php code: (While loop extracts results from a database. For brevity, I have included just the modal and dropdown menu sections.) $rezPet = mysqli_query($kon, "SEL ...

Node.js delete operator not performing as anticipated

Within my node.js express application, I am fetching a user object from the database: const newUser = await User.create({ username, password, email, avatar, }) However, before sending back the response containing the user object, I ...

The function getoffer does not exist

I am encountering an issue with my factory declaration for Malls. It seems that I am getting the error message get offer is not a function! .controller('confirmCtrl', function($scope,Malls,$stateParams,$log) { $scope.offers = Malls.g ...