【转】https://github.com/valor-software/ng2-table  demo:http://valor-software.com/ng2-table/

ng2-table

一.API

Installation

  1. A recommended way to install ng2-table is through npm package manager using the following command:
npm i ng2-table --save

Usage

import { Ng2TableModule } from 'ng2-table/ng2-table';

or if you want to import specified plugins (Table component is required, the others are optional):

import { NgTableComponent, NgTableFilteringDirective, NgTablePagingDirective, NgTableSortingDirective } from 'ng2-table/ng2-table';

in this case, don't forget to include all of the imported entities to your module

Utilisation

There are only simple table with 3 plugins/directives: filtering, paging, sorting. You don't need special config variable for storing settings for all plugins as is used in demo example. It's just showing usage sample.

Inputs (Properties)

  • page (number) - the default page after the table component loading
  • itemsPerPage (number) - number of the displaying items (rows) on a page
  • maxSize (number) - number of the displaying pages before ...
  • numPages (number) - total number of the pages
  • length (number) - total number of the items after filtering (of it's chosen)

  • config (?any) - config for setup all plugins (filtering, sorting, paging):

    • paging (?boolean) - - switch on the paging plugin
    • sorting (?any) - switch on the sorting plugin
      • columns (Array<any>) - only list of the columns for sorting
    • filtering (?any) - switch on the filtering plugin
      • filterString (string) - the default value for filter
      • columnName (string) - the property name in raw data
    • className (string|Array<string>) - additional CSS classes that should be added to a
  • rows (?Array<any>) - only list of the rows which should be displayed

  • columns (?Array<any>) - config for columns (+ sorting settings if it's needed)
    • title (string) - the title of column header
    • name (string) - the property name in data
    • sort (?string|boolean) - config for columns (+ sorting settings if it's needed), sorting is switched on by default for each column
    • className (string|Array<string>) - additional CSS classes that should be added to a column header
    • filtering (?any) - switch on the filtering plugin
      • filterString (string) - the default value for filter
      • columnName (string) - the property name in raw data

Outputs (Events)

  • tableChanged: data change event handler
  • cellClicked: onclick event handler

Filter

The responsibility of the filtering issue falls on user. You should choose on which columns the filter would be applied. You could add any number of different filters. Filter string - it's a string for matching values in raw data. Column name refers to the property name in raw data. The rest logic you could organize by yourself (the order of filters, data formats, etc). Even you could use filter for list of data columns.

You can also set up filtering param for columns, in this case filter box will appear in first row of the table.

Sorting

Data sorting could be in 3 modes: asc, desc and without sorting data (as it comes from backend or somewhere else). If you want to switch off the sorting for some of the columns then you should set it forcibly in columns config (set property sort to false value for each column you want)

Paging

Pagination could be used from ng2-bootstrap - pagination component. When the page is changed, the pagination component will emit event tableChanged with an object {page, itemsPerPage}. Then you can easily subscribe on it and request corresponding raw data.

二.html

<div class="row">
<div class="col-md-4">
<input *ngIf="config.filtering" placeholder="Filter all columns"
[ngTableFiltering]="config.filtering"
class="form-control"
(tableChanged)="onChangeTable(config)"/>
</div>
</div>
<br>
<ng-table [config]="config"
(tableChanged)="onChangeTable(config)"
(cellClicked)="onCellClick($event)"
[rows]="rows" [columns]="columns">
</ng-table>
<pagination *ngIf="config.paging"
class="pagination-sm"
[(ngModel)]="page"
[totalItems]="length"
[itemsPerPage]="itemsPerPage"
[maxSize]="maxSize"
[boundaryLinks]="true"
[rotate]="false"
(pageChanged)="onChangeTable(config, $event)"
(numPages)="numPages = $event">
</pagination>
<pre *ngIf="config.paging" class="card card-block card-header">Page: {{page}} / {{numPages}}</pre>

三.typescript

import { Component, OnInit } from '@angular/core';
import { TableData } from './table-data'; // webpack html imports
let template = require('./table-demo.html'); @Component({
selector: 'table-demo',
template
})
export class TableDemoComponent implements OnInit {
public rows:Array<any> = [];
public columns:Array<any> = [
{title: 'Name', name: 'name', filtering: {filterString: '', placeholder: 'Filter by name'}},
{
title: 'Position',
name: 'position',
sort: false,
filtering: {filterString: '', placeholder: 'Filter by position'}
},
{title: 'Office', className: ['office-header', 'text-success'], name: 'office', sort: 'asc'},
{title: 'Extn.', name: 'ext', sort: '', filtering: {filterString: '', placeholder: 'Filter by extn.'}},
{title: 'Start date', className: 'text-warning', name: 'startDate'},
{title: 'Salary ($)', name: 'salary'}
];
public page:number = 1;
public itemsPerPage:number = 10;
public maxSize:number = 5;
public numPages:number = 1;
public length:number = 0; public config:any = {
paging: true,
sorting: {columns: this.columns},
filtering: {filterString: ''},
className: ['table-striped', 'table-bordered']
}; private data:Array<any> = TableData; public constructor() {
this.length = this.data.length;
} public ngOnInit():void {
this.onChangeTable(this.config);
} public changePage(page:any, data:Array<any> = this.data):Array<any> {
let start = (page.page - 1) * page.itemsPerPage;
let end = page.itemsPerPage > -1 ? (start + page.itemsPerPage) : data.length;
return data.slice(start, end);
} public changeSort(data:any, config:any):any {
if (!config.sorting) {
return data;
} let columns = this.config.sorting.columns || [];
let columnName:string = void 0;
let sort:string = void 0; for (let i = 0; i < columns.length; i++) {
if (columns[i].sort !== '' && columns[i].sort !== false) {
columnName = columns[i].name;
sort = columns[i].sort;
}
} if (!columnName) {
return data;
} // simple sorting
return data.sort((previous:any, current:any) => {
if (previous[columnName] > current[columnName]) {
return sort === 'desc' ? -1 : 1;
} else if (previous[columnName] < current[columnName]) {
return sort === 'asc' ? -1 : 1;
}
return 0;
});
} public changeFilter(data:any, config:any):any {
let filteredData:Array<any> = data;
this.columns.forEach((column:any) => {
if (column.filtering) {
filteredData = filteredData.filter((item:any) => {
return item[column.name].match(column.filtering.filterString);
});
}
}); if (!config.filtering) {
return filteredData;
} if (config.filtering.columnName) {
return filteredData.filter((item:any) =>
item[config.filtering.columnName].match(this.config.filtering.filterString));
} let tempArray:Array<any> = [];
filteredData.forEach((item:any) => {
let flag = false;
this.columns.forEach((column:any) => {
if (item[column.name].toString().match(this.config.filtering.filterString)) {
flag = true;
}
});
if (flag) {
tempArray.push(item);
}
});
filteredData = tempArray; return filteredData;
} public onChangeTable(config:any, page:any = {page: this.page, itemsPerPage: this.itemsPerPage}):any {
if (config.filtering) {
Object.assign(this.config.filtering, config.filtering);
} if (config.sorting) {
Object.assign(this.config.sorting, config.sorting);
} let filteredData = this.changeFilter(this.data, this.config);
let sortedData = this.changeSort(filteredData, this.config);
this.rows = page && config.paging ? this.changePage(page, sortedData) : sortedData;
this.length = sortedData.length;
} public onCellClick(data: any): any {
console.log(data);
}
}
 

最新文章

  1. Markdown 语法说明
  2. 离线安装Cloudera Manager 5和CDH5(最新版5.1.3) 完全教程
  3. 微软IIS对http keep-alive的“霸道”处理
  4. 破解TP-Link路由-嗅探PPPoE拨号密码
  5. MVC学习笔记---MVC导出excel(数据量大,非常耗时的,异步导出)
  6. Python 手册——调用解释器
  7. 《深入Linux内核》 UNIX的一些故事
  8. python中的有趣用法
  9. 算法笔记_017:递归执行顺序的探讨(Java)
  10. ACM讲座心得
  11. 如何 Scale Up/Down Deployment?- 每天5分钟玩转 Docker 容器技术(126)
  12. python 安装numpy遇到无法卸载的解决办法
  13. APP-SERVICE-SDK:setStorageSync:fail;at page/near/pages/shops/shops page lifeCycleMethod onUnload function
  14. 扩展方法、委托和Lambda
  15. Confluence 6 导入一个 Confluence 站点
  16. Java 判断字符串 中文是否为乱码
  17. ts-loader 安装问题
  18. Linux通配符与基础正则表达式、扩展正则表达式
  19. async &amp; await 异步编程小示例,一看就懂
  20. springmvc之url参数传递

热门文章

  1. 设置DataGridView不自动创建生成列
  2. SQL自动流水号函数
  3. Protobuf3 编解码
  4. vim简单的移动光标
  5. web多站点跨域访问
  6. List分组
  7. python 遍历hadoop, 跟指定列表对比 包含列表中值的取出。
  8. Direct3D 11 Tutorial 4: 3D Spaces_Direct3D 11 教程4:3D空间
  9. elasticsearch以及head插件在centos7上的安装与配置教程
  10. phpmyadmin 上传超过50m限制