Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Angular – displaying text based on comparing with array values in template

I have two arrays in my .ts file which were formed based on specific condition matching

this.successIds = [1234, 2345, 3456];
this.failIds = [123, 234, 345];

In my HTML file, I have a grid column where I need to compare it’s Id in each row with above Ids from the arrays and show the respective text

  <kendo-grid-column *ngIf="isEditClicked" field="status" title="STATUS" width="70">        
    <ng-template kendoGridEditTemplate let-dataItem="dataItem" let-column="column">         
      <span *ngIf="dataItem.attributeId == <if found in successIds array>">Success </span>
      <span *ngIf="dataItem.attributeId == <if found in failIds array>">Fail </span>
    </ng-template>        
  </kendo-grid-column>

How can I do this comparison in the template and show the respective message. Please suggest. Thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could do the following which would be very slow:

<kendo-grid-column *ngIf="isEditClicked" field="status" title="STATUS" width="70">
  <ng-template kendoGridEditTemplate let-dataItem="dataItem" let-column="column">
    <span *ngIf="isInSuccess(dataItem.attributeId)">Success </span>
    <span *ngIf="isInFail(dataItem.attributeId)">Fail </span>
  </ng-template>
</kendo-grid-column>


class YourComponent {
  successIds = [1234, 2345, 3456];
  failIds = [123, 234, 345];
  
  isInSuccess(value: number): boolean {
    return this.successIds.includes(value);
  }
  
  isInFail(value: number): boolean {
    return this.failIds.includes(value);
  }
}

The following will be a lot better in terms of performance:

class Foobar {
  // successIds = [1234, 2345, 3456];
  // failIds = [123, 234, 345];

  // Lookup-object that stores the id as key and `true` (=success) or `false` (=failed) as value.
  idMap = {
    1234: true,
    2345: true,
    3456: true,

    123: false,
    234: false,
    345: false,
  }
  
  isInSuccess(value: number): boolean {
    return this.idMap[value] === true;
  }
  
  isInFail(value: number): boolean {
    return this.idMap[value] === false;
  }
  
  addToSuccess(value) {
    // used to be
    // this.successIds.push(value);
    
    // now becomes
    this.idMap[value] = true;
  }
  
  addToFail(value) {
    // used to be
    // this.failIds.push(value);
    
    // now becomes
    this.idMap[value] = false;
  }
}

I also added an example on how I guess you pushed the elements into the respective array and how to do that using an object instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading