Topic: mdbootstrap table sort issue with headers different than property names
<table mdbTable class="table table-sm table-striped" style="font-size: .75rem"> <thead class="thead-dark"> <tr> <th *ngFor="let header of tableHeaders; let i = index" [mdbTableSort]="items" [sortBy]="tableHeaderNames[i]"> {{ header }} </th> </tr> </thead> <tbody> <tr *ngFor="let item of items; let i = index"> <td>{{item.name}}</td> <td>{{item.classRestriction}}</td> <td>{{item.tier}}</td> <td>{{item.rarity[0]}}</td> <td>{{calculateWairModifier(item.attackSpeed)}}</td> <td>{{calculateWairModifier(item.maxHp)}}</td> <td>{{calculateWairModifier(item.damage)}}</td> <td>{{calculateWairModifier(item.defenseModifier)}}</td> <td>{{calculateWairModifier(item.critChance)}}</td> <td>{{calculateWairModifier(item.critBonus)}}</td> <td>{{calculateWairModifier(item.receiveHealingModifier)}}</td> <td>{{calculateWairModifier(item.selfHealModifier)}}</td> <td>{{calculateWairModifier(item.healingModifier)}}</td> <td>{{calculateWairModifier(item.minEnergyPoints)}}</td> <td>{{calculateWairModifier(item.maxEnergyPoints)}}</td> <td>{{calculateWairModifier(item.energyRegenerationModifier)}}</td> <td>{{calculateWairModifier(item.energyGainModifier)}}</td> <td>{{calculateWairModifier(item.cooldownModifier)}}</td> </tr> </tbody> </table> tableHeaders: string[] = [ 'Name', 'Class', 'Tier', 'Rarity', 'AtkSpd', 'MaxHp', 'Dmg', 'Armor', 'Crit%', 'CritDmg', 'RecHealMod', 'SelfHealMod', 'HealDmg', 'MinEnergy', 'MaxEnergy', 'EnergyRegen', 'EnergyGain', 'CooldownMod' ]; tableHeaderNames: string[] = [ 'name', 'classRestriction', 'tier', 'rarity', 'attackSpeed', 'maxHp', 'damage', 'defenseModifier', 'critChance', 'critBonus', 'receiveHealingModifier', 'selfHealModifier', 'healingModifier', 'minEnergyPoints', 'maxEnergyPoints', 'energyRegenerationModifier', 'energyGainModifier', 'cooldownModifier' ]; items: Item[] = [ { name: 'Lizard Sinew', classRestriction: '', tier: 3, rarity: 'Uncommon', attackSpeed: null, damage: null, critChance: null, critBonus: null, receiveHealingModifier: null, healingModifier: null, defenseModifier: null, maxHp: null, maxEnergyPoints: null, energyRegenerationModifier: null, energyGainModifier: null, cooldownModifier: null, minEnergyPoints: null, selfHealModifier: null }];
Damian Gemza staff answered 6 years ago
tehAon pro commented 6 years ago
not only has to be the same, it also currently has to be lowercase because it forces it toLowerCase() in the sort directive before looking up the key.
HarishHebbar commented 6 years ago
Dear Damian, Any update on this.
Damian Gemza staff commented 6 years ago
Dear Harish,
For now, sorting the table works only in the situation, when the table's header is the same as in the typescript code.
Best Regards,
Damian
HarishHebbar commented 6 years ago
Dear Damian,
Thanks for the update.
HarishHebbar commented 6 years ago
Dear Damian,
The issue I faced was if there is space difference in the header UI and header name in typescript, sorting is not working.(like I have header name as Customer Name in UI and in typescript I am having CustomerName)
WorkAround I did:I wrote a custom trim function and now sorting works for me.HTML file: [sortBy]="trim(headElements[i])"typescript file:trim(headElement){ return headElement.replace(/ /g,''"); }
Is it possible to handle this in the mdb-table-sort.directive.ts code itself like how you have handled the lowercase.
Damian Gemza staff commented 6 years ago
Harish,
Thanks for your suggestion! We'll take a look at your code, and we'll implement it, if there won't be any problems with the code itself.
Best Regards,
Damian
HarishHebbar commented 6 years ago
Dear Damian,
Thanks for the prompt update.
Damian Gemza staff commented 6 years ago
I'm glad that I could help you :)
HarishHebbar commented 6 years ago
Dear Damian,
Thanks for including the white space trim function in release v.7.4.2 :)
Damian Gemza staff commented 6 years ago
No problem :) I'm glad that I could help you.
tehAon pro answered 6 years ago
MdbTableService.prototype.filterLocalDataBy = function (searchKey) { returnthis.getDataSource().filter(fu nction (obj) { returnObject.keys(obj).some(function (key) { return (obj[key].toLowerCase()).inclu des(searchKey); }); }); };
MdbTableService.prototype.filterLocalDataBy = function (searchKey) { returnthis.getDataSource().filter(fu nction (obj) { returnObject.keys(obj).some(function (key) { returnobj[key] != null && (obj[key].toString().toLowerCa se()).includes(searchKey); }); }); };
MdbTableSortDirective.prototype.onclick = function () { this.sortDataBy(this.sortBy.to LowerCase()); };
MdbTableSortDirective.prototype.onclick = function () { this.sortDataBy(this.sortBy); };
tehAon pro answered 6 years ago
Damian Gemza staff answered 6 years ago
box86rowh answered 6 years ago
I believe this is tripping me up as well. My data structure has the key "Name" and when I pass that as the sort by I get an error:
Cannot read property 'toLowerCase' of undefined
Damian Gemza staff answered 6 years ago
Dear @box86rowh
We'll take again a look at this problem.
Unfortunately, I'm not able to provide you a workaround for this situation, because we need to change the code of the table component.
Best Regards,
Damian
Damian Gemza staff answered 6 years ago
Dear @box86rowh
This problem will be resolved with the next version of MDB Angular.
Best Regards,
Damian
Lukasz Pomianowski commented 6 years ago
Has this been fixed in the latest version as I still seem to have this problem
Lukasz Pomianowski answered 6 years ago
Has this been fixed in the latest version? It still seems to have this issue and nothing has been mentioned in the change log.
THM2018 pro answered 6 years ago
I am using MDB Angular 7.5.1 pro. In which version of MDB Angular we can expect the solution for table sort issue with headers different than property names?
Damian Gemza staff answered 6 years ago
Dear @THM2018
Introducing such a modification to Table Sort will be marked as breaking changes, so we will add it only in the new version of MDB Angular (do not expect that in MDB 8 it will be introduced, sooner in MDB 9).
Until then you can write your own sorting or use ours together with Angular pipes.
Best Regards,
Damian
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Answered
- User: Pro
- Premium support: No
- Technology: MDB Angular
- MDB Version: 6.2.3
- Device: PC
- Browser: Chrome
- OS: Windows 10
- Provided sample code: No
- Provided link: Yes