Pagination error: Cannot assign type 'void' to type 'PageEvent'

Trying to implement pagination with mat paginator without a table. Facing an issue with the error message "Type 'void' is not assignable to type 'PageEvent'" for my pagination method.

This is what I have:

HTML:

<mat-paginator 
  [length]="length"
  [pageSize]="pageSize"
  [pageSizeOptions]="pageSizeOptions"
  (page)="pageEvent = OnPageChange($event)"> //Error occurs here
</mat-paginator>

Typescript:

//Definitions
//Paginator
length = 100;
pageSize = 10;
pageSizeOptions: number[] = [5, 10, 25, 100];
// MatPaginator Output
pageEvent: PageEvent;
@ViewChild(MatPaginator) paginator: MatPaginator;

//Method for changing pages:
OnPageChange(event: PageEvent){
  let startIndex = event.pageIndex * event.pageSize;
  let endIndex = startIndex + event.pageSize;
  if(endIndex > this.length){
    endIndex = this.length;
  }
  this.pagedList = this.identFiltered.slice(startIndex, endIndex); //identFiltered is the Datasource
}

Seems like there might be an issue with the definition of pageEvent, need to investigate further

Answer №1

Let's take a closer look at the line causing the error:

(page)="pageEvent = OnPageChange($event)"

This code essentially means that whenever the page output emits an event, the return value of OnPageChange($event) is assigned to the pageEvent variable.

The problem you are encountering is that your function OnPageChange($event: PageEvent) does not have a return statement. Therefore, the function does not produce a return value and is considered void.

To resolve this issue, you can either modify OnPageChange to return $event so it aligns with

PageEvent</code, or refactor the assignment of the event to <code>pageEvent
within the function itself instead of in the HTML.

Answer №2

.ts

onPageUpdate(event: any) {
 console.log(JSON.stringify(event))
 this.initialPosition = event.position;
 this.totalItems = event.items;
}

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

Send a function as a property to a child functional component

I am attempting to pass a callback function to a functional component child in order to update the parent. However, I am encountering an error Uncaught TypeError: updateVal is not a function within the child component (I will add a comment to indicate wh ...

Having trouble accessing functions in Typescript when importing JavaScript files, although able to access them in HTML

Recently, I started incorporating TypeScript and React into my company's existing JavaScript code base. It has been a bit of a rollercoaster ride, as I'm sure many can relate to. After conquering major obstacles such as setting up webpack correc ...

Controlling the DOM with Angular 2

I am currently developing a website in Angular 2. I have successfully implemented a JQuery code that adds 2 input fields and 1 delete button when a custom button is clicked. When the user clicks on the delete button, it removes the inputs by manipulating t ...

I am struggling to understand the significance of the $ symbol in this particular context

I came across the following snippet in a book I've been reading: `images/${Date.now()}.jpg` The curly brackets used here signify 'out of string', but I'm unsure about the meaning of $... P.S. Honestly, I didn't want to ask a que ...

utilize switchMap to terminate an HTTP request within an ngrx effect

Behold the ngrx effect in question: @Effect() throwError$: Observable<Action> = this.actions$.pipe( ofType<notificationActions.ErrorThrow>( notificationActions.ActionTypes.ThrowError ), tap(() => { this.store.dispa ...

Execute the While loop covertly

I am working with a JavaScript application that is Angular-based. It runs a while loop when a user clicks a button and continues until a certain number is reached, at which point it ends. However, I am facing an issue where I cannot interact with other e ...

Encountering a blank page and slow loading when launching the VSCode debugger using VS Code version 1.76.1 and Chrome 111

Recently, I've encountered a problem with the VS Code debugger while trying to debug an Angular application. I created a new Angular app using the ng new command and made some changes to the ngOnInit function. When attempting to start the Chrome deb ...

Utilizing a combination of Ionic 3, Angular 4, and preloaded database functionality

Despite following multiple tutorials, I have been unable to successfully use a prepopulated SqlLite in Ionic 3 and Angular 4. I tried various plugins, all resulting in the same failure. Some of the plugins I attempted include: cordova-plugin-dbcopy, nativ ...

Is it possible to register multiple mini router apps using app.use() in MEAN?

Within my node/express app.js main file, I have established a mini-app router: var router = express.Router(); I pass this router to my controller functions and then export it. Finally, I register the router by using: app.use('/Link', router); ...

The API response could not be shown at this time

My API is sending a JSON response. I'm currently attempting to display it in one of my components to better understand how the data is being transferred and displayed for future development. Despite my component functioning correctly with other conte ...

Preventing a page refresh in Angular 2 on an ASPX page: Strategies for success

The use of the pagemethod is successful, but I encountered an issue with the webmethod causing the page to refresh, which defeats the purpose of using Angular 2. Is there a way to prevent the form from refreshing the page? index.aspx <body> &l ...

Difficulty with validating form groups in PrimeNG calendar

I am currently implementing validation for my form. The discount field must not be empty and its value should range from 0 to 100, while the time_from and time_to fields cannot be left empty. However, I am facing an issue with firing the validation process ...

Warning from Firebase CLI deployment: The Node.js 8 runtime has been marked as deprecated and is scheduled to be phased out by 2020-12-05

Attempting to deploy TypeScript onto my FCF isn't working as expected based on the documentation and official Firecasts video. When deploying the default code (helloworld) instead of TypeScript, it deploys a node.js file which is confusing. Below are ...

"Revolutionize your date and time selection with the dynamically updating columns

I am in need of a time selection feature where users can choose both hours and minutes. Each hour will have its own set of minutes, and the data structure should look like this: [ { hour: 1, minutes: [0, 15, 30], }, { hour: 4, minut ...

Effective approach for sharing information between components via a service with automatic updates

I am in the process of setting up a feature on a webpage that allows users to log in and view their account statistics. Two main components are involved: one for the login form and another for displaying stats. Although I could combine these into one compo ...

The issue with sorting in Angular 8 mat tables persists when dealing with multiple tables

As a newcomer to Angular, I am still learning and have encountered an issue with sorting in the mat table. I have multiple tables on one page, each separated by a mat tab. The problem is that sorting only works on the first table ("crane master list") in t ...

Extracting the name of a track from the /r/listenToThis subreddit for an IFTTT automation script

I have a list of songs gathered from this subreddit, presented in the following format: [ "Lophelia -- MYTCH [Acoustic Prog-Rock/Jazz] (2019)", "Julia Jacklin - Pressure to Party [Rock] (2019)", "The Homeless Gospel Choir - I'm Going Home [Folk ...

Calculating the minimum value of a number in Angular 8

I attempted to convert a decimal number to a whole number and encountered an issue. When using the angular pipe method {{myNumber | number: '1.0-0.'}}, it provides a rounded off value instead of the floor value. For example, with number = 3.8, ...

Exploring ways to expand the theme.mixins feature in MUI 5

Currently, I am in the process of updating Material UI from version 4 to 5 and encountering challenges with my existing theming. Since we are using typescript, it is important to include the appropriate types when extending themes. I intend to include th ...

Angular - Error: Object returned from response does not match the expected type of 'request?: HttpRequest<any>'

While working on implementing an AuthGuard in Angular, I encountered the following Error: Type 'typeof AuthServiceService' is not assignable to type '(request?: HttpRequest) => string | Promise'. Type 'typeof AuthServiceServic ...