Tips for ensuring your controls function properly and seamlessly when switching to another page

I utilized the instructions from this post to implement a slider. However, I encountered an issue with the controller when navigating to subsequent pages. While the controller functions correctly on the initial page, it duplicates the same values on the following pages. For example, if "Product 1" is active in the first line of page 1, it appears as active even on the second page, despite not being so. This pattern repeats across all pages. I suspect the problem lies within my ts code. DEMO Kindly review my code below:

  populateFormGPS() {
    this.ws.getAllGpss().subscribe(
      gpss => {
        this.gpss = gpss
        console.log(gpss)// all value arrive ok
        let controls = {
          'gps_id': new FormControl('', Validators.required)
        };

        for (let i = 0; i < this.gpss.length; i++) {
          controls['gps_actived-' + i] = new FormControl(this.gpss[i].gps_actived === 1, Validators.required)
        }
        this.acitveGPSForm = new FormGroup(controls);
        this.patchForm();
      }
    )
  }

  patchForm() {
    this.acitveGPSForm.patchValue({
      gps_id: this.gpss.map(x => x.gps_id),
    });
  }

Here is the HTML code snippet:

<div class="row">
  <div class="col s12 m2">
    <label class="col s12 label-control" style="padding: 0px">Rows on page</label>
    <select class="form-control input-sm" [(ngModel)]="rowsOnPage">
        <option [ngValue]="5">5</option>
        <option [ngValue]="10">10</option>
      <option [ngValue]="20">20</option>
    </select>
  </div>
   ...............
  .......... //Code truncated for brevity
 .............. 
</table>

If you have any insights on how to address this challenge, your assistance would be highly appreciated.

Thank you.

Answer №1

The reason for this issue is that your control names are tied to local indexes rather than IDs. This means that when you filter your rows to show n results per page, the row labeled as n will actually have an index of 0, causing it to share the same form control as the actual row 0.

To fix this problem, make the following changes in app.component.ts:

controls['active-'+i] = ...

Replace the above line with:

controls['active-'+this.homeboxsp[i].homeboxpackage_id] = ...

Next, in app.component.html, locate:

formControlName="active-{{i}}"

And replace it with:

formControlName="active-{{item.homeboxpackage_id}}" 

For a modified version of the code, visit this link.

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

Integrating modules in Angular 2

Exploring the functionalities of Angularjs 2.0, I encountered an issue when attempting to inject a service into a class. Below is the code snippet that's causing trouble: import {Component, View, bootstrap, NgFor, HttpService, Promise} from 'ang ...

Utilizing TypeScript's discriminated unions for function parameters

The function's second argument type is determined by the string value of the first argument. Here is an example of what I am trying to achieve: async action (name: 'create', args: { table: string, object: StorageObject }): Promise<Sto ...

Retrieve the values of a dynamic JSON object and convert them into a comma-separated string using Typescript

I recently encountered a dynamic JSON object: { "SMSPhone": [ "SMS Phone Number is not valid" ], "VoicePhone": [ "Voice Phone Number is not valid" ] } My goal is to extract the va ...

Retrieve a limited set of 8 results from Algolia using Angular

I am attempting to fetch 8 records from algolia using the angular-instantsearch package. This is what I have accomplished so far: .html file - <ais-instantsearch [config]="products"> <ais-hits> <ng-template let-hits= ...

Determine the data type of a property by referencing the data type of another property within an array of objects using TypeScript

I am working with a specific type of interface called Route that consists of name and path properties. interface Route { name: string, path: string } const routes = [ { name: "first", path: "/first" }, ...

Having trouble receiving a blob response using HttpClient POST request in Angular 9?

I'm encountering an issue while attempting to receive a zip file as a blob from an HTTP POST request. However, the resolved post method overload is not what I expected. const options = { responseType: 'blob' as const }; Observable<Blob ...

Establish a connection between two JSON files using the WordPress REST API

I have developed an app using ionic 2 that revolves around quotes. My goal is to manage these quotes (along with authors, categories, etc) using Wordpress and its REST API. Initially, I utilized normal posts for this purpose, but now I am exploring custom ...

Tips for Decreasing Query Time with MatTable and MatTableDataSource

When working with my firestore database, I am trying to query documents and display them while also calculating the total value of a specific column (promiAmount). I have successfully displayed the values in a mat table, but I'm struggling to calcula ...

Top Tips for Layering Components in Angular 5

Just starting out with Angular, currently in the process of creating a sample website. Created a cover page component named <app-coverpage> and have additional components that I want to overlap the cover page as part of the background effect. All c ...

Deploying an Angular application on Firebase is a great way to

I am facing an issue with hosting my Angular(5) project on Firebase. Although I have successfully deployed the application, when I access the project URL, it displays a default Firebase hosting screen instead of my actual Angular project. https://i.stack. ...

Angular CLI - managing separate development and production assets

Is there a way to switch to a different set of assets when using the --prod flag with ng build? In my scenario, I have multiple projects that share a common set of assets in a designated folder. Each project's configuration in angular.json looks simi ...

How can I ensure a module in Angular module federation v14 is only loaded in either the parent or child app, but not both?

I am currently utilizing Angular 14 along with module federation in my project. Within my child application, I have the following configuration: module.exports = withModuleFederationPlugin({ name: 'childapp', exposes: { './app1&apos ...

What is the process for deploying a .NET Core + Angular single page application to multiple environments?

After utilizing the Angular project template with ASP.NET Core in Visual Studio 2019, I developed an application that includes multiple environments: DEV, STG, and Production. Each environment corresponds to specific "appsettings.json" files for the .NET p ...

angular use ngFor directive to restrict the number of rows displayed in a table

In my angular 6 project, I am trying to limit the display to just five rows in a table using ngFor. My current code snippet is as follows: <tbody> <tr *ngFor="let item of routines; let i = index"> <td style="display: none"> ...

The error message "Property 'list' is not found on type 'void' React - Material UI" indicates that the 'list' property is not recognized

Currently, I am facing an issue while working with Material UI and React Typescript. The error message I receive is "Property 'list' does not exist on type 'void'" when attempting to use makeStyles and createStyles to remove padding. It ...

The compatibility of return value types between the constructor signature and the call signature interface is not maintained when they are used together

I'm new to TypeScript and I'm struggling to figure out why I'm getting a type error in the code below. Can someone help me identify what's wrong? interface CallOrConstruct { new (value: string): Person (value: number): number } cla ...

Encountering a 404 error while attempting to test a contact form on a Next.js website using a local server

Trying to test a contact form in Next.js where the data is logged but not sent to the API due to an error. "POST http://localhost:3000/app/(pages)/api/contact/route.tsx 404 (Not Found)" Troubleshooting to identify the issue. [directory setup] ...

Utilizing TypeScript's conditional return type with an object as a parameter, and incorporating default values

Is it possible to create a function where the return type is determined by a string, with some additional complexities involved? I'm looking to achieve the following: The parameter is contained within an object The parameter is optional The object it ...

Creating a tsconfig.json file that aligns perfectly with your package.json and tsc command: a step-by-step

I've chosen to use TodoMvc Typescript-Angular as the starting point for my AngularJS project. Everything is working smoothly so far. Here's a breakdown of what I can do: To manage all dependencies, I simply run npm install or npm update based o ...

"Encountered an error: Unable to interpret URL from (URL).vercel.app/api/getMessages" while deploying Next.js 13 using TypeScript on Vercel

Hello to all members of the StackOverflow Community: I am encountering an error message stating "TypeError: Failed to parse URL from next-chat-lenx51hr5-gregory-buffard.vercel.app/api/getMessages" while attempting to build my Next.js 13 application using T ...