Angular // binding innerHTML data

I'm having trouble setting up a dynamic table where one of the cells needs to contain a progress bar. I attempted using innerHTML for this, but it's not working as expected. Any suggestions on how to approach this?

Here is a snippet from my dashboard.component.html file where the issue lies with [innerHTML]="tableRow.limitOccupancy":

<div class="divTableRow" *ngFor="let tableRow of tableRows">
   <div class="divTableCell">{{ tableRow.name }}</div>
   <div class="divTableCell">{{ tableRow.value }}</div>
   <div class="divTableCell">{{ tableRow.previousValue }}</div>
   <div class="divTableCell">{{ tableRow.delta }}</div>
   <div class="divTableCell">{{ tableRow.rafLimit }}</div>
   <div [innerHTML]="tableRow.limitOccupancy" class="divTableCell"></div>
   <div class="divTableCell">{{ tableRow.date }}</div>
</div>

In my dashboard.component.ts file, my goal is to have the limitOccupancy display a progressBar template through innerHTML:

progressBar = '<div class="progress-bar"><span></span></div>';

tableRows = [
    {
        name: 'LEVEL 3',
        value: '8%',
        previousValue: '7%',
        delta: '-11000',
        rafLimit: '12%',
        limitOccupancy: this.progressBar,
        date: '12.09.20',

    },
    {
        name: 'LEVEL 1',
        value: '8%',
        previousValue: '7%',
        delta: '-11000',
        rafLimit: '12%',
        limitOccupancy: this.progressBar,
        date: '12.09.20',
    },
    
];

Answer №1

Everything seems to be working fine. The div is loading properly and the class has been added in style.css. It's even applying the CSS correctly. If you're encountering any issues, please let me know so I can help resolve them.

Check out the code here

Answer №2

If I were to tackle this scenario, my go-to solution would be developing a customized "progress-bar" component.

export class ProgressBarComponent implements OnInit {
  @Input() progress: number = 0;
  
  ngOnInit() {}

  clampProgress() {
    return Math.max(
      Math.min(this.progress, Math.max(0, 100)),
      Math.min(0, 100)
    );
  }
}
<div class="light-grey">
  <div class="grey" style="height:24px" [ngStyle]="{width:progress+'%'}"></div>
</div>
.light-grey {
  background-color: lightgrey;
}

.grey {
  background-color: grey;
}

You can then utilize this custom component as follows:

<div class="divTableRow" *ngFor="let tableRow of tableRows; let index as i">
   <div class="divTableCell">{{ tableRow.name }}</div>
   <div class="divTableCell">{{ tableRow.value }}</div>
   <div class="divTableCell">{{ tableRow.previousValue }}</div>
   <div class="divTableCell">{{ tableRow.delta }}</div>
   <div class="divTableCell">{{ tableRow.rafLimit }}</div>
   <div class="divTableCell">
       <app-progress-bar [progress]="getLimitOccupancy(i)"></app-progress-bar>
   </div>
   <div class="divTableCell">{{ tableRow.date }}</div>
</div>

Instead of getLimitOccupancy(index), there is now a function that generates a value between 0 and 100 based on the row-item identifier.

Check out a live example here: https://stackblitz.com/edit/angular-ivy-r8wjag?file=src/app/app.component.ts

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

How can we retrieve an API response using Fetch, manipulate it with JSON.stringify(), and what are the next steps in

After countless attempts, I still can't figure out what's missing here. I'm utilizing fetch to retrieve data from Mapbox: var response = fetch(myURL) .then(response => response.json()) .then(data => console.log(JSON.stringify(data))) ...

What steps can be taken to resolve the issue of "Access Denied: Invalid token, incorrect code"?

Assigned to me recently for a school project is a coding challenge that consists of various parts. The final task involves uploading to a private GitHub repository and submitting a completion request through a POST request under specific conditions. I hav ...

Transform the componentDidUpdate method that uses prevProps into a custom hook integrated with Redux

Trying to convert a life cycle method into a hook is not working as expected. When the component mounted, if the user ID exists in local storage, the user is connected and their name is displayed in the navbar. If they disconnect and reconnect, their name ...

Chrome on IOS: Experiencing screen freezing while scrolling

1) I'm working with Material UI in React JS. I have added a simple scrollable code, but when I reach the bottom of the screen/scroll, it freezes. 2) I also tried it with a simple HTML page and it worked correctly. <div style={{ ...

The key is not applicable for indexing the type as expected

Here is the TS code I am working with: type Fruit = { kind: "apple" } | { kind: "grape"; color: "green" | "black" }; type FruitTaste<TFruit extends Fruit> = TFruit["kind"] extends "apple" ? "good" : TFruit["color"] extends "green" ? "good" : ...

The attribute 'XXX' is not found within the type 'IntrinsicAttributes & RefAttributes<any>'

My coding hobby currently involves working on a React website using TypeScript. I recently came across a free Material UI template and decided to integrate it into my existing codebase. The only challenge is that the template was written in JavaScript, cau ...

ReactJS error: Unable to access the setState property

As a newcomer to ReactJS, I have been facing some challenges. I recently familiarized myself with the ES6 syntax, and it claims that the following pieces of code are equivalent in meaning. 1. YTSearch({key: API_KEY, term: 'nba'}, function(vide ...

Utilize multiple activated modules in Angular 2

Recently, I've been exploring the new features of Angular 2 final release, particularly the updated router functionality. An interesting example showcasing the router in action can be found at this link: http://plnkr.co/edit/mXSjnUtN7CM6ZqtOicE2?p=pr ...

function complete, ajax request finished

When working with Ajax to get data, I encountered a situation where I needed to return the value of `username` from within an if statement that is inside a loop and a function. How can I achieve this? Your help would be appreciated. $.ajax({ ...

Utilizing the default event object in ag-Grid's event methods with JavaScript

I am a newcomer to ag-grid and I need help with calling event.preventDefault() in the "cellEditingStopped" grid event. Unfortunately, I am struggling to pass the default JavaScript event object into it. Is there a way to make this work? Additionally, I al ...

Fixing the error: "React-dom.development.js:4091 Uncaught TypeError: onItemSelect is not a valid function"

Every time I change the option in the selector, I keep encountering this error: "react-dom.development.js:4091 Uncaught TypeError: onItemSelect is not a function" :( import React from "react"; import Product from "./Product" ...

Implementing defaultProps in conjunction with withStyles

Currently, I am in the process of developing a component using material-ui withStylers and defaultProps. However, I have encountered an issue where the props of the component are not being retrieved in the styles objects unless they are explicitly passed t ...

What is the method to display just the final 3 characters within a paragraph?

Is there a way to display only the last three characters of a string using either a method or a string method? I consistently have a 13-digit number, but I specifically require showing only the final three digits. Appreciate any assistance provided. Than ...

Tips on incorporating jQuery cross-domain functionality

Attempting to retrieve and display the firstName data from this URL: http://www.w3schools.com/jquery/demo_ajax_json.js, as part of a test for another project. Encountered the error message: Origin null is not allowed by Access-Control-Allow-Origin, prompt ...

Angular Bootstrap: How to Resolve the Error "Function $(...).collapse() is Undefined"

I'm a beginner with Bootstrap and I'm attempting to trigger the .collapse() function using JavaScript within an Angular controller when a user clicks on a link. The goal is to close the collapsible navbar when a link is clicked, as the routing in ...

Adding data to a multidimensional array in JavaScript

I am in need of creating a multidimensional JavaScript array dynamically, following this specific structure: array_answers[0][1]:"yes" array_answers[1][2]:"no" array_answers[2][2-subquestion]:"text input" array_answers[3][8]:"yes" array_answers[4] ...

In order to achieve a sliding effect for the second div, it can be programmed to

Currently, I am working on implementing hide and show functionality in my project. However, I have come across a bug in my code that I need assistance with. When clicking on the first div element, the content opens from bottom to top instead of the desired ...

Example showcasing the functionality of the react-custom-scrollbars package in a TypeScript React application

In my TypeScript React project, I am struggling to set up the react-custom-scrollbars package successfully. Despite consulting the examples provided in the GitHub repository, I have not been able to get it working. Can someone share a functional example ...

Tips for effectively utilizing innerHTML in this particular scenario

For an assignment, we were tasked with creating a Madlib game where users input words into textfields to replace certain words in a hidden paragraph within the HTML using JavaScript and CSS. The paragraph embedded in the HTML page is as follows: <span ...

Minifying HTML, CSS, and JS files

Are there any tools or suites that can minify HTML, JavaScript, and CSS all at once? Ideally, these tools should be able to: Identify links from the HTML document and minify the associated JavaScript and CSS. Remove any unused JavaScript functions and CS ...