Top

Tables


# First Name Last Name Username Role Country
1 Alexander Orton @mdorton Admin USA
2 John Deo Deo @johndeo User USA
3 Randy Orton the Bird @twitter admin UK
4 Randy Mark Ottandy @mdothe user AUS
5 Ram Jacob Thornton @twitter admin IND
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Username</th>
<th scope="col">Role</th>
<th scope="col">Country</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Alexander</td>
<td>Orton</td>
<td>@mdorton</td>
<td>Admin</td>
<td>USA</td>
</tr>
<tr>
<th scope="row">2</th>
<td>John Deo</td>
<td>Deo</td>
<td>@johndeo</td>
<td>User</td>
<td>USA</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Randy Orton</td>
<td>the Bird</td>
<td>@twitter</td>
<td>admin</td>
<td>UK</td>
</tr>
<tr>
<th scope="row">4</th>
<td>Randy Mark</td>
<td>Ottandy</td>
<td>@mdothe</td>
<td>user</td>
<td>AUS</td>
</tr>
<tr>
<th scope="row">5</th>
<td>Ram Jacob</td>
<td>Thornton</td>
<td>@twitter</td>
<td>admin</td>
<td>IND</td>
</tr>
</tbody>
</table>
</div>

You have to change only table sizing class.you can use table-*(sm,lg,xl,xs) class

# First Last Handle
1 Mark Otto @mdo
2 Jacob Thornton @fat
3 Larry the Bird @twitter
<div class="table-responsive">
<app-card [header]="true" [cardHeaderClass]="'pb-20'">
    <h5 header>Sizing Tables</h5><span header>Example of Extra large table, Add<code>.table-xl</code> class to
        the<code>.table</code> , Large table Add <code> .table-lg</code> , Default table Add <code>.table-de</code>
        , Small table Add <code>.table-sm </code>, Extra Small table Add <code>.table-xs</code>to create a
        table.</span>
    <div class="card-block row" card>
        <div class="col-sm-12 col-lg-12 col-xl-12">
            <div class="table-responsive custom-scrollbar">
                <table class="table table-lg">
                    <thead>
                        <tr>
                            <th scope="col">Id</th>
                            <th scope="col">Employee Name</th>
                            <th scope="col">Date</th>
                            <th scope="col">Status</th>
                            <th scope="col">Hours</th>
                            <th scope="col">Performance</th>
                        </tr>
                    </thead>
                    <tbody>
                        @for (item of sizingTable; track item) {
                        <tr>
                            <th scope="row">{{item.id}}</th>
                            <td>{{item.employee_name}}</td>
                            <td>{{item.date}}</td>
                            <td [ngClass]="item.id == 2 ?'font-success' : 'font-danger'">{{item.status}}</td>
                            <td>{{item.hours}}</td>
                            <td>{{item.performance}}</td>
                        </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</app-card>
// ts files
<div class="table-responsive">
import { Component } from '@angular/core';
import * as data from '../../../../shared/data/table/bootstrap-table';
import { CommonModule } from '@angular/common';
import { CardComponent } from "../../../../shared/components/card/card.component";

@Component({
    selector: 'app-sizing-tables',
    templateUrl: './sizing-tables.component.html',
    styleUrls: ['./sizing-tables.component.scss'],
    imports: [CommonModule, CardComponent]
})

export class SizingTablesComponent {

  public sizingTable = data.sizingTable;
  
}
;
// data file
<div class="table-responsive">
export const sizingTable = [
  {
      id: 1,
      employee_name: "Mark Jecno",
      date: "22/08/2023",
      status: "On leave",
      hours: "0",
      performance: "29/30"
  },
  {
      id: 2,
      employee_name: "Elana Robbert",
      date: "21/08/2023",
      status: "Present",
      hours: "10",
      performance: "30/30"
  },
  {
      id: 3,
      employee_name: "John Deo",
      date: "18/08/2023",
      status: "On leave",
      hours: "8",
      performance: "28/30"
  }
]
Name Position Office Age Start date Salary
Tiger Nixon System Architect Edinburgh 61 2011/04/25 $320,800
Garrett Winters Accountant Tokyo 63 2011/07/25 $170,750
<!-- Datatable Html Files -->
     <div class="table-responsive custom-scrollbar" body>
          <div class="dataTables_wrapper no-footer">
            <div id="basic-1_filter" class="dataTables_filter">
              <label>Search:<input type="search" [(ngModel)]="service.searchTerm"></label>
              @if (service.loading$ | async) {
              <span class="col col-form-label">Loading...</span>
              }
            </div>
            <table class="display dataTable no-footer" id="basic-1">
              <thead>
                <tr>
                  <th sortable="name" (sort)="onSort($event)">Name</th>
                  <th sortable="position" (sort)="onSort($event)">position</th>
                  <th sortable="office" (sort)="onSort($event)">Office</th>
                  <th sortable="age" (sort)="onSort($event)">Age</th>
                  <th sortable="startDate" (sort)="onSort($event)">Start Date</th>
                  <th sortable="salary" (sort)="onSort($event)">Salary</th>
                  <th>Action</th>
                </tr>
              </thead>
              <tbody>
                @for (item of basicDataTable$ | async; track item) {
                <tr>
                  <td>{{item.name}}</td>
                  <td>{{item.position}}</td>
                  <td>{{item.office}}</td>
                  <td>{{item.age}}</td>
                  <td>{{item.startDate}}</td>
                  <td>{{item.salary}}</td>
                  <td>
                    <ul class="action">
                      <li class="edit"><a href="javascript:void(0)"><i class="icon-pencil-alt"></i></a></li>
                      <li class="delete"><a href="javascript:void(0)"><i class="icon-trash"></i></a></li>
                    </ul>
                  </td>
                </tr>
                }
                @if (basicData.length === 0) {
                <tr>
                  <td>No matching records found</td>
                </tr>
                }
              </tbody>
            </table>
            <div class="d-flex justify-content-between p-2">
              <select class="form-select" style="width: auto" name="pageSize" [(ngModel)]="service.pageSize">
                <option [ngValue]="10">10 items per page</option>
                <option [ngValue]="25">25 items per page</option>
                <option [ngValue]="50">50 items per page</option>
                <option [ngValue]="100">100 items per page</option>
              </select>
              <ngb-pagination [collectionSize]="(total$ | async)!" [(page)]="service.page"
                [pageSize]="service.pageSize"> </ngb-pagination>
            </div>
          </div>
        </div>
<!-- Datatable Ts Files -->
import { CommonModule, DecimalPipe } from "@angular/common";
import { Component, QueryList, ViewChildren } from "@angular/core";
import { FormsModule } from "@angular/forms";
import { NgbPaginationModule } from "@ng-bootstrap/ng-bootstrap";
import { Observable } from "rxjs";

import { BasicDataTableService } from "../../../shared/services/basicdatatable.service";
import { BasicDataTableDirective, SortEvent } from "../../../shared/directive/basic-data-table.directive";
import { Table } from "../../../shared/data/table/data-table";
import { PageWrapperComponent } from "../../../shared/components/page-wrapper/page-wrapper.component";
import { CardComponent } from "../../../shared/components/card/card.component";

@Component({
    selector: "app-data-tables",
    templateUrl: "./data-tables.component.html",
    styleUrls: ["./data-tables.component.scss"],
    providers: [BasicDataTableService, DecimalPipe],
    imports: [FormsModule, CommonModule, NgbPaginationModule, BasicDataTableDirective, PageWrapperComponent, CardComponent]
})

export class DataTablesComponent {
  public isShow: boolean = false;
  public basicDataTable$: Observable;
  public total$: Observable;
  public basicData: Table[];
  @ViewChildren(BasicDataTableDirective)
  public headers: QueryList;

  constructor(public service: BasicDataTableService) {
    this.basicDataTable$ = service.basicDataTable$;
    this.total$ = service.total$;
  }

  ngOnInit() {
    this.service.basicDataTable$.subscribe((data) => {
      if (data) {
        this.basicData = data;
      }
    });
  }

  onSort({ column, direction }: SortEvent) {
    this.headers.forEach((header) => {
      if (header.sortable !== column) {
        header.direction = "";
      }
    });
    this.service.sortColumn = column;
    this.service.sortDirection = direction;
  }
}

<!-- Datatable Service -->
import { DecimalPipe } from '@angular/common';
import { Injectable, PipeTransform } from '@angular/core';
import { BehaviorSubject, Observable, Subject, debounceTime, delay, of, switchMap, tap } from 'rxjs';
import { Table, basicDataTable } from '../data/table/data-table';
import { SortColumn, SortDirection } from '../directive/basic-data-table.directive';

interface SearchResult {
	basic: Table[];
	total: number;	
}

interface State {
	page: number;
	pageSize: number;
	searchTerm: string;
	sortColumn: SortColumn;
	sortDirection: SortDirection;
}

const compare = (v1: string | number, v2: string | number) => (v1 < v2 ? -1 : v1 > v2 ? 1 : 0);

function sort(basic: Table[], column: SortColumn, direction: string): Table[] {
	if (direction === '' || column === '') {
		return basic;
	} else {
		return [...basic].sort((a, b) => {
			const res = compare(a[column], b[column]);
			return direction === 'asc' ? res : -res;
		});
	}
}

function matches(basic: Table, term: string, pipe: PipeTransform) {
	return (
		basic.name.toLowerCase().includes(term.toLowerCase()) ||
		basic.office.toLowerCase().includes(term.toLowerCase()) ||
		basic.position.toLowerCase().includes(term.toLowerCase()) ||
		basic.salary.toLowerCase().includes(term.toLowerCase()) ||
		basic.startDate.toLowerCase().includes(term.toLowerCase()) ||
		pipe.transform(basic.age).includes(term) ||
		pipe.transform(basic.id).includes(term)
	)
}

@Injectable({
	providedIn: 'root'
})
export class BasicDataTableService {

	private _loading$ = new BehaviorSubject(true);
	private _search$ = new Subject();
	private _basicTable$ = new BehaviorSubject([]);
	private _total$ = new BehaviorSubject(0);

	private _basic: State = {
		page: 1,
		pageSize: 10,
		searchTerm: '',
		sortColumn: '',
		sortDirection: '',
	};

	constructor(private pipe: DecimalPipe) {
		this._search$
			.pipe(
				tap(() => this._loading$.next(true)),
				debounceTime(200),
				switchMap(() => this._search()),
				delay(200),
				tap(() => this._loading$.next(false)),
			)
			.subscribe((result) => {
				this._basicTable$.next(result.basic);
				this._total$.next(result.total);
			});
		this._search$.next();
	}

	get basicDataTable$() {
		return this._basicTable$.asObservable();
	}
	get total$() {
		return this._total$.asObservable();
	}
	get loading$() {
		return this._loading$.asObservable();
	}
	get page() {
		return this._basic.page;
	}
	get pageSize() {
		return this._basic.pageSize;
	}
	get searchTerm() {
		return this._basic.searchTerm;
	}

	set page(page: number) {
		this._set({ page });
	}
	set pageSize(pageSize: number) {
		this._set({ pageSize });
	}
	set searchTerm(searchTerm: string) {
		this._set({ searchTerm });
	}
	set sortColumn(sortColumn: SortColumn) {
		this._set({ sortColumn });
	}
	set sortDirection(sortDirection: SortDirection) {
		this._set({ sortDirection });
	}

	private _set(patch: Partial) {
		Object.assign(this._basic, patch);
		this._search$.next();
	}

	private _search(): Observable {
		const { sortColumn, sortDirection, pageSize, page, searchTerm } = this._basic;

		// 1. sort
		let basic = sort(basicDataTable, sortColumn, sortDirection);

		// 2. filter
		basic = basic.filter((list) => matches(list, searchTerm, this.pipe));
		const total = basic.length;

		// 3. paginate
		basic = basic.slice((page - 1) * pageSize, (page - 1) * pageSize + pageSize);
		return of({ basic, total });
	}
}


<!-- Datatable Directive -->
import { Directive, EventEmitter, Input, Output } from '@angular/core';
import { Table } from '../data/table/data-table';

export type SortColumn = keyof Table | '';
export type SortDirection = 'asc' | 'desc' | '';
const rotate: {[key: string]: SortDirection} = { 'asc': 'desc', 'desc': '', '': 'asc' };

export interface SortEvent {
  column: SortColumn;
  direction: SortDirection;
}

@Directive({
  selector: 'th[sortable]',
  host: {
    '[class.asc]': 'direction === "asc"',
    '[class.desc]': 'direction === "desc"',
    '(click)': 'rotate()',
  },
})

export class BasicDataTableDirective {

  constructor() { }

  @Input() sortable: SortColumn = '';
  @Input() direction: SortDirection = '';
  @Output() sort = new EventEmitter();
  rotate() {
    this.direction = rotate[this.direction];
    this.sort.emit({ column: this.sortable, direction: this.direction });
  }
  
}


<!-- Datatable Data -->
export interface Table {
    id: number;
    name: string;
    position: string;
    office: string;
    startDate: string;
    salary: string;
    age: string;
}

export const basicDataTable: Table[] = [
    {
      id: 1,
      name: 'Tiger Nixon',
      position: 'System Architect',
      office: 'Edinburgh',
      age: '61',
      startDate: '2011/04/25',
      salary: '$320,800',
    },
    {
      id: 2,
      name: 'Garrett Winters',
      position: 'Accountant',
      office: 'Tokyo',
      age: '63',
      startDate: '2011/07/25',
      salary: '$170,750',
  
    },
    {
      id: 3,
      name: 'Ashton Cox',
      position: 'Junior Technical Author',
      office: 'San Francisco',
      age: '66',
      startDate: '2009/01/12',
      salary: '$86,000',
  
    },
    {
      id: 4,
      name: 'Cedric Kelly',
      position: 'Senior Javascript Developer',
      office: 'Edinburgh',
      age: '22',
      startDate: '2012/03/29',
      salary: '$433,060',
  
    },
    {
      id: 5,
      name: 'Airi Satou',
      position: 'Accountant',
      office: 'Tokyo ',
      age: '33',
      startDate: '2008/11/28',
      salary: '$162,700',
  
    },
    {
      id: 6,
      name: 'Brielle Williamson',
      position: 'Integration Specialist',
      office: 'New York ',
      age: '61',
      startDate: '2012/12/02',
      salary: '$372,000',
  
    },
    {
      id: 7,
      name: 'Herrod Chandler',
      position: 'Sales Assistant',
      office: 'San Francisco',
      age: '59',
      startDate: '2012/08/06',
      salary: '$137,500',
  
    },
    {
      id: 8,
      name: 'Rhona Davidson',
      position: 'Integration Specialist',
      office: 'Tokyo',
      age: '55',
      startDate: '2010/10/14',
      salary: '$327,900',
  
    }, 
    {
      id: 9,
      name: 'Colleen Hurst',
      position: 'Javascript Developer',
      office: 'San Francisco',
      age: '39',
      startDate: '2009/09/15',
      salary: '$205,500',
  
    },
    {
      id: 10,
      name: 'Sonya Frost',
      position: 'Software Engineer',
      office: 'Edinburgh',
      age: '23',
      startDate: '2008/12/13',
      salary: '$103,600',
  
    },
    {
      id: 11,
      name: 'Jena Gaines',
      position: 'Office Manager',
      office: 'London',
      age: '30',
      startDate: '2008/12/19',
      salary: '$90,560',
  
    },
    {
      id: 12,
      name: 'Quinn Flynn',
      position: 'Support Lead',
      office: 'Edinburgh',
      age: '22',
      startDate: '2013/03/03',
      salary: '$342,000',
  
    },
    {
      id: 13,
      name: 'Charde Marshall',
      position: 'Regional Director',
      office: 'San Francisco',
      age: '36',
      startDate: '2008/10/16',
      salary: '$470,600',
  
    },
    {
      id: 14,
      name: 'Donna Snider',
      position: 'Customer Support',
      office: 'New York',
      age: '27',
      startDate: '2011/01/25',
      salary: '$112,000',
    },
  ];