"Optimizing Pagination in AngularJS: A Guide to Fixing

Can someone please help me with the code below? I am having an issue where my page 1 is not displaying any data and it only starts showing data from page 2. I've been trying to solve this for the past 3 hours with no success. I am new to angularjs and would greatly appreciate any assistance. Here is the code:

/HTML

 <table class="table table-hover" aria-describedby="dataTables-example_info" role="grid" class="table table-striped table-bordered table-hover dataTable no-footer" id="dataTables-example">
       <thead>
       <tr role="row">

   <th style="width: 360px;" colspan="1" rowspan="1">Class</th>
                                                    <th style="width: 360px;" colspan="1" rowspan="1">Section</th>
                                                    <th style="width: 260px;" colspan="1" rowspan="1">Edit Subjects</th>

                                                </tr>
                                            </thead>
                                            <tbody>
                                                <tr ng-repeat="info in data.slice(((currentPage-1)*itemsPerPage), ((currentPage)*itemsPerPage))">
                                                   <td>{{info.class_name}}</td>
                                                    <td>{{info.section_name}}</td>                                                   

                                                   <td> <button ng-click="moreinformation(info)" type="button" class="btn btn-info btn-flat"><i class="fa fa-pencil"></i></td>


                                                </tr>
                                            </tbody>
                                            </table>
                                            <ul class="pagination"> 
             <li class="paginate_button next" aria-controls="dataTables-example" tabindex="0" id="dataTables-example_next">
                            <a  ng-click="prevPage()">« Prev</a>
                        </li>
            <li  ng-repeat="n in range(totalItems)"
                            ng-class="{active: n == currentPage}"
                            ng-click="setPage()">
                         <a href ng-bind="n + 1"></a>
                        </li>

             <li class="paginate_button next" aria-controls="dataTables-example" tabindex="0" id="dataTables-example_next">
                            <a  ng-click="nextPage()">Next »</a>
                        </li>
       </ul>

//JS CODE...

 $scope.itemsPerPage =10;      
     $scope.currentPage = 0;
      $scope.totalItems = $scope.data.length / 10 ;
           $scope.data = [{ "class_name": "CLASS1", "section_name":"A" }, { "class_name": "CLASS2", "section_name": "A" }];
       $scope.prevPage = function () {
                            if ($scope.currentPage > 0) {
                                $scope.currentPage--;
                            }
                        };
                        $scope.nextPage = function () {
                            console.log($scope.totalItems);
                            if ($scope.currentPage < $scope.totalItems - 1) {
                                $scope.currentPage++;
                            }
                        };        
           $scope.setPage = function () {
                                console.log(this.n);
                                if (this.n == 0)
                                {
                                    $scope.currentPage++;
                                }
                                else
                                {
                                    $scope.currentPage = this.n;
                                }

                            };

                            $scope.range = function (start, end) {
                                var ret = [];
                                if (!end) {
                                    end = start;
                                    start = 0;
                                }
                                for (var i = start; i < end; i++) {
                                    ret.push(i);
                                }

                                return ret;
                            };

https://i.sstatic.net/Cq0RX.jpg

https://i.sstatic.net/jvgU1.jpg

Answer №1

Every time you call your <code>updatePage
function, the value of this.n remains the same, regardless of whether you pass in a new value like n+1 to the <a> element. This new value only affects the inner text of the <a> element.

To address this issue, you can modify your updatePage function to require a specific value, like so:

ng-click="updatePage(n + 1)"

Then, in your JavaScript code, you can update the function as follows:

$scope.updatePage = function (n) {
    $scope.currentPage = n;
};

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

Guide to displaying a table enclosed in a div based on a selected value in a select dropdown?

I am attempting to retrieve the table options using the select question-picker: Here is what I have tried: $(document).ready(function() { var question_container = $('.question-picker option[value="1"]').parent(); console.log(question_cont ...

Using Angular select asynchronously within a custom directive

Despite my efforts, I am struggling to get an angular select with async to work properly. It seems to be mostly working, but not entirely. Consider the controller below: $scope.stuff = {}; $scope.stuff.blah = "SOME_KEY"; External.list().then( function ...

The integration of a Bootstrap modal within the side bar's rendering

Struggling with a modal appearing inside my side div and can't find any solutions in the bootstrap5 documentation or online forums. https://i.stack.imgur.com/gHsBi.png I just want the modal to display centered on the page with the background fade ef ...

The Angular.js UIBootstarp Timepicker is displaying the "ng-invalid" class when used with an input type of "number"

Currently, I am working on incorporating a time picker using UIBootstrap and angular.js. By default, the timepicker utilizes {{input type="text}} for capturing hours and minutes. However, since I intend to use this feature on mobile devices, I need to di ...

Develop an innovative showcase page showcasing 151 individual profiles with React Router

I am new to using React Router and feeling a bit confused about how to proceed. On my homepage, I have 151 unique monster thumbnails. When a user clicks on a thumbnail, they should be directed to the specific monster's 'show page'. Currently ...

Changing the value of one particular key within an object during a reduce operation will impact all other keys within the object as well

I'm completely lost here. It seems like there's a reference issue causing all data properties to be overwritten with the value of the last row. I need help figuring out how to iterate over a list of objects, using specific keys to assign data to ...

Encountering a problem with Node.js because of a SyntaxError: Receiving an

Recently, I decided to dive into learning node.js and attempted something very basic: displaying a "Hello World" message from the server. The code snippet I used (taken directly from a book) is as follows: var http = require("http"); http.createServer(fu ...

JavaScript, XML, and PHP all support the use of HTML entities within their code

Having some trouble as a newbie again))) Can you please help me out, guys? I'm having an XML file with the following data: <Page> <Content>&lt;p&gt;Article content&lt;/p&gt;&#13; &#13; &lt;h1 style="font-style ...

Obtain the CSRF token from a webpage using JavaScript

I am currently working on a project where I need to retrieve the csrf token from a web page built with Django using javascript. The structure of my HTML template is as follows: <div class = "debugging"> <p id = "csrf">{% csrf_token %}</p ...

Leverage JSON data to generate individual elements, rows, and columns for every object within the array

Just starting out with JavaScript and struggling a bit. We need to fetch data from this URL: and then manipulate the data to create new rows (tr) and columns (td) for each game without altering the HTML file. The desired outcome should resemble this: http ...

What is causing my Vue leave transition to malfunction?

Currently, I am utilizing vee validate for my form validation. Specifically, I have implemented vue transition to handle the display of validation errors in the following manner: <input type="text" name="name" v-validate="'required'"> < ...

I am encountering an issue where body-parser is not functioning properly with typescript. Whenever I make a request, the request.body is returning as undefined

Below is the code snippet for my Express application using TypeScript version 3.7.4: import bodyParser from "body-parser"; import config from "config"; import cookieParser from "cookie-parser"; import express from "express"; import mongoose from "mongoose ...

Encountering an Issue with NextJS on GAE: EROFS Error indicating a read-only file system

Trying to deploy a customized Next.js application into Google App Engine has hit a snag. The project runs smoothly locally and on Google Cloud Platform CLI, but upon successful deployment using gcloud app deploy, an error arises when opening the app. 2020 ...

Adjusting the sensitivity of mousewheel and trackpad using JavaScript

I am currently utilizing CSS3 to smoothly move a div up and down based on the direction of scrolling. By monitoring the mouse wheel event, I can detect when a user scrolls down and trigger a CSS3 transition to slide the div up. However, I am encountering a ...

Exploring ES6 Property Assignment

After researching, I've discovered that ES6 does not support setting properties of a class and returning that class. class MyClass { constructor() { this.x = 0; this.y = 0; } update(value) { // logic this.y ...

Creating dynamic and engaging animations for components using ReactCSSTransitionGroup

I'm currently attempting to add animation to a modal that appears when a button is clicked using ReactCSSTransitionGroup. The modal is showing up on button click, however, there is no transition effect. My render method is set up like this: render() ...

Using ReactJS to send formData to an Express API and retrieving a JSON response

Attempting to have the ReactJS frontend send a username and password from a form to my express API via a proxy, with the intention of having the API return a JSON file containing a user id. While the proxy connection is working as expected, the issue arise ...

What is the best way to implement promise function in a JavaScript functional method such as forEach or reduce?

I have implemented a promise function in the following way: // WORK let res = {approveList: [], rejectList: [], errorId: rv.errorId, errorDesc: rv.errorDesc}; for (let i = 0; i < rv.copyDetailList.length; i ++) { const item = rv.copyDetailList[i]; ...

Challenge: Issue with ng-required in Angular when using a custom directive

I am currently utilizing Angularjs version 1.5 for input validation within my form. The use of ng-required is essential for validating all required inputs. Nevertheless, I am encountering an issue with a custom directive that generates a combo box. This ...

Converting JSON arrays to integers from strings in JavaScript: A step-by-step guide

When my application receives a Json string from the server (written in Java), I am faced with an issue when retrieving the data in JavaScript. The current format of the data looks like this: var data = [{"value":"3","label": "17 hr"}, {"value":"2", "labe ...