Angular 2: Enhancing Tables

I am looking to create a custom table using Angular 2.

Here is the desired layout of the table:

https://i.sstatic.net/6Mrtf.png

I have a Component that provides me with data

export class ResultsComponent implements OnInit {
  public items: any;

  ngOnInit() {
    this.items = ['app1', 'item1', 'item2', 'item3', 'app2', 'item4',
            'item5', 'item6', 'app3', 'item7', 'item8', 'item9'];
    this.buildArr(this.items);
  }

  buildArr(theArr: any[]) {
    const arrOfarr = [];
    for (let i = 0; i < theArr.length ; i += 4) {
      const row = [];
      for (let j = 0; j < 4; j++) {
        const value = theArr[i + j];
        if (!value) {
          break;
        }
        row.push(value);
      }
      arrOfarr.push(row);
    }
    return arrOfarr;
  }

Below is the Template I am currently using

<h1>Results</h1>
<table class="table" id="myTable" class="table">
  <thead>
  <tr>
    <th></th>
    <th>Part 1</th>
    <th>Part 2</th>
    <th>Part 3</th>
    <th>Part 4</th>
  </tr>
  </thead>
  <tbody>

  <tr *ngFor="let row of buildArr(items);let i = index">
    <td *ngFor="let item of row">{{item}}</td>
  </tr>
  </tbody>
</table>

However, here is the output I am seeing

https://i.sstatic.net/12kCL.png

How can I achieve the desired table layout? And also, how do I populate a cell in the table with the message "Doesn't exist!"?

Answer №1

To complete each row, simply add your "does not exist" text as needed. Consider optimizing the process by converting your items array into a list of objects or a map.

Below is a revised snippet of code that inserts "does not exist" text for missing elements in each row:

export class ResultsComponent implements OnInit {
  public items: any;

  ngOnInit() {
    this.items = ['app1', 'item1', 'item2', 'item3', 'app2', 'item4',
      'item5', 'item6', 'app3', 'item7', 'item8', 'item9'
    ];

    this.buildArr(this.items);
  }

  buildArr(theArr: any[]) {
    const arrOfarr = [];
    for (let i = 0; i < theArr.length; i += 4) {
      const row = [];
      for (let j = 0; j < 4; j++) {
        const value = theArr[i + j];
        if (j > 0 && value.substring(0, 2) == "app") { //skip first item
          while (j < 4) { //fill row with placeholder string
            row[j] = "does not exist"
          }
          break;
        }
        row.push(value);
      }
      arrOfarr.push(row);
    }
    return arrOfarr;
  }
<h1>Results</h1>
<table class="table" id="myTable" class="table">
  <thead>
    <tr>
      <th></th>
      <th>Part 1</th>
      <th>Part 2</th>
      <th>Part 3</th>
      <th>Part 4</th>
    </tr>
  </thead>
  <tbody>

    <tr *ngFor="let row of buildArr(items);let i = index">
      <td *ngFor="let item of row">{{item}}</td>
    </tr>
  </tbody>
</table>

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

Having trouble getting an HTML form to work when making a PHP and Ajax request

I am trying to validate an HTML form using Ajax to prevent the browser from loading the page. Ideally, when a user enters their first name, it should display above the HTML form. However, I am encountering an issue where it is not showing up as expected... ...

What could possibly be causing a syntax error in my JavaScript code?

<script type="text/javascript> $(document).ready(function(){ $("a.grouped_elements").fancybox( 'transitionIn' : 'elastic', 'transitionOut' : 'elastic', 'speedIn' : 600, ...

How can I make two flexbox items in a row stack on top of each other as a column?

I'm currently working on a layout that involves a parent flexbox containing multiple sections. My challenge lies in getting two specific sections to stack vertically while the rest of the layout remains horizontal. At the moment, I have two sections ...

Ways to customize the color of a ReactSelect component?

Hey there! I'm currently utilizing the React select component, with the following import statement: import ReactSelect, {ValueType} from 'react-select'; Here is what my component currently looks like: https://i.stack.imgur.com/yTmW4.png Th ...

Collaborate on code for a cross-platform mobile application and a traditional desktop web application

I have a vision to create a cutting-edge app that can be utilized as both a hybrid mobile application and a desktop web application. For the most part, the logic and user interface will remain consistent across both versions. However, there are a few key ...

Encountering difficulties in starting a new Angular project using a Mac operating system

Attempting to set up a fresh Angular project named 'test' on a Mac. The command used in Terminal is as follows: xxxx$ ng new test Upon execution, the following error is displayed: -bash: /Users/xxxx/.npm-packages/lib/node_modules/@angular/cli ...

Save the altered aircraft shapes as JSON files, utilizing the Three.js framework

I've been working on modifying the vertices of a plane geometry in order to create new shapes. However, I've run into an issue where when I export the modified geometry as JSON, the changes I made to the vertices are not included in the exported ...

Comparing Fetch and Axios: Which is Better?

Currently delving into the realms of axios and the fetch API, I am experimenting with sending requests using both methods. Here is an example of a POST request using the fetch API: let response = await fetch('https://online.yoco.com/v1/charges/&ap ...

Guide to creating fog animations in three.js

I'm attempting to adjust the fog density using tweening, but for some reason, it doesn't seem to be working. Here are my default settings: var camera, densityFog, colorFog2; colorFog2 = 0xfee2ed; densityFog ...

Troubles with concealing dropdown menu when hovering

I have noticed that this issue has been raised before, but none of the solutions provided seem to fix my problem specifically. The submenu in my menu is not functioning as intended. It should be displayed when hovered over and hidden when not being hovere ...

Encountering incorrect JSON formatting even though the content is accurate

I'm encountering an error while trying to fetch the JSON. It seems to be in the wrong format. Below is the JSON data: { "favorite_page_response": "<div class=\"col-md-12 col-lg-12\">\n <div class=\"cart\ ...

Rejuvenate your Bootstrap Accordion with these Settings

Using bootstrap documentation, I have implemented an accordion in my web application with the following code: <div class="accordion" id="accordion2"> <div class="accordion-group"> <div class="accordion-heading"> <a class=" ...

Tips for linking server value with Javascript onmousedown in an aspx web page

I have a single Hyperlink on an aspx page. There are two tasks I need to accomplish: When I click on the hyperlink, I want to log some activity. While logging the EmployeeID value, I need to bind it from the server. However, I am encountering an error t ...

Making jQuery work: Utilizing tag replacements

My current code is: this.html(this.html().replace(/<\/?([i-z]+)[^>]*>/gi, function(match, tag) { return (tag === 'p') ? match : '<p>'; return (tag === '/p') ? match : '</p& ...

Is less.js capable of compiling CSS code that is compatible with all browsers?

Is there a simple way to convert my Less code into browser-compatible CSS like the example below? #grad1 { background: -webkit-linear-gradient(red, blue); /* For Safari 5.1 to 6.0 */ background: -o-linear-gradient(red, blue); /* For Opera 11.1 to ...

The request to login at the specified API endpoint on localhost:3000 was not successful, resulting in a

As I continue to develop my programming skills, I have been working on configuring a database connected with nodejs in the same folder. However, when trying to make an ajax request to the database, I encountered an error indicating that the database may be ...

Resolve Bootstrap 4 responsive table header alignment glitch

Utilizing the d-none and d-md-block classes on a responsive table to hide certain columns at smaller screen sizes has been successful overall. However, there is one issue - the hidden columns seem to be slightly misaligned with the rest of the table. Despi ...

Angular 2 Typeface Delivery to the Server

I'm experiencing an issue with the font when trying to access it from the server. It works fine on localhost, but on the server with a different directory structure, it fails to load properly. The same problem occurs with the logo as well. @font-face ...

"Make updates to the data with the help of a button input

I am working on a form that has two input type buttons. The first button is labeled "edit", and when clicked, it enables the input field. The second button is labeled "save", and when clicked, it disables the input field and should save the new values in a ...

Is it possible to choose the inverse of a user-defined type in Angular?

Is it possible to retrieve the opposite of a specified custom type within a variable using Typescript? For example, if I define a type like this: type Result = 'table' | 'grid'; Then any variable with the type Result can only be assign ...