Sorting did not work in KTDataTable, so I decided to improve it.
Here are the specific lines I added or changed in your component:
Added two new refs to store the current sort field and sort order:
const sortField = ref<string | null>(props.sortLabel);
const sortOrder = ref<"asc" | "desc">(props.sortOrder);
const sortedData = computed(() => {
if (!props.data || !sortField.value) {
return props.data;
}
return [...props.data].sort((a, b) => {
const fieldA = a[sortField.value!];
const fieldB = b[sortField.value!];
if (fieldA < fieldB) {
return sortOrder.value === "asc" ? -1 : 1;
}
if (fieldA > fieldB) {
return sortOrder.value === "asc" ? 1 : -1;
}
return 0;
});
});
const dataToDisplay = computed(() => {
if (sortedData.value) {
if (sortedData.value.length <= itemsInTable.value) {
return sortedData.value;
} else {
let sliceFrom = (currentPage.value - 1) * itemsInTable.value;
return sortedData.value.slice(sliceFrom, sliceFrom + itemsInTable.value);
}
}
return [];
});
const onSort = (sort: Sort) => {
const column = props.header.find(
(col) => col.columnLabel === sort.label
);
if (column && column.sortEnabled) {
sortField.value = column.sortingField;
sortOrder.value = sort.order;
}
emit("on-sort", sort);
};
Hi,
Thank you for your feedback.
We were not able to reproduce this problem in the latest Metronic version.
Could you please specify which metronic version you are using?
Do you have the same problem on our preview page?
https://preview.keenthemes.com/metronic8/vue/demo1/apps/subscriptions/subscription-list
Regards,
Lauris Stepanovs,
Keenthemes Support Team