I'm using Angular 7 and Angular DataTables, "the angular way". I want to have a filter for each column, like "Individual column searching" -> https://l-lin.github.io/angular-datatables/#/advanced/individual-column-filtering, which is shown there "not the angular way".
I tried to combine "the angular way"(https://l-lin.github.io/angular-datatables/#/basic/angular-way) with the "other way", but the filter is not working. Only the global filter in the right top corner is working. It looks like there is no reference of the columns to the filters.
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>
I expect that the filters will work like the global filter, but they don't react.
I don't believe that I'm the first one who wants to try this. But unfortunately I can't find any solution. I found