Spaces:
Running
Running
James McCool
commited on
Commit
Β·
e74f850
1
Parent(s):
9f63aed
Enhance DataTable sorting functionality to support three-state sorting (asc, desc, none) and update sort indicator logic for better user experience.
Browse files- src/components/DataTable.jsx +13 -5
src/components/DataTable.jsx
CHANGED
@@ -29,9 +29,15 @@ const DataTable = () => {
|
|
29 |
const sortData = (key) => {
|
30 |
let direction = 'asc';
|
31 |
|
32 |
-
//
|
33 |
-
if (sortConfig.key === key
|
34 |
-
direction
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
}
|
36 |
|
37 |
setSortConfig({ key, direction });
|
@@ -39,7 +45,7 @@ const DataTable = () => {
|
|
39 |
|
40 |
// Get sorted data
|
41 |
const getSortedData = () => {
|
42 |
-
if (!sortConfig.key) return data;
|
43 |
|
44 |
return [...data].sort((a, b) => {
|
45 |
let aValue = a[sortConfig.key];
|
@@ -64,7 +70,9 @@ const DataTable = () => {
|
|
64 |
// Get sort indicator for column headers
|
65 |
const getSortIndicator = (key) => {
|
66 |
if (sortConfig.key !== key) return 'βοΈ';
|
67 |
-
|
|
|
|
|
68 |
};
|
69 |
|
70 |
const fetchFromMongoDB = async () => {
|
|
|
29 |
const sortData = (key) => {
|
30 |
let direction = 'asc';
|
31 |
|
32 |
+
// Three-state sorting: asc β desc β none
|
33 |
+
if (sortConfig.key === key) {
|
34 |
+
if (sortConfig.direction === 'asc') {
|
35 |
+
direction = 'desc';
|
36 |
+
} else if (sortConfig.direction === 'desc') {
|
37 |
+
// Reset to no sorting
|
38 |
+
setSortConfig({ key: null, direction: 'none' });
|
39 |
+
return;
|
40 |
+
}
|
41 |
}
|
42 |
|
43 |
setSortConfig({ key, direction });
|
|
|
45 |
|
46 |
// Get sorted data
|
47 |
const getSortedData = () => {
|
48 |
+
if (!sortConfig.key || sortConfig.direction === 'none') return data;
|
49 |
|
50 |
return [...data].sort((a, b) => {
|
51 |
let aValue = a[sortConfig.key];
|
|
|
70 |
// Get sort indicator for column headers
|
71 |
const getSortIndicator = (key) => {
|
72 |
if (sortConfig.key !== key) return 'βοΈ';
|
73 |
+
if (sortConfig.direction === 'asc') return 'β';
|
74 |
+
if (sortConfig.direction === 'desc') return 'β';
|
75 |
+
return 'βοΈ';
|
76 |
};
|
77 |
|
78 |
const fetchFromMongoDB = async () => {
|