Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Added numeric sorting to tables #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,8 @@
}
}
}
},
"cli": {
"analytics": false
}
}
6 changes: 3 additions & 3 deletions src/app/@components/crypto-list/crypto-list.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ <h3>Crypto List</h3>
<ng-template pTemplate="header">
<tr>
<th>Name</th>
<th>Price</th>
<th>Change</th>
<th>Vol 24h</th>
<th (click)="sortTableByNumericField('priceUsd')">Price</th>
<th (click)="sortTableByNumericField('changePercent24Hr')">Change</th>
<th (click)="sortTableByNumericField('volumeUsd24Hr')">Vol 24h</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-coin>
Expand Down
17 changes: 15 additions & 2 deletions src/app/@components/crypto-list/crypto-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export class CryptoListComponent implements OnInit {
(resp) => {
if (Array.isArray(resp.data)) {
this.coinCryptoAssetsArray = resp.data;
console.log(this.coinCryptoAssetsArray);
} else {
console.error('Los datos recibidos no son un arreglo:', resp.data);
}
Expand All @@ -57,7 +56,6 @@ export class CryptoListComponent implements OnInit {
filterTable() {
this.dataTable.filter(this.searchText, 'name', 'contains');
this.filteredCoins = this.dataTable.filteredValue || [];
console.log(this.filteredCoins);
}

convertToTwoDecimalString(input: string): string {
Expand All @@ -71,4 +69,19 @@ export class CryptoListComponent implements OnInit {
return 'Invalid number';
}
}

sortTableByNumericField(field: string) {
const isNumeric = typeof this.dataTable.value[0][field] === 'number';
this.dataTable.value.forEach(item => {
if(!isNumeric){
item[field] = parseFloat(item[field]);
}
});
this.dataTable.sortOrder *= -1;
this.dataTable.value.sort((a, b) => {
return (a[field] - b[field]) * this.dataTable.sortOrder;
});
this.dataTable.sortSingle();
this.filteredCoins = this.dataTable.filteredValue || [];
}
}