ngx-datatable学习经验

ngx-datatable在网络上教程相对较少,并且在官网文档中查看demo需要找到对应的位置,也不方便开发。
所以我将GitHub上的ngx-datatable下载下来运行,与编译器对应。

下载地址:

1
https://github.com/swimlane/ngx-datatable

文档地址:

1
https://swimlane.gitbook.io/ngx-datatable

安装依赖,启动:

1
2
npm i
npm start

搜索demo

在这里插入图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<div>
      <h3>
        Client-side Search and Filtering
        <small>
          <a
            href="https://github.com/swimlane/ngx-datatable/blob/master/src/app/basic/filter.component.ts"
            target="_blank"
          >
            Source
          </a>
        </small>
      </h3>
      <input
        type="text"
        style="padding:8px;margin:15px auto;width:30%;"
        placeholder="Type to filter the name column..."
        (keyup)="updateFilter($event)"
      />
      <ngx-datatable
        #table
        class="material"
        [columns]="columns"
        [columnMode]="ColumnMode.force"
        [headerHeight]="50"
        [footerHeight]="50"
        rowHeight="auto"
        [limit]="10"
        [rows]="rows"
      >
      </ngx-datatable>
</div>

在这里插入图片描述

constructor:

1
2
3
4
5
6
7
8
9
constructor() {
    this.fetch(data => {
      // cache our list 缓存列表
      this.temp = [...data];

      // push our inital complete list 推送我们的初始完整列表
      this.rows = data;
    });
  }

updateFilter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    //toLowerCase() 方法用于把字符串转换为小写
    //indexOf() 返回字符串首次出现的位置
    //target 事件属性可返回事件的目标节点(触发该事件的节点),如生成事件的元素、文档或窗口。
  updateFilter(event) {
   
    const val = event.target.value.toLowerCase();

    //如果有值
    const temp = this.temp.filter(function (d) {
      return d.name.toLowerCase().indexOf(val) !== -1 || !val;
    });

    // update the rows  
    this.rows = temp;
    // Whenever the filter changes, always go back to the first page  
    this.table.offset = 0;
  }
}