ng2-table

【转】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);
  }
}
时间: 2024-10-13 22:23:02

ng2-table的相关文章

现在主流网站为什么都用div+css布局而不是用table

由于刚刚接触前端,一直觉得table布局在代码上看起来比div+css更整洁,div+css布局的页面,一堆的<div><div><div>...</div></div></div>看起来都让人犯密集恐惧症,那么为什么现在的主流网站还都乐此不疲呢?为什么div+css反而成了一种主流布局方式呢?一直对此不解.这篇文章好像是解决了我的问题,先摘录过来,以便查阅. 以下内容摘自:http://www.divcss5.com/wenji/w

&lt;table&gt;标签总结(colspan跨列 ,rowspan跨行)

table标签有些内置属性要设置: <table cellpadding="0" cellspacing="0" border="0" summary="各银行的网上银行支付限额总表"> 1.摘要summary的内容是不会在浏览器中显示出来的.它的作用是增加表格的可读性(语义化), 使搜索引擎更好的读懂表格内容,还可以使屏幕阅读器更好的帮助特殊用户读取表格内容. 2.

关于hibernate中提示can not create table ******

最近这两天,一直在搞hibernate,被 其中一个问题困扰了好久.在网上找了好久,一直都没有找到可行的方法,把jdk,jre,tomcat,装了拆,拆了装,可就是搞不好.实在是没有办法,又重新建了一个项目来测试,,从一点一滴开始试.所有的代码全部都手写,,,,可是问题依旧还在. 之所以一直没有找到解决办法,其实是自己的思维一直局限在一处,,,一直在想着hibernate只要把配置文件配置好,它就可以自己在数据库创建表结构,自己增删改查,相当的方便,可是自己 当时成功了,为什么这次却一而再再而三

【R】数据导入读取read.table函数详解,如何读取不规则的数据(fill=T)

函数 read.table 是读取矩形格子状数据最为便利的方式.因为实际可能遇到的情况比较多,所以预设了一些函数.这些函数调用了 read.table 但改变了它的一些默认参数.  注意,read.table 不是一种有效地读大数值矩阵的方法:见下面的 scan 函数. 一些需要考虑到问题是: 编码问题 如果文件中包含非-ASCII字符字段,要确保以正确的编码方式读取.这是在UTF-8的本地系统里面读取Latin-1文件的一个主要问题.此时,可以如下处理 read.table(file("fil

bootstrap table 服务器端分页--ashx+ajax

1.准备静态页面 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title></title> 6 <meta charset="utf-8" /> 7 <link rel="

mysql启动报错:Fatal error: Can’t open and lock privilege tables: Table ‘mysql.host’ doesn’t exist

mysql在首次启动的时候可能会报错:Can’t open and lock privilege tables: Table ‘mysql.host’ doesn’t exist 这时候可以执行脚本 mysql_install_db –user=mysql –ldata=数据存放的路径

div与table用作布局的区别

本文向大家描述一下DIV和Table页面布局的区别和联系,一般来说Table开发快,容易控制,浏览器兼容也好些,另一部分认为DIV好,是以后的发展趋势,主要原因请看下文详细介绍. DIV布局和Table页面布局的区别和联系 现在对于网页制作是选择传统的Table还是用新型的DIV,有分歧.一部分说还是用Table好,开发快,容易控制,浏览器兼容也好些;另一部分认为DIV好,以后的发展趋势,主要是如下原因: DIV+CSS布局比Table布局节省页面代码,代码结构也更清晰明了. DIV+CSS开发

doesn&#39;t contain a valid partition table 解决方法

输入 fdisk -l 可以看到 输入 fdisk /dev/xvdb 跟着向导一步步做下去(如果不知道该输入什么,就输入“m”并回车,可以打印出菜单): Command (m for help): m Command action a   toggle a bootable flag b   edit bsd disklabel c   toggle the dos compatibility flag d   delete a partition l   list known partiti

html页面里table导出为excel

只要想不到,没有做不到,一直都不知道html里table元素可直接导出为excel,太牛逼了! 这其中用到一个jquery的插件table2excel 使用方法也很简单: 1 . 包含必要的文件 <script src="src/1.11.1/jquery.min.js"></script> <script src="src/jquery.table2excel.js"></script> 2. 创建导出按钮 <

Using JavaFX UI Controls 12 Table View

原文链接地址:http://docs.oracle.com/javafx/2/ui_controls/table-view.htm#CJAGAAEE 在这一章,你将学习如:添加一个表格表.数据填充.编辑表格行等格组件 JavaFx的基本操作. 很多JavaFX SDK API种的类为在表格表单中呈现数据.在JavaFX 应用中对创建表格最重要的是TableView, TableColumn和TableCell这三个类. 你可以通过实现数据模型(data model) 和 实现  单元格工厂(ce