Angular 4 Ngx-datatable Filter not working object is null but accessed (even with if condition)
Angular 4 Ngx-datatable Filter not working object is null but accessed (even with if condition)
我正在使用 ngx-datatable 并尝试向我的代码添加过滤功能,但它根本不起作用。
这是我的代码:
import {Component, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {NavbarService} from '../../Services/navbar.service';
import {DataService} from '../../Services/data.service';
import {DatatableComponent} from '@swimlane/ngx-datatable';
import {forEach} from '@angular/router/src/utils/collection';
@Component({
selector: 'app-student',
templateUrl: './student.component.html',
styleUrls: ['./student.component.css'],
encapsulation: ViewEncapsulation.None
})
export class StudentComponent implements OnInit {
rows: Student[];
@ViewChild('myTable') table: any;
students: Student[];
temp = [];
constructor(private nav: NavbarService, public dataService: DataService) {
this.dataService.getStudents().subscribe(Students => {
this.Students = [...Students];
this.rows = Students;
});
}
ngOnInit() {
this.nav.show();
}
onPage(event: Event) {
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
console.log('paged!', event);
}, 100);
}
toggleExpandRow(row) {
console.log('Toggled Expand Row!', row);
this.table.rowDetail.toggleExpandRow(row);
}
onDetailToggle(event) {
console.log('Detail Toggled', event);
}
updateFilter(event) {
const val = event.target.value.toLowerCase();
this.rows = this.students.filter(row =>
row.AspNetUsers.Nom.toLowerCase().indexOf(val) !== -1 || !val
);
this.table.offset = 0;
}
}
就像在文档代码中一样,我必须向我的输入添加一个函数 updateFilter
来获取输入的值,然后有一个函数根据插入的值过滤临时数组,但这个函数不会根本不起作用 我得到 Cannot read property 'Name' of undefined
我使用控制台检查了它们数组的值并调试它似乎数组是正确的并且有值但我不知道为什么过滤器函数 returns 没有。我试过添加 try - catch 问题消失了,但是过滤函数什么也没过滤。
编辑:student.component.html
的代码
<div class="container">
<div class="row">
<div class="col">
<form>
<div class="input-group">
<input class="form-control" id="search" name="search" (keyup)='updateFilter($event)' placeholder="Rechercher">
<div class="input-group-addon"><i class="material-icons">search</i></div>
</div>
</form>
</div>
</div>
<div class="row mt-3">
<div class="col">
<ngx-datatable
#myTable
class='material expandable'
[columnMode]="'force'"
[headerHeight]="50"
[footerHeight]="50"
[rowHeight]="50"
[rows]='rows'
[limit]="20"
(page)="onPage($event)">
<ngx-datatable-row-detail [rowHeight]="120" #myDetailRow (toggle)="onDetailToggle($event)">
<ng-template let-row="row" let-expanded="expanded" ngx-datatable-row-detail-template>
<div class="container">
<table class="table">
<tr>
<th>Email</th>
<td>{{row.AspNetUsers.Email}}</td>
</tr>
<tr>
<th>Adresse</th>
<td>{{row.ADRESSE}}</td>
</tr>
</table>
</div>
</ng-template>
</ngx-datatable-row-detail>
<ngx-datatable-column
[width]="50"
[resizeable]="false"
[sortable]="false"
[draggable]="false"
[canAutoResize]="false">
<ng-template let-row="row" let-expanded="expanded" ngx-datatable-cell-template>
<a
href="javascript:"
[class.datatable-icon-right]="!expanded"
[class.datatable-icon-down]="expanded"
title="Expand/Collapse Row"
(click)="toggleExpandRow(row)">
</a>
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="ID" [width]="100">
<ng-template let-row="row" ngx-datatable-cell-template>
{{row.studentID}}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="First Name" [width]="100">
<ng-template let-row="row" ngx-datatable-cell-template>
{{row.AspNetUsers.Name}}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="Last Name" [width]="100">
<ng-template let-row="row" ngx-datatable-cell-template>
{{row.AspNetUsers.LastName}}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="Special Code" [width]="100">
<ng-template let-row="row" ngx-datatable-cell-template>
{{row.AspNetUsers.SpecialCode}}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="PhoneNumber" [width]="100">
<ng-template let-row="row" ngx-datatable-cell-template>
{{row.AspNetUsers.PhoneNumber}}
</ng-template>
</ngx-datatable-column>
</ngx-datatable>
</div>
</div>
</div>
编辑 2:
我发现了为什么我收到错误,缺少一些名称(它们是空的)并且在过滤器内部如果它是空的就会发生问题。
我添加了 student !== null 但问题仍然存在。
这显然是 HTML 中的一个错误,它破坏了您的过滤。
将安全导航运算符放入行的每个插值中,如:
{{row.AspNetUsers?.Name}}
{{row.AspNetUsers?.SpecialCode}}
等等。应该消除错误,这样你就可以更深入地调试。
我正在使用 ngx-datatable 并尝试向我的代码添加过滤功能,但它根本不起作用。
这是我的代码:
import {Component, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {NavbarService} from '../../Services/navbar.service';
import {DataService} from '../../Services/data.service';
import {DatatableComponent} from '@swimlane/ngx-datatable';
import {forEach} from '@angular/router/src/utils/collection';
@Component({
selector: 'app-student',
templateUrl: './student.component.html',
styleUrls: ['./student.component.css'],
encapsulation: ViewEncapsulation.None
})
export class StudentComponent implements OnInit {
rows: Student[];
@ViewChild('myTable') table: any;
students: Student[];
temp = [];
constructor(private nav: NavbarService, public dataService: DataService) {
this.dataService.getStudents().subscribe(Students => {
this.Students = [...Students];
this.rows = Students;
});
}
ngOnInit() {
this.nav.show();
}
onPage(event: Event) {
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
console.log('paged!', event);
}, 100);
}
toggleExpandRow(row) {
console.log('Toggled Expand Row!', row);
this.table.rowDetail.toggleExpandRow(row);
}
onDetailToggle(event) {
console.log('Detail Toggled', event);
}
updateFilter(event) {
const val = event.target.value.toLowerCase();
this.rows = this.students.filter(row =>
row.AspNetUsers.Nom.toLowerCase().indexOf(val) !== -1 || !val
);
this.table.offset = 0;
}
}
就像在文档代码中一样,我必须向我的输入添加一个函数 updateFilter
来获取输入的值,然后有一个函数根据插入的值过滤临时数组,但这个函数不会根本不起作用 我得到 Cannot read property 'Name' of undefined
我使用控制台检查了它们数组的值并调试它似乎数组是正确的并且有值但我不知道为什么过滤器函数 returns 没有。我试过添加 try - catch 问题消失了,但是过滤函数什么也没过滤。
编辑:student.component.html
的代码<div class="container">
<div class="row">
<div class="col">
<form>
<div class="input-group">
<input class="form-control" id="search" name="search" (keyup)='updateFilter($event)' placeholder="Rechercher">
<div class="input-group-addon"><i class="material-icons">search</i></div>
</div>
</form>
</div>
</div>
<div class="row mt-3">
<div class="col">
<ngx-datatable
#myTable
class='material expandable'
[columnMode]="'force'"
[headerHeight]="50"
[footerHeight]="50"
[rowHeight]="50"
[rows]='rows'
[limit]="20"
(page)="onPage($event)">
<ngx-datatable-row-detail [rowHeight]="120" #myDetailRow (toggle)="onDetailToggle($event)">
<ng-template let-row="row" let-expanded="expanded" ngx-datatable-row-detail-template>
<div class="container">
<table class="table">
<tr>
<th>Email</th>
<td>{{row.AspNetUsers.Email}}</td>
</tr>
<tr>
<th>Adresse</th>
<td>{{row.ADRESSE}}</td>
</tr>
</table>
</div>
</ng-template>
</ngx-datatable-row-detail>
<ngx-datatable-column
[width]="50"
[resizeable]="false"
[sortable]="false"
[draggable]="false"
[canAutoResize]="false">
<ng-template let-row="row" let-expanded="expanded" ngx-datatable-cell-template>
<a
href="javascript:"
[class.datatable-icon-right]="!expanded"
[class.datatable-icon-down]="expanded"
title="Expand/Collapse Row"
(click)="toggleExpandRow(row)">
</a>
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="ID" [width]="100">
<ng-template let-row="row" ngx-datatable-cell-template>
{{row.studentID}}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="First Name" [width]="100">
<ng-template let-row="row" ngx-datatable-cell-template>
{{row.AspNetUsers.Name}}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="Last Name" [width]="100">
<ng-template let-row="row" ngx-datatable-cell-template>
{{row.AspNetUsers.LastName}}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="Special Code" [width]="100">
<ng-template let-row="row" ngx-datatable-cell-template>
{{row.AspNetUsers.SpecialCode}}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="PhoneNumber" [width]="100">
<ng-template let-row="row" ngx-datatable-cell-template>
{{row.AspNetUsers.PhoneNumber}}
</ng-template>
</ngx-datatable-column>
</ngx-datatable>
</div>
</div>
</div>
编辑 2: 我发现了为什么我收到错误,缺少一些名称(它们是空的)并且在过滤器内部如果它是空的就会发生问题。 我添加了 student !== null 但问题仍然存在。
这显然是 HTML 中的一个错误,它破坏了您的过滤。 将安全导航运算符放入行的每个插值中,如:
{{row.AspNetUsers?.Name}}
{{row.AspNetUsers?.SpecialCode}}
等等。应该消除错误,这样你就可以更深入地调试。