【发布时间】:2020-01-02 01:32:37
【问题描述】:
我正在使用 Angular 7 和 Angular DataTables,“角度方式”。我想为每一列设置一个过滤器,例如“单独的列搜索”-> https://l-lin.github.io/angular-datatables/#/advanced/individual-column-filtering,在那里显示“不是角度方式”。
我尝试将“角度方式”(https://l-lin.github.io/angular-datatables/#/basic/angular-way)与“其他方式”结合起来,但过滤器不起作用。只有右上角的全局过滤器在工作。似乎没有对过滤器的列的引用。
export class AppComponent implements OnInit, AfterViewInit, OnDestroy {
@ViewChild(DataTableDirective)
datatableElement: DataTableDirective;
dtOptions: DataTables.Settings = {};
users: User[] = [];
dtTrigger: Subject<User> = new Subject();
constructor(private http: HttpClient) {}
ngOnInit(): void {
this.dtOptions = {
pagingType: 'full_numbers',
pageLength: 2
};
this.http.get<User[]>('https://l-lin.github.io/angular-datatables/data/data.json')
.subscribe(users => {
this.users = users;
this.dtTrigger.next();
});
}
ngAfterViewInit(): void {
this.datatableElement.dtInstance.then((dtInstance: DataTables.Api) => {
dtInstance.columns().every(function () {
const that = this;
$('input', this.footer()).on('keyup change', function () {
if (that.search() !== this['value']) {
that
.search(this['value'])
.draw();
}
});
});
});
}
<table datatable="ng" [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" class="row-border hover">
<tfoot>
<tr>
<th><input type="text" placeholder="Search ID" name="search-id"/></th>
<th><input type="text" placeholder="Search first name" name="search-first-name"/></th>
<th><input type="text" placeholder="Search last name" name="search-last-name"/></th>
</tr>
</tfoot>
<thead>
<tr>
<th>ID</th>
<th>First name</th>
<th>Last name</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of users.data">
<td>{{ user.id }}</td>
<td>{{ user.firstName }}</td>
<td>{{ user.lastName }}</td>
</tr>
</tbody>
</table>
我希望过滤器会像全局过滤器一样工作,但它们不会做出反应。
我不相信我是第一个想尝试这个的人。但不幸的是我找不到任何解决方案。 我找到了
【问题讨论】:
标签: angular filter datatable filtering angular7