Ionic 5 Virtual scroll leaves gaps where new items should be added

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

My goal is to display a list using ion-virtual-scroll, but I'm facing an issue where the new items are not showing up dynamically. Instead, they create blank spaces in the list. However, when I inspect the scroll, I can see that the new item is present with this CSS property: " transform: translate3d(0px, -9999px, 0px); "

   <ion-col size="12" size-sm="8" offset-sm="2" class="ion-text-center">
    <ion-virtual-scroll [items]="listedLoadedPlaces"

    >
      <ion-item

        [routerLink]="place.id"
        detail
        *virtualItem="let place"
      >
        <ion-thumbnail slot="start">
          <ion-img [src]="place.imageUrl"></ion-img>
        </ion-thumbnail>
        <ion-label>
          <h2>{{place.title}}</h2>
          <p>{{place.description}}</p>
        </ion-label>
      </ion-item>
    </ion-virtual-scroll>
  </ion-col>

Answer №1

For those facing a similar problem, consider using the checkRange and checkEnd APIs provided by the IonVirtualScroll. These functions mark either the last item or a specified item as dirty to trigger a fresh render of that particular item.

In an Ionic Angular project, the code will look something like this:

template: <ion-virtual-scroll #listedLoadedPlacesScroll [items]="listedLoadedPlaces">
           <ion-item........

component:
  @ViewChild('#listedLoadedPlacesScroll', null) listedLoadedPlacesScroll: IonVirtualScroll;

.....
functionToUpdateList(): void {
   this.listedLoadedPlaces.push(newItem);
   this.listedLoadedPlacesScroll.checkEnd();
}

If you need to update a specific item that is not the last one in the source data array, use the checkRange function:

template: <ion-item
        [routerLink]="place.id"
        detail
        *virtualItem="let place; let index = index;"
        (click)="doSomething(place, index)"
      >

component:
    doSomething(place, index): void {
      ..do something to place...
      this.listedLoadedPlacesScroll.checkRange(index, 1);
}

With checkRange, you can specify the index of the item to mark as dirty. The second parameter, len, determines how many items starting from the current index should be marked as dirty. For example, setting len = 3 marks the items at index, index + 1, and index + 2 as dirty for a fresh render.

In one scenario, I needed to update an item out of view when changing an item in view. To solve this, I marked all items as dirty using checkRange(0, items.length). It may not be elegant, but it gets the job done effectively now.

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

Controlling numerous websockets using React

I am currently developing a single-page application using React.js with a JSON RPC 2.0 backend API that relies on websockets. Managing multiple websocket connections simultaneously across different React.js components has been a challenge. Initially, I th ...

Restoring scroll position in Next.js when the page is reloaded

Problem Description I am facing an issue with the sticky header functionality I have implemented. It relies on a useEffect hook to monitor its scroll Y offset state. However, when I reload the page, it fails to detect the position until I manually scroll ...

Troubleshooting problems with permissions when using the AWS IAM assumeRole function with session

Within my aws-cdk application, I am working on setting up a lambda function to assume a specific role and obtain credentials through aws-sts. These credentials need to include a tenant_id tag. AWS CDK Code: Role to be assumed: const pdfUserRole = new Rol ...

Declaration of function with extra attributes

Is there a way to define a type for a function that includes additional properties like this? foo({ name: 'john' }) foo.type I tried the following solution, but TypeScript seems to think that foo only returns the function and cannot be called wi ...

What are some practical ways to employ optional chaining when working with arrays and functions?

I'm attempting to implement optional chaining with an array instead of an object but I'm unsure how to proceed: Here's my attempt at using it myArray.filter(x => x.testKey === myTestKey)?[0]. I am also experimenting with a similar approa ...

Retrieve the response status using a promise

There is a promise in my code that sometimes results in an error response (either 400 or 403, depending on the user). I am trying to handle this situation by catching the response and implementing a conditional logic to execute different functions based on ...

I'm looking for recommendations for an Ionic application template, can

I am interested in creating a mobile app using Ionic and AngularJS, similar to this one: Is it possible to achieve this with Ionic and Angular? If so, could you recommend a suitable template for me? Many thanks, ...

choose this.variable angular 12 in a dynamic manner

I am facing an issue with selecting pre-defined variables within a component. Here are the variables in question: public editable1: number = 0; public editable2: number = 0; public editable3: number = 0; public editable4: number = 0; public editable5: numb ...

What could be causing the Ioncol not triggering the Onclick event?

I am facing an issue where my onclick event is not working on an ion-col. The error message says that the method I call "is not defined at html element.onclick". Here is a snippet of my code: <ion-row style="width:100%; height:6%; border: 1px solid # ...

Achieving VS Code/typescript autocomplete functionality without the need to import the library

When a module (for example, moment.js, knockout, or big.js) is added with a <script> tag like this: <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"> </script> that defines a global property (for instance ...

The Vue API client fetches necessary information from the server and generates a .csv file with accurate headers and IDs. However, the data retrieved is incomplete

Check out my code repository here: https://github.com/d0uble-happiness/discogsCSV App.vue <template> <div> <button @click="downloadSampleInputFile">Download basic sample file</button> </div> < ...

Can inheritance pass constructor values in TypeScript/JavaScript?

I stumbled upon some code that seems confusing to me: I am still relatively new to TypeScript, but this doesn't seem like it should be functioning correctly: There are 2 classes involved in this scenario (pertaining to an automation framework, specif ...

Compiler raises concerns about potential undefined values in nested objects

In my code snippet, I am experiencing an issue with TypeScript when I try to access an object property after checking for its existence. The sample code can be found here (when strictNullChecks is enabled). 1. let boolVar: number | undefined; 2. 3. if ...

How to Validate Comma-Separated Email IDs Using Regex in Angular 5 Template?

I am currently working on a project utilizing Angular 5. Within this project, there is an input field designated for E-Mail IDs. The main goal I aimed to achieve was: To enable the user to input a maximum of 3 E-Mail IDs, with each E-Mail ID being subject ...

increase the selected date in an Angular datepicker by 10 days

I have a datepicker value in the following format: `Fri Mar 01 2021 00:00:00 GMT+0530 (India Standard Time)` My goal is to add 60 days to this date. After performing the addition, the updated value appears as: `Fri Apr 29 2021 00:00:00 GMT+0530 (India St ...

App is only being installed on devices running Nougat when using Ionic/Cordova

Recently, I delved into the world of ionic/cordova and am now testing my app on various Android platforms. In my config.xml file, I specified my minSdkVersion, targetSdk, and maxSdkVersion to be 19 (Kitkat), 19 (Kitkat), and 26 (Oreo) respectively. <pl ...

Why am I encountering numerous errors while attempting to install Juice Shop?

My attempt to install the juice shop app from GitHub resulted in 63 errors showing up after running the command npm install. [riki@anarchy]: ~/juiceShop/juice-shop>$ npm install (Various warnings and engine compatibility issues) Multiple vulnerabilit ...

Mastering Redux Toolkit - Ensuring Payload Type Verification is in Place when Invoking an Action Creator

I have been utilizing the redux toolkit for a while now and I appreciate how it helps in reducing redundant code. I am also interested in incorporating typescript, but I am facing challenges with getting the typechecking of an action payload to function pr ...

Is it possible for the binding model of Mat-Checkbox to be optional or null?

Greetings everyone! I am a newcomer to the world of Angular, where I have successfully created a dynamic list of checkboxes. However, I have encountered a challenge when trying to bind selected values from an API to these checkboxes. <div *ngFor="let b ...

I'm encountering an error after updating Angular/Typescript. The error message states that Type 'Observable<unknown>' cannot be assigned to type 'Observable<ProcResult[]>'

My Angular database service has been set up in this manner since Angular 6, and I am now encountering this error for the first time. I have a standard HTTP POST method that returns a result. In case of an error, I want to handle it appropriately. The most ...