Angular Space-Friendly Table

After attempting to code the sample in Angular, I realized it wasn't what I was looking for.

                    <table>
                        <th >Number</th>
                        <th >Merchant Name</th>
                        <th >Last Order Total</th>
                        <th >Last Order Date</th>
                        <hr>
                          <tr *ngFor="let item of topFiveActiveMerchant; index as i;">
                            <td>{{i+1}}</td>
                            <td>{{item.merchant_name}}</td>
                            <td>{{item.merchant_name}}</td>
                            <td>{{item.created_date | date:'dd/MM/yyyy h:mm a'}}</td>
                          </tr>
                    </table>

I intended for it to look like this https://i.stack.imgur.com/8foa3.png

but unfortunately, mine ended up looking like https://i.stack.imgur.com/Yf1rj.png

Answer №1

If you are looking to implement a simple table, I have a solution for you. However, I recommend checking out mat-table (Documentation) as it offers easy implementation and a wide range of features. Below is the code snippet for a simple table:
HTML:

<table class="my-table">
  <thead class="my-table headers">
    <tr>
      <th *ngFor="let column of config">{{column.header}}</th>
    </tr>
  </thead>
  <tbody class="my-table body">
    <tr my-active *ngFor="let row of data.data; index as rowIndex">
      <td (click)="onSelect(rowIndex)" *ngFor="let column of config; index as i" [ngClass]="{last: i === (config.length - 1) }">{{row[column.value]}}</td>
    </tr>
  </tbody>
</table>

TS:

import {Component, OnInit, ViewChild} from '@angular/core';

export const configColumns = [{
  header: "Name",
  value: "name"
}, {
  header: "E-mail",
  value: "email"
}, {
  header: "Phone",
  value: "phone"
}];
export const data = [{
  name: "John Doe 1",
  email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e747176707a717b4171707b5e7b737f7772307d7173">[email protected]</a>",
  phone: "900001111"
}, {
  name: "John Doe 2",
  email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="204a4f484e444f457f54574f60454d41494c0e434f4d">[email protected]</a>",
  phone: "900002222"
}, {
  name: "John Doe 3",
  email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="761c191e1812191329021e04131336131b171f1a5815191b">[email protected]</a>",
  phone: "900003333"
}];

@Component({
  selector: 'table-sorting-example',
  styleUrls: ['table-sorting-example.css'],
  templateUrl: 'table-sorting-example.html',
})
export class TableSortingExample implements OnInit {
   data:any;
   config:any;
  ngOnInit() {
    this.config=configColumns;
    this.data=data;
  }
}

CSS:

table.my-table {
  border-collapse: collapse;
  width: 100%;
  text-align: center;
}

thead.my-table.headers tr th {
  border: black solid 1px;
  background-color: darkblue;
  color: white;
  height: 25px;
}

tbody.my-table.body tr td {
  border: black solid 1px;
 
  height: 25px;
}

tbody.my-table.body tr.active {
  background-color: gainsboro;
  cursor: pointer;
}

div.pag-space {
  margin-top: 10px;
  display: flex;
  justify-content: space-around;
}

button.pag-button {
  width: 50px;
  height: 20px;
  cursor: pointer;
}

select.size-page {
  width: 100px;
  text-align-last: center;
  cursor: pointer;
}

Answer №2

If you are experienced with Angular Material or Bootstrap, you have the option to utilize their predefined table styles. However, if you wish to create a custom table design, you will need to individually style each element of the table.

To customize the appearance of each column within the Table Header and Table Description, you can apply the following css styling:

In your component.html file, assign a class to the table:

<table class="custom-table">

In your component.css or component.scss file:

.custom-table {
  border-collapse: collapse;
}
.custom-table th,
.custom-table td {
    padding: 10px;
    // add other css properties as needed
}

Distinguish header styling separately:

.custom-table th {
    padding: 10px;
    // add other css properties as needed
}

Differentiate column styling separately:

.custom-table td {
    padding: 10px;
    // add other css properties as needed
}

To specify the width of a column:

<th style="width: 200px">Merchant</th>

Explore additional styles such as alignment:

<th style="width: 200px; text-align: center">Merchant</th>

Apply similar adjustments for column descriptions:

<td style="text-align: center">{{item.merchant_name}}</td>

Refine styling within column descriptions:

<td style="text-align: center">
    <div class="name">{{item.merchant_name}}</div>
    <div class="desg">{{item.merchant_desg}}</div>
</td>

Refer to Angular's component-styles guide for further assistance in styling components.

::ng-deep .custom-table .name {
    font-weight: bold;
    font-size: 18px;
    // add other styles
}
::ng-deep .custom-table .desg {
    font-size: 12px;
    // add other styles
}

If using SCSS:

::ng-deep .custom-table {
    .name {
        font-weight: bold;
        font-size: 18px;
        // add other styles
    }
    .desg {
        font-size: 12px;
        // add other styles
    }
}

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

What is the best way to upload a canvas image from a GUI to the back-end using an AJAX call and setting the content-type as "image/jpeg"?

What is the best method for saving a canvas image from GUI to back-end using an AJAX call that accepts content type "image/jpeg" and avoids the error "jquery.js: 8453 Uncaught TypeError: Illegal invocation?" HTML <canvas id="myImage"></canvas> ...

Encountering an issue: JwPagination in angular 9 throws an error of "Cannot read property 'currentValue' of undefined"

Currently using Jw pagination with a page size that changes on 5, 10, or 15 using a dropdown. The Angular version being used is angular 9. The HTML code snippet for this functionality looks like: <div class="col-md-6"> <div ...

The Jquery script is ineffective for newly added elements

After creating an Ajax Form that functions well, I noticed that when the result of the form is another form, my script does not work for the new form generated. Is there a way to make the new form function like the old one? $(document).ready(function() ...

How should one correctly align various classes in a cascading manner within CSS?

Here is an interesting challenge - I want each of these buttons to have their background image change when hovered over. However, I'm struggling with the class setup in my code. I've been trying to understand how to distinguish between divs and c ...

Ways to implement CSS in my JQuery Plugin

In the process of creating a unique JQuery component plugin that relies on some essential default CSS for its layout, I am faced with a dilemma. Should I: Create a separate .css file and load it dynamically within my plugin code (How would this be accomp ...

"Exploring the Power of Angular 2 with Route Resolving

Currently, I am diving into the world of Angular and it's all quite new to me. I have a challenge where I need to display data in a view fetched from the server asynchronously. To tackle this, I attempted to use resolves to ensure the data is retrieve ...

What is the best way to show an element when hovering over another element?

I'm looking for a CSS-only solution (NO JQuery) to display a FontAwsome icon (<i class='fas fa-arrow-alt-circle-left fa-lg arrowbounce text-success hidearrowbounce'></i>) only when hovering over another element. However, despite ...

Extract attributes from a string of HTML containing name-value pairs

Here is a sample string that I need to work with '<img id="1" data-name="test" src="img_01.jpg" />' My goal is to extract the attribute name and value pairs from the string, create the element, and apply these attributes. I have come up ...

How to retrieve a parameter value within the app component in Angular 2

Within my appcomponent, I have incorporated a dropdown functionality. Whenever the user selects an option from the dropdown, it loads a new page in the router outlet. However, if I refresh the page, the router loads correctly but the dropdown selection i ...

What is the best way to ensure observables in a template (using async pipe) are fully subscribed to before executing any initialization code?

I am facing an issue with my HTTP service that returns information based on a given item ID. The data is fetched using a Subject, which receives the initial data in the ngOnInit method. To display the returned data in the HTML, I utilize the async pipe. ...

Is it necessary to retrieve the data that was posted from the back-end? Implementation using Angular and Node.js

I need some advice on how to handle worker data in my Angular, NodeJS, and MySQL app. In the FORM, users can add workers whose information is then sent to the backend for processing. The user can preview the posted information and delete workers if needed. ...

Importing JSON data from a URL to display in an HTML table

Looking to utilize the Untappd API for a beer menu project, I've encountered an issue. The goal is to showcase the JSON data received from the server and organize it into an HTML table. Despite successfully importing data from a local list.json file ...

Guide on activating a service in one component to close a pop-up in a separate component [angular]

In order to manage navigation in our app, we have chosen to utilize the Angular router instead of the Ionic navigation controller. One challenge we are facing is handling navigation using the Android back button. When working with the Ionic framework, pre ...

Prevent zooming or controlling the lens in UIWebview while still allowing user selection

Is there a way to disable the zoom/lens on uiwebview without affecting user selection? I'm trying to avoid using "-webkit-user-select:none" in my css. -webkit-user-select:none ...

What is the best method for automating the transfer of data from a database to the user interface of a website using html and javascript?

As a volunteer coordinator for a non-profit organization, I have some basic experience working with Python. I am willing to dedicate fewer than 8 hours of learning time to my current project in order to automate certain tasks (or else I will do them manual ...

Currently working on enhancing the responsiveness of the login form

I am encountering difficulties while trying to make the page responsive. I've checked my basic code, but something seems off. Could you please take a look at the plnkr link below and give me some feedback on where I might have gone wrong? Here's ...

Why are polyfills-es2015, vendor-es2015, main-es2015, and other files blocked from loading because the MIME type is not allowed (text/html

Hey there! I've encountered an issue during the deployment of my Angular app. When I serve it from Angular locally, everything works perfectly fine. However, when I deploy it on a Tomcat server, I encounter errors and I can't see anything related ...

Developing a customizable datepicker with the ability to select specific months or date ranges

I am currently developing a WebApp using flask and constructing templates in HTML/JS for the front end. I am in need of a datepicker that will provide the user with the option to choose a specific date range or select a range of months. Take a look at the ...

Exploring the Power of TailwindCss in Storybook 6 for Angular Development

I am in the process of creating a component library using Angular version 11.2.8, and I'm attempting to integrate TailwindCss and Storybook 6. Despite trying various configurations, none seem to be working correctly for me. Whenever I run Storybook, ...

Reposition icons accordingly while incorporating a new set of icons

After creating a new icon set font, I noticed that the icons are not positioning correctly and appear smaller than expected when loaded: https://i.stack.imgur.com/1OsAL.png https://i.stack.imgur.com/zmLQq.png I attempted to adjust the .icon class by add ...