admin管理员组文章数量:1358086
I have a set of data in which is filtering by checkbox. However it only works one time. For example. I have 2 distinct company If I check company it filters and shows two rows and I uncheck it shows all rows which is good. But if I check it again it doesn't filter. Seems like I have some sort of state management or data binding problem. Here is my apply filter method that shows me trying to use filter predicate and also trying to filter manually.
applyFilter(): void {
console.log('mockData************', this.mockData);
if (!this.mockData || this.mockData.length === 0) {
console.error('No Data to Filter');
return;
}
let filteredData = this.mockData; // Start with the full data
if (this.identifierFilterValue && this.identifierFilterValue.length > 0) {
filteredData = this.filterByIdentityCheckbox(filteredData);
}
console.log('Filtered Data ', filteredData);
if (this.dataSource) {
this.dataSource.data = filteredData;
this.dataSource.paginator = this.paginator; // Assign paginator here
}
if (this.dataSource && this.dataSource.paginator) {
this.dataSource.paginator.firstPage();
}
}
filterByIdentityCheckbox(data: any[]): any[] {
console.log(
'******Applying filter by identifierFilterValue:',
this.identifierFilterValue
);
if (
!this.identifierFilterValue ||
this.identifierFilterValue.length === 0
) {
console.log('No Identifier Filters are selected return full data');
return [...data];
}
return data.filter((item) => {
console.log('Checking item identifier:', item.identifier);
if (this.identifierFilterValue.indexOf(item.identifier) !== -1) {
console.log('Matched identifier:', item.identifier);
return true;
} else {
console.log('Identifier not matched', item.identifier);
return false;
}
});
}
}
I've also tried includes instead of indexOf
filterByIdentityCheckbox(data: any[]): any[] {
console.log('******Applying filter by identifierFilterValue:',
this.identifierFilterValue);
if (!this.identifierFilterValue ||
this.identifierFilterValue.length === 0) {
console.log('No Identifier Filters are selected return full data');
return [...data];
}
return data.filter(item => {
console.log('Checking item identifier:', item.identifier);
if (this.identifierFilterValue.includes(item.identifier)) {
console.log('Matched identifier:', item.identifier);
return true;
} else {
console.log('Identifier not matched', item.identifier);
return false;
}
});
}
I have a parent component, filter component, and view table component.
I read something about using lodash as ngOnChanges doesn't work the second time...let me do some reading.
Here is my stackblitz
I have a set of data in which is filtering by checkbox. However it only works one time. For example. I have 2 distinct company If I check company it filters and shows two rows and I uncheck it shows all rows which is good. But if I check it again it doesn't filter. Seems like I have some sort of state management or data binding problem. Here is my apply filter method that shows me trying to use filter predicate and also trying to filter manually.
applyFilter(): void {
console.log('mockData************', this.mockData);
if (!this.mockData || this.mockData.length === 0) {
console.error('No Data to Filter');
return;
}
let filteredData = this.mockData; // Start with the full data
if (this.identifierFilterValue && this.identifierFilterValue.length > 0) {
filteredData = this.filterByIdentityCheckbox(filteredData);
}
console.log('Filtered Data ', filteredData);
if (this.dataSource) {
this.dataSource.data = filteredData;
this.dataSource.paginator = this.paginator; // Assign paginator here
}
if (this.dataSource && this.dataSource.paginator) {
this.dataSource.paginator.firstPage();
}
}
filterByIdentityCheckbox(data: any[]): any[] {
console.log(
'******Applying filter by identifierFilterValue:',
this.identifierFilterValue
);
if (
!this.identifierFilterValue ||
this.identifierFilterValue.length === 0
) {
console.log('No Identifier Filters are selected return full data');
return [...data];
}
return data.filter((item) => {
console.log('Checking item identifier:', item.identifier);
if (this.identifierFilterValue.indexOf(item.identifier) !== -1) {
console.log('Matched identifier:', item.identifier);
return true;
} else {
console.log('Identifier not matched', item.identifier);
return false;
}
});
}
}
I've also tried includes instead of indexOf
filterByIdentityCheckbox(data: any[]): any[] {
console.log('******Applying filter by identifierFilterValue:',
this.identifierFilterValue);
if (!this.identifierFilterValue ||
this.identifierFilterValue.length === 0) {
console.log('No Identifier Filters are selected return full data');
return [...data];
}
return data.filter(item => {
console.log('Checking item identifier:', item.identifier);
if (this.identifierFilterValue.includes(item.identifier)) {
console.log('Matched identifier:', item.identifier);
return true;
} else {
console.log('Identifier not matched', item.identifier);
return false;
}
});
}
I have a parent component, filter component, and view table component.
I read something about using lodash as ngOnChanges doesn't work the second time...let me do some reading.
Here is my stackblitz
Share Improve this question edited Mar 31 at 22:07 spring_hiber asked Mar 31 at 19:27 spring_hiberspring_hiber 1711 gold badge2 silver badges14 bronze badges1 Answer
Reset to default 2The problem is that the reference to identifierFilterValue
doesn't change (it's the same array, but with new values), so the input of ReportsViewTableComponent
is not updated.
Here is the problematic code in the IdentifierFilter
component:
onIdentifierSelectionChange(event: MatCheckboxChange, formType: string) {
if (event.checked) {
this.selectedFormTypes.push(formType);
} else {
this.selectedFormTypes = this.selectedFormTypes.filter(type => type !== formType);
}
console.log('Selected Filters to Parent:', this.selectedFormTypes);
this.identifierFilterChanged.emit(this.selectedFormTypes);
}
You need to write something like this:
this.identifierFilterChanged.emit([...this.selectedFormTypes]);
本文标签: javascriptHow to properly filter material table using angular using checkboxStack Overflow
版权声明:本文标题:javascript - How to properly filter material table using angular using checkbox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743925235a2562838.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论