Issue
I have a function that trigger when I sort the table deepens of a column I click but the data is not updating. This is the function and the table code:
<table mat-table #table [dataSource]="dataSourceMD" matSort (matSortChange)="getRowMaximoTable($event)" matTableExporter #exporter="matTableExporter" #sortMD="matSort" class="tr_table">
<ng-container *ngFor="let disCol of displayedColumnsMD; let colIndex = index"
matColumnDef="{{disCol}}" [sticky]="isIn(disCol)">
<th mat-header-cell *matHeaderCellDef mat-sort-header>
<div [innerHTML]="displayedColumnsNamesMD[colIndex]"></div>
</th>
<td mat-cell *matCellDef="let element" [style.background-color]="element[disCol+'Color']" [style.color]="element[disCol+'Texto']">
<div *ngIf="element[disCol]" [innerHTML]="element[disCol]"></div>
<div *ngIf="!element[disCol] && !isIn(disCol)" [innerHTML]="'-'"></div>
</td>
</ng-container>
<ng-container *ngFor="let filCol of displayedFilterColumnMD; let colIndexF = index"
matColumnDef="{{filCol}}" [sticky]="colIndexF<3">
<th mat-header-cell *matHeaderCellDef [style.text-align]="center" [attr.colspan]="1">
<mat-form-field class="columnas" floatLabel='never'>
<input matInput placeholder="Filtro" type="text"
[(ngModel)]='filterTableMD[displayedColumnsMD[colIndexF]]'
name="displayedColumnsMD[colIndexF]"
(keyup)="hanlerOnChangeFilter($event, 'MD',displayedColumnsMD[colIndexF])">
<button mat-button *ngIf="filterTableMD[displayedColumnsMD[colIndexF]]"
(click)="handlerClearFilterField('MD',displayedColumnsMD[colIndexF])" matSuffix
mat-icon-button aria-label="Clear">
<mat-icon class="material-icons-outlined">close</mat-icon>
</button>
</mat-form-field>
</th>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumnsMD; sticky: true"></tr>
<tr mat-header-row *matHeaderRowDef="displayedFilterColumnMD"></tr>
<tr mat-row [ngClass]="{ 'maximo-row': i == indexMaximoMD }" *matRowDef="let row; columns: displayedColumnsMD; let i = index"></tr>
</table>
function:
getRowMaximoTable(event?: MatSort) {
let originalArrayData = [ ...this.dataSourceMD.data ];
const valoresArrayASortear = originalArrayData.slice(0, -4);
const valoresArrayFijos = originalArrayData.slice(-4);
const sortedArray = this.dataSourceMD.sortData(valoresArrayASortear, this.dataSourceMD.sort);
this.dataSourceMD.data = [...sortedArray, ...valoresArrayFijos];
}
I want to update the data correctly when i click of one header columns
Solution
I solved it this way:
this.dataSourceMD.sortingDataAccessor = (item, property) => {
let sortDirection = this.sortMD.direction;
if((this.dataSourceMD.data.indexOf(item) >= this.dataSourceMD.data.length - 4) && (sortDirection == 'asc')){
return "ZZZZZ";
} else if((this.dataSourceMD.data.indexOf(item) >= this.dataSourceMD.data.length - 4) && (sortDirection == 'desc')){
return "AAAAA";
}
return item[property];
};
Answered By - Imad
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.