1.7.1 • Published 5 years ago

es-ng6-bs4-table v1.7.1

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

ES Angular 6 Bootstrap 4 Table

ToDo List

  • editable
  • more method
  • more events
  • more handler

Features

  • create for bootstrap 4
  • card view in mobile
  • get json data from server
  • flexible header and cell template
  • support client and server pagination
  • keep params on url
  • easy to extend tool
  • i18n

Table of contents

Setup

First you need to install the npm module

npm install es-ng6-bs4-table

Then import bootstrap 4 css in angular.json

{
  "styles": [
    "../node_modules/bootstrap/dist/css/bootstrap.css",
    "../node_modules/open-iconic/font/css/open-iconic-bootstrap.css"
  ]
}

Last import the 'HttpClientModule' and 'BsTableModule' to module

import {NgModule} from "@angular/core"
import {HttpClientModule} from "@angular/common/http";
import {BsTableModule} from "es-ng6-bs4-table";

@NgModule{
    imports: [
        HttpClientModule,
        BsTableModule,
        //if you want to keep search result
        //RouterModule
    ],
    /* if you want to add some http interceptors
    providers: [
        {provide: HTTP_INTERCEPTORS ...}
    ]
    */
}
export class AppModule{}

Usage

Client side pagination

<es-ng-table></es-ng-table>

Client side pagination structure

Array<any>

Server side pagination

<es-ng-sd-table></es-ng-sd-table>

Server side pagination structure

{
    rows: Array<any>;
    total: number;
}

Server side params

{
    limit: number;
    offset: number;
    sort: string;
    order: string;
    search: string;
    query: string; //json
}

Basic Usage

image

ts:

export class AppComponent {
  data = [
    {id: 1, name: "Sean Johnston", score: 99, description: "d1"},
    {id: 2, name: "Morgan Davies", score: 80, description: "d2"},
    {id: 3, name: "Morgan John", score: 80, description: "d3"},
    {id: 4, name: "Tommy Walker", score: 80, description: "d4"},
    {id: 5, name: "William Lee", score: 80, description: "d5"},
    {id: 6, name: "Russell Brady", score: 80, description: "d6"},
    {id: 7, name: "Isaiah Ferguson", score: 80, description: "d7"},
    {id: 8, name: "Dominic Lynch", score: 80, description: "d8"},
    {id: 9, name: "Alberto Walls", score: 80, description: "d9"},
    {id: 10, name: "Jerry Pate", score: 80, description: "d10"},
    {id: 11, name: "Spencer Gordon", score: 79, description: "d11"},
  ]
}

html:

<div class="container-fluid">
  <h1>Basic Use</h1>
  <es-ng-table [data]="data">
    <es-ng-table-col field="state" [checkbox]="true"></es-ng-table-col>
    <es-ng-table-col title="ID" field="id"></es-ng-table-col>
    <es-ng-table-col title="Name" field="name"></es-ng-table-col>
    <es-ng-table-col title="Score" field="score"></es-ng-table-col>
    <es-ng-table-col title="Description" field="description"></es-ng-table-col>
  </es-ng-table>
</div>

Formatter and Text Template

image

ts:

export class AppComponent {
  formatter = (value: any) => {
    return value + ' postfix'
  }
}

html:

<es-ng-table [data]="data">
    <es-ng-table-col title="Name" field="name"></es-ng-table-col>
    <es-ng-table-col title="Score" field="score">
      <ng-template #textTemplate let-row="row" let-value="value" let-index="index" let-formatter="formatter">
        <button>{{value}}</button>
      </ng-template>
    </es-ng-table-col>
    <es-ng-table-col title="Description" field="description" [formatter]="formatter"></es-ng-table-col>
</es-ng-table>

Keep

image

IMPORTANT: NEED IMPORT "RouterModule". ONLY ONE KEEP IN ONE PAGE.

You will see url change after any params changes(like: page, size, search...)

html:

<es-ng-table [data]="data" [keep]="true"></es-ng-table>

Fetch Data From API

html:

<es-ng-table url="http://domain/path">
   <es-ng-table-col field="state" [checkbox]="true"></es-ng-table-col>
   <es-ng-table-col title="ID" field="id"></es-ng-table-col>
   <es-ng-table-col title="Name" field="name"></es-ng-table-col>
</es-ng-table>

i18n

The default locale is en-us. Just provide "NG_TABLE_I18N" to change locale.

@NgModule{
    imports: [BsTableModule],
    providers: [
        {provide: NG_TABLE_I18N, useClass: NgTableZhTwService}
    ]
}

Current locale support: 'zh-tw', 'en-us'

Tool

add tool in html:

<es-ng-table>
  <es-ng-table-tool-bar>
    <div class="row">
      <!--add tool here-->
    </div>
  </es-ng-table-tool-bar>
</es-ng-table>

General Search

image html:

<es-ng-table [data]="data">
    <es-ng-table-tool-bar>
      <div class="row">
        <es-ng-table-general-search></es-ng-table-general-search>
      </div>
    </es-ng-table-tool-bar>
    <es-ng-table-col title="ID" field="id"></es-ng-table-col>
    <es-ng-table-col title="Name" field="name"></es-ng-table-col>
</es-ng-table>

Advanced Search

image You can user <ng-template #editTemplate let-col="col" let-obj="obj"> to customize form control

html:

<es-ng-table [data]="data">
    <es-ng-table-tool-bar>
      <div class="row">
        <es-ng-table-advanced-search></es-ng-table-advanced-search>
      </div>
    </es-ng-table-tool-bar>
    <es-ng-table-col title="ID" field="id"></es-ng-table-col>
    <es-ng-table-col title="Name" field="name">
        <ng-template #editTemplate let-col="col" let-obj="obj">
           <select class="form-control" name="{{col.field}}" [(ngModel)]="obj[col.field]">
              <option *ngFor="let row of data" [ngValue]="row.name">{{row.name}}</option>
            </select>
        </ng-template>
    </es-ng-table-col>
</es-ng-table>

Hide Column

image html:

<es-ng-table [data]="data">
    <es-ng-table-tool-bar>
      <div class="row col-md-12">
        <es-ng-table-hide-col></es-ng-table-hide-col>
      </div>
    </es-ng-table-tool-bar>
    <es-ng-table-col field="state" [checkbox]="true"></es-ng-table-col>
    <es-ng-table-col title="ID" field="id"></es-ng-table-col>
    <es-ng-table-col title="Name" field="name"></es-ng-table-col>
</es-ng-table>

Extend Tool

create tool component with inject NG_TABLE_TOKEN

export class NgTableGeneralSearchComponent implements OnInit {
  searchTerm: string = "";

  constructor(
    @Inject(NG_TABLE_TOKEN) private table: NgTable
  ) { }

  ngOnInit() {
    this.table.search$
      .pipe(take(1))
      .subscribe((term) =>this.searchTerm = term);
  }

  search(searchTerm: string){
    this.table.generalSearch(searchTerm);
  }

}

then, put this component in

<es-ng-table [data]="data">
    <es-ng-table-tool-bar>
      <es-ng-table-general-search></es-ng-table-general-search>
    </es-ng-table-tool-bar>
</es-ng-table>

Directive

esSearchCaseSensitive

The default search is case sensitive. Just add this change it.

<es-ng-table [esSearchCaseSensitive]="false"></es-ng-table>

esAdvancedSearchCaseSensitive

The default advanced search is case sensitive. Just add this to change it.

<es-ng-table [esAdvancedSearchCaseSensitive]="false"></es-ng-table>

Variable

NgTableComponent & NgSdTableComponent

NameTypeDefaultDescription
dataArraydata of rows.
rowsArray[]current data of rows.
totalnumbersize of total data.
pagenumber1current page.
sizenumber10size in a page.
searchstringsearch text, used in general search.
querystringquery json, user in advanced search.
sortstringsort by field.
order'asc' 'desc''asc'order.
urlstringget json from url.
keepbooleanfalsekeep search result.

NgTableColComponent

NameTypeDefaultDescription
titlestringth text.
fieldstringfield name in row.
checkboxbooleanfalseis checkbox cell?
radiobooleanfalseis radio cell?
hiddenbooleanfalseis column hidden?

Method

NgTableComponent & NgSdTableComponent

NameParamReturnDescription
refreshvoidrefresh rows.
generalSearch(term: string)voidgeneral search.
advancedSearch(query: any)voidadvance search.
getSelections(field: string)Arrayget selected rows.
hideColumn(col: NgTableColComponent)voidhide column.
showColumn(col: NgTableColComponent)voidshow column.
toggleColumn(col: NgTableColComponent)voidtoggle column.
hideColumnByIndex(index: number)voidhide column by index.
showColumnByIndex(index: number)voidshow column by index.
toggleColumnByIndex(index: number)voidtoggle column by index.
hideColumnByField(field: string)voidhide column by field.
showColumnByField(field: string)voidshow column by field.
toggleColumnByField(field: string)voidtoggle column by field.

NgTableColComponent

NameParamReturnDescription
hidevoidhide column.
showvoidshow column.
togglevoidtoggle column.

Input

NgTableComponent & NgSdTableComponent

NameTypeDefaultDescription
dataArraydata of rows.
urlstringget json from url.
sortstringsort by field.
order'asc' 'desc''asc'order.
keepbooleanfalsekeep search result.
queryFunFunction(client pagination) advanced search. (row: any, index: number, query: any) => Array
responseHandlerFunctionhandler after get data from url. (data: any) => any

NgTableColComponent

NameTypeDefaultDescription
titlestringth text.
fieldstringfield name in row.
checkboxbooleanfalseis checkbox cell?
radiobooleanfalseis radio cell?
hiddenbooleanfalseis column hidden?
classArrayArray[]class of th and td.
formatterFunctionformatter cell. (value: any) => string

Output

NgTableComponent & NgSdTableComponent

NameTypeDescription
onRowClickanyfired when row be clicked.
onInitKeepParamsanyfired when init keep params

NgTableColComponent

NameTypeDescription
onCellClickanyfired when cell be clicked.

Demo

  git clone https://github.com/endsound0211/es-ng6-bs4-table.git
  npm install
  ng serve
1.7.1

5 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.4

6 years ago

1.5.3

6 years ago

1.5.2

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago