What is the best way to line up changing column values with changing row values in an HTML table?

In its current state, the data appears as follows without alignment:

https://i.sstatic.net/o5OLu.jpg The data columns are housed within a single variable due to the presence of multiple excel tabs with varying columns. Within the tr tag, rows are checked to match specific columns.


The HTML Code

 <table class="styled-table">
                        <thead>
                            <tr id="colNames">
                                <!-- This table header is kept separate for checkbox in dedicated column -->
                                <th>
                                </th>
                                <th *ngFor="let column of tabColumnsForTable">
                                    {{column}}
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr *ngFor="let row of toDoTasks; let i = index" id="taskList" class="active-row">
                                <td class="divTd">
                                    <label>
                                        <mat-checkbox (change)="moveTabTaskToDone(row, i)"
                                            [(ngModel)]="checkboxesForToDo[i]">
                                        </mat-checkbox>
                                    </label>
                                </td>
                                <div class="divTd" *ngIf="row.qty">
                                    <td [ngClass]="{'crossOut': checkboxesForToDo[i]}">{{row.qty}}</td>
                                </div>
                                ... continuing the list of columns and corresponding data entries
                            </tr>
                        </tbody>
                    </table>
                </div>
            </mat-tab>
            <mat-tab label="Done">

                <mat-checkbox class="checkMarkAll" (change)="markAllTasksAsToDo()" [(ngModel)]="markAllToDo">
                    Mark all as to do
                </mat-checkbox>

                <div id="taskList">
                    <table class="styled-table">
                        <thead>
                            <tr id="colNames">
                                <!-- This table header is kept separate for checkbox in dedicated column -->
                                <th>
                                </th>
                                <th *ngFor="let column of tabColumnsForTable">
                                    {{column}}
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr *ngFor="let row of doneTasks; let i = index" id="taskList" class="active-row">
                                <td class="divTd">
                                    <label>
                                        <mat-checkbox (change)="moveTabTaskTo_ToDo(row, i)"
                                            [(ngModel)]="checkboxesForDone[i]">
                                        </mat-checkbox>
                                    </label>
                                </td>
                                <div class="divTd" *ngIf="row.qty">
                                    <td>{{row.qty}}</td>
                                </div>
                                ... continuing the list of columns and corresponding data entries for completed tasks
                            </tr>
                        </tbody>
                    </table>

The CSS Styling Referenced from the following link, with some modifications:

https://dev.to/dcodeyt/creating-beautiful-html-tables-with-css-428l

Answer №1

According to the HTML standard, using div elements directly within tr is not allowed (as stated here). To create a valid table without div elements, it's recommended to move the *ngIf conditions directly onto the td elements. Here's an example:

<table class="styled-table">
  <thead>
    <tr id="colNames">
      <th *ngFor="let column of tabColumnsForTable">
        {{column}}
      </th>
    </tr>
  </thead>
  <tbody>
    <td *ngIf="false">one</td>
    <td *ngIf="true">two</td>
    <td *ngIf="false">three</td>
    <td *ngIf="true">four</td>
    <td *ngIf="false">five</td>
    <td *ngIf="true">six</td>
    <td *ngIf="true">seven</td>
  </tbody>
</table>

In this scenario, the variable tabColumnsForTable is defined as:

tabColumnsForTable = [
    "Column 1", "Column 2", "Column 3", "Column 4"
]

The outcome is shown below:

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

By skipping td elements for missing columns, all rows should align with the header row in terms of column count and alignment.

Note: If columns still appear misaligned, ensure that the ngIf condition properly handles cases where a column should be displayed but lacks data in a specific row. Otherwise, subsequent columns may shift leftward unexpectedly.

To address this issue, consider checking if tabColumnsForTable includes a specific column. The ngIf statement would look like:

<td *ngIf='tabColumnsForTable.includes("vendor")'>
    {{row.vendor}}
</td> 

If row.vendor is empty or null but the vendor column exists in the list, an empty cell will maintain the correct table structure.

Answer №2

If it seems like you're searching for a solution, CSS can help you achieve what you want.

To center-align text within a column, consider using the CSS properties text-align: center; or align-items: center;. To perfectly center them within the column window, combine align-items: center; with justify-content: center;!

Hopefully this provides some assistance. If I misunderstood your needs, this method should still offer a fix for the problem.

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

Issue encountered when attempting to install dialog-polyfill for IE11 in Angular2

Currently, I am facing issues with getting dialogs to function properly on Internet Explorer 11. It seems that in order to make it work, I need to install a polyfill. However, my attempts to install the polyfill using npm have resulted in numerous errors t ...

Is there a way to assign a specific item in *ngFor to a reference variable?

Looking for help with this piece of code I have: <ng-container *ngFor="let item of results; let i = index"> <ion-item #triggerElement lines="none"> I want to set the reference #triggerElement to the item with the index of 3. Any idea ...

What is the best way to ensure the width of the div is adjusted to fit the table it contains?

What is the best way to adjust the width of a div containing a potentially wider-than-screen table? Despite setting padding: 10px for the div, the right padding disappears when the table exceeds the screen width. Below is an example code snippet: <div ...

What are the steps to ensure that this iframe adjusts perfectly to the page in terms of both vertical and horizontal dimensions

I have a sandbox from Material UI that you can view at this link: https://codesandbox.io/s/material-demo-forked-c8e39?file=/demo.js Upon loading the page, an iframe displays some HTML content. Although the width fits perfectly, there are two vertical scro ...

Guide to "flashing" an image momentarily on the screen using PHP

I'm looking for a way to display an image on my simple website for 3 seconds and then prompt the user to indicate if they recognized the image or not by clicking a button. I don't need help with the button, just how to create the timer. While I ...

Struggling to align list-items in a horizontal manner

I'm having trouble aligning the list-items in my horizontal navbar. Can anyone assist me in identifying the error? Below is the HTML code, which has a complex structure due to the use of the Wordpress thesis theme and Cufon for font replacement: < ...

Oops! There seems to be an issue with locating a differ that supports the object '[object Object]' of type 'object', like an Array

I'm currently encountering an error that reads: (ERROR Error: NG02200: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables, such as Arrays. Did you mean to use the key ...

Uncovering the class value of an element using CSS

Is there a way for me to use CSS to access the numbers (1 and 2 in this example) at the end of the paragraph's class attribute? <p class="cooking-step-1"><br> <!-- Second step ... --><br> </p> <p class="cooking-s ...

Having trouble reaching a public method within an object passed to the @Input field of an Angular component

My configurator object declaration is as follows. export class Config { constructor(public index: number, public junk: string[] = []) { } public count() : number { return this.junk.length; } } After declaring it, I pass it into the input decorated fi ...

What is the best way to showcase a standalone JSON object within the template?

I have a detailed component that is designed to show the 5-day forecast for a specific city. I have successfully retrieved the data using the http.get(Url) method. However, I am unsure of how to bind this JSON data to my view. I am familiar with displayi ...

Tips for displaying backend error messages on the frontend

I am facing an issue with returning error messages from the backend to the frontend in my Angular project. The specific requirement is to display an error message when the value of msisdn is not eligible for renewal. Currently, the hardcoded error message ...

encountered net::ERR_EMPTY_RESPONSE while attempting to locate a CSS file within an AngularJS directive

Every time my webpage attempts to access the css file from a directive, I encounter a net::ERR_EMPTY_RESPONSE. I have implemented door3's on-demand css feature, which allows for lazy loading of css files only when necessary. This feature works flawle ...

Generate HTML content based on the permissions from a REST API

I have a frontend that renders based on user permissions from a REST API. These permissions could include deleting users, creating users, adding users to workflows, and more. Most of these permissions are managed by an administrator. So my question is: H ...

Ways to display an SVG spinner prior to a substantial UI refresh

I am currently facing an issue with updating 10 apexcharts bar charts simultaneously in a Vue app. When this process occurs, it takes approximately one second to load completely, and during that time, I would like to display an svg spinner. However, the co ...

Managing custom Bootstrap 4 styles and assets within Yii2 framework

Recently, I integrated the yii2-bootstrap4 extension into my yii2-advanced project and customized it using a Sass file named custom.css. After adding custom.css to frontend/web/css, I made modifications to frontend/assets/AppAsset.php as shown below: cla ...

How to implement separate environment.ts files for different languages in Angular 12? For example, environment.en.ts and environment.de.ts files

Recently, I've been developing a multi-language Angular application and successfully deployed it to Firebase hosting. If you visit , you'll see the German version. On the other hand, displays the English version of the app. One challenge I enc ...

Fade-in effect applied to images upon exposure

Is there a way to fade in an image based on the percentage scrolled, rather than a set number of pixels? I want my website to be responsive and adjust to all screen resolutions. Or perhaps is there a method to have the image fade in when it enters the fiel ...

Pair objects that possess a specific attribute

I have a HTML snippet that I want to modify by adding the CSS class HideEdit to hide specific columns. <th class="rgHeader" style="text-align: left;" scope="col" _events="[object Object]" control="[object Object]" UniqueName="Edit"> This particular ...

What is the best way to ensure bootstrap cards' container expands horizontally?

Currently experimenting with Bootstrap 4 cards (view more at ) My goal is to have a container with a card deck inside that stretches horizontally as I add new cards, causing a horizontal scrollbar to appear when needed. By default, when the container wid ...

Adjust the width of the column to only contain fixed content and not span the entire page

Check out my solution on Plunkr to see the issue I'm facing: Plunkr I'm dealing with a layout that includes a menu on the left and page contents on the right. My goal is to have the menu fixed in place so that it doesn't move when the page ...