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

Loop through the elements of a class in JavaScript and choose all except for the one that was

Imagine having 5 div elements, each with a similar onclick-function that hides the other divs when clicked. HTML: <div id="1" class="divs" onclick="hide()"></div> <div id="2" class="divs" onclick="hide()"></div> <div id="3" cla ...

Pattern for Ajax callback in Javascript

I'm facing an issue with the code snippet below. var bar = { ajaxcall : function(){ var _obj = {}; $.ajax({ headers: { 'Content-Type': "application/json; charset=utf-8", 'da ...

Collaborative service involves objects passing through reference

I am encountering an issue with a shared service in angular. Upon application startup, the init function is triggered, fetching and returning data that is vital across the entire application. Components have the ability to inject this service and retrieve ...

Utilize Angular 2 routes within your express application

I've recently developed a web application that has the server-side built on Node.js and the client-side on Angular. The file structure of the project is as follows: |_ api |_ client |_ config |_ models |_ package.json |_ server.js However, I'm ...

what distinguishes CSS properties for Id versus class selectors

Currently in the process of creating my inaugural website, which consists of just four pages. Each page follows the standard layout structure with a header, navigation bar, content division (div id="content"), and footer. As I delve into adding CSS proper ...

Adjust the active carousel item to 0 within onsen-ui (displaying a list of carousel items in a sliding menu)

In my sliding menu, each menu item contains a carousel with two items. I am trying to make the first carousel item show after closing and reopening the menu, or by clicking a button outside of the list on the menu page. This is my current setup: <ons- ...

Position components in Angular 2 based on an array's values

Hello all, I am a beginner in terms of Angular 2 and currently facing some obstacles. My goal is to create a board for a board game called Reversi, which has a similar layout to chess but with mono-color pieces. In order to store the necessary information, ...

How can we update the information in the initial observable using data from a separate observable, taking into consideration specific conditions in Angular with RxJS?

Encountered a scenario where I need to fetch data from an API (e.g. cars via getCars()) that returns Observables and then get additional data by car model (e.g. features via getFeatures(model)) in order to replace the features data for each car. In relati ...

Tips for setting height within a flexbox layout?

Is there a way to specify the height of rows in a flex wrap container so that text containers appear square on both smaller and larger screens? I want the sizing to be dynamic, adjusting as the window is resized. Check out this example on CodeSandbox Loo ...

Generating PDF files from HTML using Angular 6

I am trying to export a PDF from an HTML in Angular 6 using the jspdf library. However, I am facing limitations when it comes to styling such as color and background color. Is there any other free library besides jspdf that I can use to achieve this? Feel ...

My element is not being animated by Elementbyclass

Without Using JQUERY The animation I'm trying to create isn't functioning properly. I attempted to utilize document.getElementsByClassName, but it's not working as expected. There are no errors, but the element is not animating correctly. ...

Enhance Your File Uploads with Custom Images in Bootstrap

For the file upload buttons, I have used the given image by customizing the button look using Bootstrap 3. Here is an example of how I achieved this: <label class="btn btn-primary" for="my-file-selector"> <input id="my-file-selector" type="fi ...

The aligning property "justify-content: space around" does not behave as expected on a line and does not provide the desired spacing

Below is the HTML code I am working on: <!DOCTYPE html> <html lang="fr"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta na ...

The option to clear searches is missing from the iOS interface

My application is designed to perform searches using post codes, and for the most part, it functions properly. However, I have encountered an issue where the clear icon on the right-hand side of the field does not display in certain browsers. To investiga ...

CSS hover effect applied uniformly to all link children

Here is the HTML code snippet I am working with: <a href="">Bioshock 2<span> - Xbox 360 review</span></a> My goal is to style the first part of the link differently from the span. The desired styling should look like this: https: ...

Utilizing the background-clip property with a gradient overlay

My Objective: My goal is to have both the arrow container and the full-width bar share the same gradient. I initially tried creating them separately and applying the gradient individually, but it resulted in a clipped look. Codepen: https://codepen.io/si ...

What is the best way to ensure that all elements inside a Grid item extend to the bottom, except for the text?

I currently have four columns within a Grid container, each structured as follows: <Grid item> <Typography>Big Title 1</Typography> <Card className={classes.stretchMe}> <CardContent> ...

Having trouble accessing specific results using Firestore's multiple orderBy (composite index) feature

I am facing an issue with a query that I run on various data types. Recently, one of the collections stopped returning results after I included orderBy clauses. getEntitiesOfType(entityType: EntityType): Observable<StructuralEntity[]> { const col ...

Exploring Angular5 Testing Utilizing Jasmine: A Component Found in Both the AppModule and DynamicTestModule's Declarations

Currently, I'm working on writing my initial test and facing a specific error: An error stating that Type SearchComponent is part of the declarations of 2 modules – AppModule and DynamicTestModule. The suggestion is to consider moving SearchCompo ...

The height of the container is not adjusted correctly after loading data via ajax which causes it to overlap with

My HTML code consists of a container with two columns (content and sidebar) and a footer. The content contains a horizontal list with li elements that are populated via ajax when the "Load More" button is pressed. I am using Bootstrap and testing on Chrome ...