Encountering an issue: JwPagination in angular 9 throws an error of "Cannot read property 'currentValue' of undefined"

Currently using Jw pagination with a page size that changes on 5, 10, or 15 using a dropdown. The Angular version being used is angular 9.

The HTML code snippet for this functionality looks like:

<div class="col-md-6">
            <div class="row">
              <div class="col-md-3">
                  <span class="mr-2">Page Size:</span>
              </div>
              <div class="col-md-9">
                  <select (change)="updatePageSize($event.target.value)"
                  class="control-form-sm" style="width:20%;">
                <option selected>5</option>
                <option >10</option>
                <option>15</option>
                <option>20</option>
                </select>
              </div>
            </div>
          </div>

https://i.sstatic.net/cQlvM.png

Upon changing the dropdown to 10 or 15, the following function in the component.ts file gets called:

updatePageSize(pageNumber:number){
  console.log(pageNumber);
    this.pageSize=pageNumber;
    this.listBooks();
}

https://i.sstatic.net/XBJwQ.png

Even though the value is logged successfully in the console, an error "

Cannot read property 'currentValue' of undefined
" is encountered. This error occurs even though the pagination continues to work as expected when the dropdown option is changed.

The complete content of my component.ts file can be seen below:

import { Component, OnInit } from '@angular/core';
import { Book } from 'src/app/common/book';
import { ActivatedRoute } from '@angular/router';
import { BookService } from 'src/app/service/book.service';

@Component({
  selector: 'app-book-list',
 /* templateUrl: './book-list.component.html', */
  templateUrl: './book-grid.component.html',
  styleUrls: ['./book-list.component.css']
})
export class BookListComponent implements OnInit {
  books :Book[];
  currentCategoryId:number;
  searchMode:boolean;

  //for pagination
  pageOfItems:Array<Book>;
  pageSize:number=6;
  
  constructor(private bookService:BookService,private _activatedRoute:ActivatedRoute) { 

  }

  ngOnInit(): void {
   // this.listBooks();
   this._activatedRoute.paramMap.subscribe(() => {
        this.listBooks();
      })

   
  }

//pagination
  pageClick(pageOfItems:Array<Book>){
  //  console.log(pageOfItems);
      //update the current page of items
      this.pageOfItems=pageOfItems;
  }

//updating page size
updatePageSize(pageNumber:number){
  console.log(pageNumber);
    this.pageSize=pageNumber;
    this.listBooks();
}


  listBooks(){
    this.searchMode =this._activatedRoute.snapshot.paramMap.has('keyword');
    if(this.searchMode){
      //do search books
      this.handleSearchBooks();
    }
    else{
      //display books based on category
      this.handleListBooks();
    }
  }

  
  handleListBooks(){
        
    const hasCategoryId:boolean=  this._activatedRoute.snapshot.paramMap.has('id');

    if(hasCategoryId){
     this.currentCategoryId= +this._activatedRoute.snapshot.paramMap.get('id');
    }
    else{
      this.currentCategoryId=1;
    }

    this.bookService.getBooks(this.currentCategoryId).subscribe(
      data  => {
       // console.log(data);
       this.books=data;
      // this.items=this.books;
      }
    )
  }

  handleSearchBooks(){
       const keyword:string= this._activatedRoute.snapshot.paramMap.get('keyword');
       this.bookService.searchBooks(keyword).subscribe(
         data =>{
         //console.log(data);
         this.books=data;
       //  this.items=this.books;
       })
  }
}

Answer №1

Please ensure that you include a value in your options (value="pageNumber"), as currently the value is undefined. Consider implementing the following structure:

<div class="col-md-6">
            <div class="row">
              <div class="col-md-3">
                  <span class="mr-2">Page Size:</span>
              </div>
              <div class="col-md-9">
                  <select (change)="updatePageSize($event.target.value)"
                  class="control-form-sm" style="width:20%;">
                <option value="5" selected>5</option>
                <option value="10">10</option>
                <option value="15">15</option>
                <option value="20">20</option>
                </select>
              </div>
            </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

Unable to bring in the directive from the Angular library

I've created this custom Directive within my library: @Directive({ selector: '[layoutHeaderTitle]', standalone: true }) export class HeaderDirective { constructor( readonly tpl: TemplateRef<any>, private re ...

What is the reason for the regeneration of the 2D array?

I have a method called generateWeights() that retrieves random values in an array; And a method named learn() that calls the changeWeights() method to alter the values in the array. Expected: Prior to invoking the learn() method, I anticipate receiving an ...

Updating and Preserving Content in Angular

I've encountered an issue while working on a code that allows users to edit and save a paragraph on the screen. Currently, the editing functionality is working fine and the save() operation is successful. However, after saving, the edited paragraph do ...

"Learn the process of extracting information from a database and exporting it to a CSV file with Angular 2

Currently, I am facing an issue where I need to retrieve data from a database and export it to a CSV file. While I am able to fetch all the data successfully, I am encountering difficulty when trying to fetch data that is in object format as it shows up as ...

Inversify's Http Context consistently remains void of any content

Hello there, I'm in need of assistance with building an API using inversify and inversify-express-utils. Everything seems to be working fine with my controllers and the API so far, except for one issue. When trying to access the httpContext property i ...

How can the dependencies object be extracted from the package.json file in an Angular project?

Scenario: I am managing multiple Angular applications within the same project. Whenever I need to upgrade an npm package, I find myself having to manually update the package.json files in each application. While I attempted a mono repo approach, it did not ...

Angular - issue with subject observable not functioning as expected

In my service SelectorService, I have created a subject and an observable. private eventEligibleApiStatus = new Subject<any>(); public eventEligibleApiStatusUpdated$ = this.eventEligibleApiStatus.asObservable(); I also have a method in which I use s ...

Combining TypeScript and JavaScript for efficient mixins

I came across an article on MDN discussing the usage and creation of mix-ins (link). Intrigued, I decided to try implementing it in TypeScript: type Constructor = new (...args: any) => any; function nameMixin(Base: Constructor) { return class extends ...

Creating Custom Form Control with Custom Validation in Angular 6

I designed the app-input component for displaying an input textbox. I am struggling to implement custom form validation. Can you offer any suggestions? ...

Create a Typescript type extension specifically designed for objects with nested arrays of objects

After stumbling upon this inquiry, I am eager to adjust my code in a similar fashion, utilizing typescript and a more intricate object. Consider the type below: export type ImportationType = { commercialImportation: boolean dateToManufacturer: string ...

Trouble Ensues When Implementing Angular 9 Template Driven Form Validation and Setting Focus to the Next Field Simultaneously

Angular 9 is used for my template-driven login form. <form name="login" #loginForm="ngForm" (ngSubmit)="loginForm.form.valid && login()" *ngIf="loginPage" novalidate> <ion-item> &l ...

Angular 2 FormArray validation based on conditions

My goal is to apply validation rules based on changes in one field to another field within a formGroup that is nested inside a FormArray containing multiple instances of this group. Specifically, when the date field is modified, I need the Reason for Chang ...

The art of binding styles and classes in code

What is the advantage of using style binding and class binding in Angular compared to directly using HTML style and traditional class attributes? <img src="link_img" [style.width]="imgWidth"> over <img src="link_img" width="200"> Looking fo ...

Incorrect Angular Routing Component OpeningI am experiencing an issue where

I am facing an issue with lazy loading a module, where it is not correctly displaying the desired component. Even though the route seems correct, it shows a different component instead. https://i.sstatic.net/v4oAB.png Despite specifying the path for "Pus ...

Leverage the capabilities of one service within another service

I have been working on enhancing the functionality of Http so that when a user encounters a 403 error, their user information is removed and they are redirected to the login page. I have shared an example of AuthHttp below for reference. @Injectable() ...

Error in MEAN Stack: Unable to access the property 'companyTitle' because it is undefined

I have established a MongoDB collection named joblist in my database. Additionally, I have developed a DB schema known as jobList.js. var mongoose = require('mongoose'); const joblistSchema = mongoose.Schema({ companyTitle: String, jobT ...

Authenticate using oidc-client-js and automatically redirect to a different web application

Having some trouble with the authentication process. I'm not entirely convinced it's well thought out. https://i.stack.imgur.com/62ju6.png I've got an Angular app specifically for user login and registration. When a user wants to log in 1, ...

Ways to change information in a class variable using Angular

A sample registration application has been created and I am looking to store the data in a class data model. Below is the class model that has been created: export class Model { name: string; tableData: any[]; constructor() { this.tableData = [ ...

Unlocking new perspectives with a click

Currently exploring Angular development, I have encountered a question here but couldn't find the solution I was looking for. I am seeking suggestions and ideas on how to approach this issue. Essentially, my HTML includes buttons like the ones shown ...

Angular: sending the user input to the component

I'm trying to link the value of an HTML input field to a variable 'numToAdd' in my component, and then increment the 'numToAdd' variable by 1. However, I'm facing difficulties passing the input value to the component variable. ...