To make the sort buttons work, you need to add methods that handle the sorting logic for the data displayed in the table. Then, bind these methods to the @click event of each sorting button. Below is an approach where we implement sorting functionality for both "Gold Medal" and "Total Medals" using the existing Vue instance:
-
Add Sorting Methods: Implement methods to sort the tableData array based on the selected criteria.
-
Bind Click Events: Update the template to bind these methods to the click events of the "Gold Medal" and "Total Medals" buttons.
Here is how you can achieve this:
<script>
import FormEdit from './FormEdit.vue';
import FormCreate from './FormCreate.vue';
const urlAPI = "http://localhost:7788/api/medals/";
export default {
components: {
FormEdit,
FormCreate,
},
data() {
return {
tableData: [],
nationalities: [], // Storing nationality information
};
},
mounted() {
this.loadData();
this.loadNationalities();
},
methods: {
loadNationalities() {
fetch('/nationality.json') // Ensure this file is served from the right path
.then((response) => response.json())
.then((data) => (this.nationalities = data));
},
loadData() {
fetch(urlAPI)
.then((response) => response.json())
.then((data) => this.updateTableData(data));
},
updateTableData(rawData) {
this.tableData = this.processData(rawData);
},
processData(rawData) {
return rawData.map((item) => ({
...item,
totalMedals: item.goldMedal + item.silverMedal + item.bronzeMedal,
alpha2Code: this.getAlpha2Code(item.nationality), // Assuming nationality stores alpha3Code
}));
},
handleDelete(index, row) {
console.log('To delete /medals/' + row.pk, index);
fetch(urlAPI + row.pk, {
method: 'DELETE',
})
.then((response) => response.json())
.then((data) => {
const foundPos = this.tableData.findIndex(
(entry) => entry.id == row.id
);
if (foundPos >= 0) {
this.tableData.splice(foundPos, 1);
}
});
},
handleEdit(index, row) {
console.log('To edit /medals/' + row.pk);
this.$refs.formEdit.showDialog(row);
},
handleFormEditCancel() {
console.log('handle form edit cancel');
},
handleFormEditConfirm(rec) {
console.log('handle form edit confirm', rec);
fetch(urlAPI + rec.pk, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(rec),
})
.then((response) => response.json())
.then((data) => {
const foundPos = this.tableData.findIndex(
(entry) => entry.pk == data.pk
);
this.tableData[foundPos] = data;
this.loadData();
});
},
handleCreate() {
this.$refs.formCreate.showDialog();
},
handleFormCreateConfirm(rec) {
console.log('handle form create confirm', rec);
fetch(urlAPI, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(rec),
})
.then((response) => response.json())
.then((data) => {
const newItem = this.processData([data]);
this.tableData.push(...newItem);
this.loadData();
});
},
sortByGoldMedals() {
this.tableData.sort((a, b) => b.goldMedal - a.goldMedal);
},
sortByTotalMedals() {
this.tableData.sort((a, b) => b.totalMedals - a.totalMedals);
},
getAlpha2Code(alpha3Code) {
const nationality = this.nationalities.find(
(n) => n.alpha3Code === alpha3Code
);
return nationality ? nationality.alpha2Code : null;
},
},
};
</script>
<template>
<el-button @click="handleCreate">Add new item</el-button>
<el-text class="mx-1" type="info">Sort By: </el-text>
<el-button @click="sortByGoldMedals">Gold Medal</el-button>
<el-button @click="sortByTotalMedals">Total Medals</el-button>
<el-table :data="tableData" style="width: 80%">
<el-table-column prop="nationality" label="Nationality" width="300">
<template #default="scope">
<div style="display: flex; align-items: center;">
<img
v-if="scope.row.alpha2Code"
:src="`https://flagcdn.com/${scope.row.alpha2Code.toLowerCase()}.svg`"
alt="flag"
width="16"
height="12"
style="margin-right: 8px;"
/>
{{ scope.row.nationality }}
</div>
</template>
</el-table-column>
<el-table-column prop="name" label="Name" width="160" />
<el-table-column prop="goldMedal" label="🥇 Gold Medal" width="180" />
<el-table-column prop="silverMedal" label="🥈 Silver Medal" width="180" />
<el-table-column prop="bronzeMedal" label="🥉 Bronze Medal" width="180" />
<el-table-column prop="totalMedals" label="🏅 Total Medals" width="180" />
<el-table-column label="Operations">
<template #default="scope">
<el-button
type="primary"
size="mini"
plain
@click="handleEdit(scope.$index, scope.row)"
>Edit</el-button
>
<el-button
type="danger"
size="mini"
plain
@click="handleDelete(scope.$index, scope.row)"
>Delete</el-button
>
</template>
</el-table-column>
</el-table>
<form-edit
ref="formEdit"
@cancel="handleFormEditCancel"
@confirm="handleFormEditConfirm"
>
</form-edit>
<form-create ref="formCreate" @confirm="handleFormCreateConfirm">
</form-create>
</template>
<style scoped>
</style>
Explanation:
-
Sorting Methods:
sortByGoldMedals(): Sorts tableData based on the goldMedal count in descending order.
sortByTotalMedals(): Sorts tableData based on the totalMedals count in descending order.
-
Click Events:
- The buttons for "Gold Medal" and "Total Medals" are linked to their respective sorting methods, allowing the user to sort the data by simply clicking on these buttons.
This setup provides a way to dynamically sort the table content based on the chosen criteria, offering a better user experience when viewing medal data.