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

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

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

Creating routes in Node.js after setting up middleware

Currently tackling a project using node.js and encountering a specific issue. After setting up all routes with express (app.get("..", func)), I find myself stuck with a middleware that catches all requests and redirects to a 404-page. The problem arises w ...

Is there a way to retrieve command line arguments from a running Electron Application?

I am currently facing a challenge with retrieving command line arguments from an Electron application. When the application is launched with command line arguments, I need to utilize these arguments in the renderer process (specifically within a webview). ...

Navigating through the DOM using JavaScript or regular expressions

I have a DOM string called innerHTML and I am looking to extract or display the node value using either JavaScript's DOM API or JavaScript RegEx. "<nobr> <label class="datatable-header-sortable-child" onmousedown="javascript:giveFeedback(&ap ...

Retrieving information from controller to HTML page in AngularJS

Check out my code on Plunkr: http://plnkr.co/edit/8sBafktFzFa8fCLLJgMF This is the JavaScript file: angular.module('ui.bootstrap.demo', ['ui.bootstrap']); angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl&ap ...

Enhancing OpenSeadragon images with custom overlays and managing error messages

Currently, I am experimenting with the Basic Single-Row Tile Source Collection example using the same configurations and tile sources outlined in the example. However, I am encountering difficulties in adding overlays to the first and second images as int ...

Issue: $injector:unpr Unknown Provider (app.js is appearing to be properly defined)

Struggling with the unknown provider issue, I've searched through other threads and tried their solutions to no avail. My goal is to inject 'MockSvc' service into a controller without encountering any errors. Any advice would be greatly appr ...

unable to modify the content within a div by clicking on a link

Lately, I've been experimenting with a code snippet I found on this fiddle: http://jsfiddle.net/unbornink/LUKGt/. The goal is to change the content of a div when clicking on links to see if it will work on my website. However, no matter which link I c ...

Directly insert an image into your webpage by uploading it from the input field without the need to go through the server

Can an image be uploaded directly from <input type="file" /> into an HTML page, for example through the use of Javascript, without needing to first save the image to the server? I am aware that AJAX can accomplish this, but if there is a way to ...

Set JSON Value Session

In the table, there is an option to edit certain entries using a button. I have developed a function that retrieves JSON data and populates the table with it. This process happens before any other actions. Once the data is loaded, my goal is to create a c ...

Executing NodeJS custom middleware to show parent function being called

Goal: Showcase the parent function of a middleware function shared = require('./RoutFuctions'); app.post('/link', shared.verifyToken, (req, res) => { ... } In the middleware function exports.verifyToken = functio ...

Tips for enabling selection of list items in an input tag

Below is the code I have written to create an InputFilter. MyFilter = function(args) { var dataUrl = args.url; var divID = args.divID; var div = document.getElementById(divID); var myTable = '<input type="text" id="myInput" on ...

Incorporate form information into an array in Angular Version 2 or higher

This form is where I craft my questions https://i.sstatic.net/93781.png When composing a question, the ability to include multiple alternatives is available. There will also be an option to edit these alternatives. The desired format for my array is as ...

Encountering a rollbackFailedOptional error during the NPM Install process

When attempting to use various command prompts like Windows Powershell, command prompt as an admin, and bash CMD, I encountered the same error message after trying to run npm install: npm install npm@latest -g The specific error message was: [...] / ro ...

Is it possible to load components lazily without lazy loading modules in Angular?

Lazy loading is a widely used strategy, especially in Angular where it typically applies at the module level. However, can components be lazily loaded as well? Most web tutorials explain how lazy loading works with modules, such as having a main module in ...

Is there a way to deactivate other dropdown options?

To simplify the selection process, I would like to disable the options for "Province", "City", and "Barangay". When the user clicks on the "Region" field, the corresponding "Province" options should be enabled. Then, when a specific province is selected, t ...

How can I create a top right profile button similar to Google using AngularJS?

I am looking to implement a floating button in the top right corner of my main screen. When clicked, I want it to open a card below with an arrow pointing towards the button similar to the one on . I am currently using Angular Material and have tried using ...

Calculating the total sum of values using a dropdown list as a filter with J

I have successfully implemented a dropdown filter in my jQuery datatable to filter data. However, I am now looking to calculate the sum of all values in the filtered table each time a user selects a value from the dropdown list. How can I achieve this? My ...

Failed to retrieve UI data from localstorage while performing an asynchronous action

Whenever I click on a specific "task," it loads correctly and the correct UI with data is displayed. The issue arises when clicking on the link through the browser input window - localhost:port/task/edit/id, or when manually refreshing the page for each "t ...

I am eager to display this JSON data using AngularJS tables

I have JSON file with content about categories, departments, and digital marketing: { "categories":[ { "dept_id":"123", "category_name":"database", "category_discription":"list of database", "current time ...

Understanding 'this' in ChartJS within an Angular application

Here is my event handler for chartJS in Angular that I created: legend: { onClick: this.toggleLegendClickHandler After changing the text of the y scale title, I need to update the chart. I am looking to accomplish this by calling this._chart.cha ...