Angular Bootstrap causes misalignment of table column headings based on different properties in object

I have an object with two properties: person and vehicle. Both properties consist of arrays of data. I have separate column headings for the person and vehicle properties and display the data in tabular form. However, the column headings for both properties are not aligned. Can someone assist me with this issue?

import { Component } from "@angular/core";

@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.scss"]
})
export class AppComponent {
personColumns = ["Name", " Age", "Department"];
vehicleColumns = ["Model", "Color", "Company"]
values = {
person: [
  {
    name: "John",
    age: 28,
    department: "Accounting"
  },
  {
    name: "Max",
    age: 26,
    department: "Sports"
  },
  {
    name: "Rose",
    age: 24,
    department: "Arts"
  }
],
vehicle: [
{
  model: "SEDAN",
  color: "Red",
  compay: "Mercedes"
},
{
  model: "COUPE",
  color: "White",
  compay: "BMW"
},
{
  model: "SUV",
  color: "Yellow",
  compay: "AUDI"
}
]
};
}

In the component.html file

<div *ngIf="values != []">
<span *ngIf="values.person">
<div class="table-responsive">
  <table class="table table-striped">
    <thead>
      <tr>
        <th class="p-1" *ngFor="let column of personColumns">
          {{ column }}
        </th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let value of values.person">
        <ng-container *ngIf="value.name != 'Max'">
          <td class="p-1">
            {{ value.name }}
          </td>
          <td class="p-1">
            {{ value.age }}
          </td>
          <td class="p-1">
            {{ value.department }}
          </td>
        </ng-container>
        <ng-container *ngIf="value.name == 'Max'">
          <td class="p-1">
            {{ value.name }}
          </td>
          <td class="p-1">
            {{ value.age }}
          </td>
          <td class="p-1">
            {{ value.department + " changed to" + " IT" }}
          </td>
        </ng-container>
      </tr>
    </tbody>
  </table>
</div>
</span>

<span *ngIf="values.vehicle">
<div class="table-responsive">
  <table class="table table-striped">
    <thead>
      <tr>
        <th class="p-1" *ngFor="let column of vehicleColumns">
          {{ column }}
        </th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let value of values.vehicle">
        <ng-container *ngIf="value.model != 'SUV'">
          <td class="p-1">
            {{ value.model }}
          </td>
          <td class="p-1">
            {{ value.color }}
          </td>
          <td class="p-1">
            {{ value.compay }}
          </td>
        </ng-container>
        <ng-container *ngIf="value.model == 'SUV'">
          <td class="p-1">
            {{ value.model }}
          </td>
          <td class="p-1">
            {{ value.color }}
          </td>
          <td class="p-1">
            {{ value.compay + " changed to" + " Ford" }}
          </td>
        </ng-container>
      </tr>
    </tbody>
  </table>
</div>

In app.component.scss

@import "../styles.scss";

.table thead th {
vertical-align: top;
}

th {
white-space: nowrap;
cursor: pointer;
user-select: none;
color: grey;
}

table {
margin: 0;
font-family: "Lato";
font-size: 12px;
}

.table-responsive {
 min-height: 60px;
}

.cell {
max-width: 250px;
white-space: nowrap;
overflow: hidden;
display: inline-block;
text-overflow: ellipsis;
}

.table thead th {
vertical-align: top;
}

https://i.sstatic.net/tjmDY.jpg

Answer №1

To ensure uniform length for all values, it is recommended to utilize a single table. To achieve this, you may consider implementing the following template:

<div *ngIf="values != []">
<span *ngIf="values.person">
    <div>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th *ngFor="let column of personColumns">
                        {{ column }}
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="let value of values.person">
                    <ng-container *ngIf="value.name != 'Max'">
                        <td>
                            {{ value.name }}
                        </td>
                        <td>
                            {{ value.age }}
                        </td>
                        <td>
                            {{ value.department }}
                        </td>
                    </ng-container>
                    <ng-container *ngIf="value.name == 'Max'">
                        <td>
                            {{ value.name }}
                        </td>
                        <td>
                            {{ value.age }}
                        </td>
                        <td>
                            {{ value.department + " changed to" + " IT" }}
                        </td>
                    </ng-container>
                </tr>
            </tbody>
            <thead *ngIf="values.vehicle">
                <tr>
                    <th *ngFor="let column of vehicleColumns">
                        {{ column }}
                    </th>
                </tr>
            </thead>
            <tbody *ngIf="values.vehicle">
                <tr *ngFor="let value of values.vehicle">
                    <ng-container *ngIf="value.model != 'SUV'">
                        <td>
                            {{ value.model }}
                        </td>
                        <td>
                            {{ value.color }}
                        </td>
                        <td>
                            {{ value.company }}
                        </td>
                    </ng-container>
                    <ng-container *ngIf="value.model == 'SUV'">
                        <td>
                            {{ value.model }}
                        </td>
                        <td>
                            {{ value.color }}
                        </td>
                        <td>
                            {{ value.company + " changed to" + " Ford" }}
                        </td>
                    </ng-container>
                </tr>
            </tbody>
        </table>
    </div>
 </span> 

To incorporate bootstrap classes, you can include the following code snippet in the index.html file:

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

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

Is it possible to nest a <div> element inside a table in HTML code?

I am facing a requirement where I need the table rows inside a table, excluding the head row, to be scrollable. I attempted the following code, but it did not produce the desired outcome: <table id="applicantList1" border="1" align="left"> &l ...

What is more beneficial: using one comprehensive design that requires just one HTTP request or opting for several smaller designs that necessitate

In the process of developing an Angular application, I am faced with the task of displaying the count of each individual animal. For instance: In my backend data, I have animals stored as follows: { id: 1, name: "lion" },{ id: 2, name: "lion" },{ id: 3, ...

angular2 with selenium webdriver: Issue with resolving 'child_process' conflict

I followed these steps: ng new typescript-selenium-example npm install selenium-webdriver --save I also made sure to copy chromedriver to my /Application. I updated the app.component.ts file as shown below: import { Component } from '@angular/core ...

Changing the value of a form input name after a $scope.$watch event is activated

In my AngularJS 1.4 project, I have a form input element where I am attempting to dynamically set the name attribute. <input type="email" name="{{ctrl.name}}" class="form-control" id="email" ng-minlength="5" required> The assignment of the name att ...

In Angular 4, the variable within the component is refreshed or updated only when the service is called for the second

Recently, I started working with the Angular framework and encountered an issue while developing a basic data retrieval feature from a component using a service. I had already set up a web service that returns specific data for an ID (wwid). The function c ...

Unable to reach the top while perfectly centered

I am currently struggling to create a perfectly centered popup menu that allows me to scroll all the way to the top. It seems like the transform property only affects visuals, so even though #content is set to top: 50%, I can't see everything at the t ...

Using Ajax and jQuery to Retrieve the Value of a Single Tag Instead of an Entire Page

Let's say I have a webpage named page.html: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Page</title> </head> <body> <h1>Hello World!!</h1> </body> </html> Now ...

list without specific order spacing

I have implemented an unordered list in the footer section of my HTML document. Within this list, the first two items consist of social media logos displayed as images. To enhance their visibility and prominence within the footer, I have assigned them a wh ...

Incorporate an icon into a text input field using Material UI and React

I have a form with a text input element: <FormControl className='searchOrder'> <input className='form-control' type='text' placeholder='Search order' aria-label='Search&ap ...

Transforming a GIMP design into a fully functional webpage

How can I transform my GIMP design into an actual website? After reviewing some information, it appears that using GIMP might not be ideal for creating CSS. It is recommended to utilize a CSS/HTML editor instead. Exporting HTML/CSS with Inkscape or GIMP N ...

Is there a way to adjust the position of the scrolling bar to be closer to the post area?

I was exploring this page (http://noahsdad.com/state-fair-texas-2011/) and noticed a scrolling sidebar to the left of the content area. I'm looking to adjust it so that it is closer to the content area. Any suggestions on how to achieve this? Thank y ...

Understanding how to access and interpret comments within the HTML DOM body using Selenium操作

I am attempting to extract the following comment from the body of an HTML DOM using Selenium: <html> <head class=.....> <body> <script>...</script> <!-- Campaign Name: POC_SITE_MONETIZATION_DEALS_QA Experience Name: SMBelo ...

Turn your mouse cursor into a dynamic and animated image using jQuery

I've implemented a code snippet that replaces the cursor with 2 images placed at a small distance from each other: $(document).mousemove(function (e) { $("#firstImage").css({ left: e.pageX, top: e.pageY }); ...

The grid flex end is behaving differently than I anticipated

I am struggling to align two buttons vertically on the right side. Here is my code snippet: const WalletsContainer = () => { return ( <Grid style={{ background: 'red' }} direction={'column'} alignItems={'flex-end'} ...

How can I bring the toolbar to the forefront of the page in an Angular/HTML environment?

I am currently facing an issue with bringing the toolbar to the forefront of the page. When I scroll down, the elements on the page end up covering the toolbar which is not the desired behavior. Below is the CSS code for the toolbar: .maintoolbar.mat-tool ...

The Angular2 Mvc5 demonstration application is experiencing compilation errors

I recently started using Visual Studio Enterprise 2015 Update 3 and decided to create a new project called "Angular2 Mvc5 sample application" from the templates available online. However, upon compiling the project, I encountered numerous errors such as: ...

What is the best way to animate the scaling of a CSS property using jQuery?

I need help creating an animation where a circle div with the class of "bubble" grows from nothing to its full size when a button is clicked using jQuery. I am currently facing difficulties in making it work properly. Here's my current code snippet: ...

How can you create two HTML DIVs side by side with equal heights?

I am working on creating two side by side DIVs, where one contains static content and the other has dynamic content. My goal is to make sure that the height of the static DIV increases whenever the height of the dynamic DIV increases. Here's the code ...

Fixing the alignment of the left column table in Bootstrap 3.3.7

Is there a way to fix the left table column (date and all column names) in Bootstrap 3.3.7? I attempted to use position: fixed; but it didn't work as expected. Any suggestions on what I should do next? Check out this fiddle for reference <scr ...

Display an image in an Angular application using a secure URL

I am trying to return an image using a blob request in Angular and display it in the HTML. I have implemented the following code: <img [src]="ImageUrl"/> This is the TypeScript code I am using: private src$ = new BehaviorSubject(this.Url); data ...