*ngIf - use a property that requires multiple settings

In my Angular 6 project, I have implemented a WIJMO grid in the template that pulls data from a database table. Each row in the grid should display either a delete button or an un-delete button based on the condition specified using *ngIf else:

<wj-flex-grid>
        <wj-flex-grid-column [header]="'ID'" [binding]="'ID'"></wj-flex-grid-column>
        <wj-flex-grid-column [header]="'deleted'" [binding]="'deleted'"></wj-flex-grid-column>

        <wj-flex-grid-column [header]="'delete/undelete buttons'" [binding]="'buttons'">

            <button *ngIf="!deleted; else unDeleted">Delete</button>
                <ng-template #unDeleted>
                    <button>Un-Delete</button>
                </ng-template>

        </wj-flex-grid-column>
</wj-flex-grid>

The challenge I am facing is with handling the deleted property in the .ts file. I need to set and access this property multiple times for each row in the table, but it seems that *ngIf only recognizes it after its last definition. In my TypeScript code, I use a for loop to iterate over every data item and set the deleted property to true or false based on a column value retrieved from the database. If the last row in the grid is marked as deleted, then all buttons will display undelete, and vice versa:

export class myComponent extends WjFlexGridParent implements OnChanges, OnInit {

/////// deleted property //////
////// *ngIf only likes to read this after the last time it is defined //////
    public deleted: boolean;

       loadData() {        
        this.myDatabaseService.get(this.myApiPath)
            .subscribe
            (
                data => {

                  this.setCollectionView(data);
                  this.m_collectionView.trackChanges = true;

                    /////// delete/undelete logic //////
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].deleted == null) {
                            this.deleted = false;
                        } 
                        else if (data[i].deleted !== null) {
                            this.deleted = true;
                        }
                    }
                },
                errorCode => {
                    // this.statusMessage = "";
                }
            );
    }

}

Is there a way to make *ngIf recognize this property after each update?

Answer №1

Successfully resolved - a simple adjustment in the HTML was all that was required, eliminating the need for the for loop in the .ts file.

A big thank you to Jeto and Aragorn in the comments section for steering me in the right direction, and special gratitude to Sharad from GrapeCity (WIJMO) support for providing the ultimate solution.

To all fellow WIJMO FlexGrid enthusiasts, I connected *ngIf to my items.deleted attribute in the html (similar to Aragorn's suggestion:

'It looks like the data you are getting already has the deleted attribute, just use that.'
).

HTML:

    <wj-flex-grid-column [header]="'delete/undelete buttons'" [binding]="'buttons'">

        //// set *ngIf="!item.deleted" ////
        <button *ngIf="!item.deleted; else unDeleted">Delete</button>
            <ng-template #unDeleted>
                <button>Un-Delete</button>
            </ng-template>

    </wj-flex-grid-column>

TS:

loadData() {
    var self = this;

    this.myDatabaseService.get(this.myApiPath)
        .subscribe
        (
            data => {
                this.setCollectionView(data);
                this.m_collectionView.trackChanges = true;

                //// for loop was not needed ////
            },
            errorCode => {
                // this.statusMessage = "";
            }
        );
}

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

When a MatFormFieldControl both implements ControlValueAccessor and Validator, it can potentially lead to a cyclic

I am attempting to develop a custom form control by implementing MatFormFieldControl, ControlValueAccessor, and Validator interfaces. However, I encounter issues when including NG_VALUE_ACCESSOR or NG_VALIDATORS. @Component({ selector: 'fe-phone-n ...

Tips for updating the value of a dynamically fetched field in a Firestore document using Cloud Functions

I have a simple question: How can I update a field in a firestore document when the field name is only known dynamically through a variable? const myCounterName = "exampleName"; const docRef = admin.firestore().collection("metadata").do ...

What is the best way to modify the quantity property of a cart item object within the framework of this Angular 13 online shopping application?

I am currently developing an e-commerce app with a front-end built using Angular 13. The following code snippet is designed to calculate the total price of items in the shopping cart: import { Component, OnInit } from '@angular/core'; @Componen ...

Creating a generic class for third-party API requests in NestJS

I'm working on a NestJS application that involves making third-party API requests. Every function requires the same code to retrieve data, causing repetition. How can I streamline this process by creating a common class for handling GET or POST reque ...

Error Encountered: The Angular Compiler demands TypeScript version to be greater than or equal to 3.6.4 and less than 3.9.0, however it has detected version 3.9.7

I have successfully Dockerized Angular 9. It runs smoothly in development with ng serve. Even when I run the Docker container on my local development environment (macOS), everything works as expected. However, when attempting to build Angular in a Docker ...

What is the process for declaring a set in typescript?

In the documentation on basic types for Typescript, it explains using Arrays as a primitive type I am interested in the syntax: const numbers: string[] = [] How can I achieve the same with a set? ...

Dynamic addition of a Javascript script to a component leads to malfunctioning

I am facing an issue with a dynamically created component where I added the script tag, but unfortunately, the code inside is not functioning properly. Despite trying multiple solutions, none seem to work for me. Could you please review the code? https:// ...

Remove the swipe functionality in Ionic 3

I am trying to implement an ionic delete swipe gesture on myPage.html, but for some reason it is not working properly. **This is my home page ( i called it myPage.html)** <ion-header> <ion-navbar color="secondary"> <ion-ti ...

How can we determine the type of InertiaFormProps in Laravel Inertia?

I have a webpage with a useForm hook implemented, featuring a multi-step form split into separate components. Here's an example: export default function create(){ const form = useForm({ name: '', content: &ap ...

Angular menu with nested dropdowns

Is it possible to create a nested select menu similar to the example select screenshot menu below, using my mat-select? When a user selects an item from the REPM mat select, it should display the Master Broker Company menu on the right side. Thanks. #exam ...

Exploring the capabilities of multiple router-outlets and submodules within Angular 5

Despite my extensive review of the documentation and other questions, I am still struggling to find a suitable solution for my project. I decided to steer clear of the named router-outlets option as it results in a less clean URL. It seems that many of the ...

TypeScript error TS2503: Namespace 'browser' not found

I encountered a problem while trying to compile TypeScript, resulting in the following error related to @cliqz/adblocker: node_modules/@cliqz/adblocker/dist/commonjs/request.d.ts:12:37 - error TS2503: Cannot find namespace 'browser'. 12 export t ...

Tips for simulating a callback function to evaluate a promise using jasmine in an Angular 8 environment

I'm struggling with figuring out how to mock a callback function and the function that calls it. The specific function I'm trying to mock is "listenOnAMessageWithCallBack" from a service I'm injecting, along with the callback function it tri ...

Using Unique Typeface in Ionic Framework 3

I am currently utilizing Ionic3: Your device information: Cordova CLI: 6.4.0 Ionic Framework Version: 3.0.1 Ionic CLI Version: 2.1.18 Ionic App Lib Version: 2.1.9 Ionic App Scripts Version: 1.3.0 ios-deploy version: Not installed ios-sim version: Not in ...

Finding the Name of the Active Tab in Ionic 3 app.module

My app features a side menu with tabs, following a structure similar to . The root of my app is Menu.ts, where I load the Tabs Pages. In the constructor of app.component, I am trying to get the current active tab using various methods like: this.nav.get ...

Retrieve the text content from the HTML document

I'm facing a beginner's challenge. I have a div element and I want to extract the URL from the data-element attribute into a .json file Is there a way to do this? <div content="" id="preview" data-element="http://thereislink" class="sample ...

Create a function that takes in a function as an argument and generates a new function that is identical to the original function, except it has one

I want to establish a function called outerFn that takes another function (referred to as innerFn) as a parameter. The innerFn function should expect an object argument with a mandatory user parameter. The main purpose of outerFn is to return a new functio ...

Improving my solution with PrimeNG in Angular2 - fixing the undefined tag error

After working with Angular for just three days, I successfully set up a login page dashboard using a web API solution. Everything was working great until I encountered an issue when trying to load the PrimeNG DataTableModule into my components. After searc ...

Troubleshooting the carouselExampleIndicators issue in Angular 4

I successfully implemented a carousel on my webpage using the code below: <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel"> <ol class="carousel-indicators"> <li data-target="#carouselExampleIndicat ...

Tips for enabling custom tags in the tinyMce editor

<textarea><div style="margin-top: 15px;"> <div class="dropdown "> <p> hello my name is <Enter Your Name> </p> <p> hehe</p> </div> </div> ...