From 208cd09c62b80d134807ee60793aeac49be5fe47 Mon Sep 17 00:00:00 2001 From: Peter Date: Sat, 16 Nov 2024 08:51:25 -0600 Subject: [PATCH] Sortable player batting stats --- src/components/PlayerCareerBattingTable.vue | 109 +++++++++++++------- 1 file changed, 73 insertions(+), 36 deletions(-) diff --git a/src/components/PlayerCareerBattingTable.vue b/src/components/PlayerCareerBattingTable.vue index 026452f..deeb499 100644 --- a/src/components/PlayerCareerBattingTable.vue +++ b/src/components/PlayerCareerBattingTable.vue @@ -6,33 +6,33 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -109,6 +109,10 @@ import { aggregateBattingStats, type BattingStat } from '@/services/battingStats interface BattingStatWithSeason extends BattingStat { seasonNumber: number isRegularSeason: boolean + + sortableSeason: string // constructed to sort S1 -> S1 Playoffs -> S2 -> etc + + kPct: string } export default { @@ -118,6 +122,12 @@ export default { postSeasonBattingStats: { type: Array, required: true }, showPostSeasonStats: { type: Boolean, required: true } }, + data() { + return { + sortKey: 'sortableSeason' as keyof BattingStatWithSeason, + sortOrder: 1 + } + }, computed: { hasBattingStats(): boolean { return !!(this.regularSeasonBattingStats.length + this.postSeasonBattingStats.length) @@ -143,7 +153,9 @@ export default { return { ...stat, seasonNumber: stat.player.season, - isRegularSeason: true + isRegularSeason: true, + sortableSeason: `${`${stat.player.season}`.padStart(3, '0')}-A-Regular`, + kPct: this.calculateStrikeoutPercent(stat) } })) } @@ -153,21 +165,31 @@ export default { return { ...stat, seasonNumber: stat.player.season, - isRegularSeason: false + isRegularSeason: false, + sortableSeason: `${`${stat.player.season}`.padStart(3, '0')}-B-Playoffs`, + kPct: this.calculateStrikeoutPercent(stat) } })) } - return seasonStats.sort((s1, s2) => { - return s1.seasonNumber - s2.seasonNumber === 0 - ? s1.isRegularSeason - ? -1 - : 1 - : s1.seasonNumber - s2.seasonNumber - }) + return seasonStats.sort((s1, s2) => s2[this.sortKey] < s1[this.sortKey] ? this.sortOrder : -1 * this.sortOrder) }, }, methods: { + setKey(stat: keyof BattingStatWithSeason): void { + if (this.sortKey === stat) { + // if key currently selected, flip sort order + this.sortOrder *= -1 + } else { + this.sortKey = stat + this.sortOrder = stat === 'sortableSeason' ? 1 : -1 + } + }, + getArrow(stat: keyof BattingStatWithSeason): string { + if (this.sortKey !== stat) return 'faux-arrow' + + return this.sortOrder > 0 ? 'up' : 'down' + }, calculateStrikeoutPercent(stat: BattingStat): string { if (!stat.pa) return 'N/A' return (stat.so * 100 / stat.pa).toFixed(1) @@ -175,3 +197,18 @@ export default { } } + +
SeasonPAABRH2B3BHRRBISBCSBBSOBAOBPSLGOPSwOBAK%BPHRBPFOBP1BBPLOGIDPHBPSACIBBSeasonPAABRH2B3BHRRBISBCSBBSOBAOBPSLGOPSwOBAK%BPHRBPFOBP1BBPLOGIDPHBPSACIBB