Skip to content

Commit

Permalink
Feat : orderIdx로 정렬(#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
ohinhyuk committed Sep 21, 2023
1 parent a79a680 commit d26bca3
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/components/common/CustomTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,15 @@ const typeConverter = (type) => {
*/

export default function EnhancedTable({ originalRows, headCells, type }) {
function sortByOrderIdx(data) {
return data.sort((a, b) => a.orderIdx - b.orderIdx);
}

/**
* @field 필터링을 거치고 보여주는 값들 (rows)
*/
const [rows, setRows] = React.useState(originalRows);

const [rows, setRows] = React.useState(sortByOrderIdx(originalRows));
console.log('debug', rows, originalRows);

/**
Expand All @@ -331,9 +336,11 @@ export default function EnhancedTable({ originalRows, headCells, type }) {
const studentName = useSelector((state) => state.filter.studentName);
const grade = useSelector((state) => state.filter.grade);
const department = useSelector((state) => state.filter.department);

/**
* @brief 필터링
*/

useEffect(() => {
let copyRows = originalRows;
if (category && category !== '전체') {
Expand Down Expand Up @@ -503,26 +510,40 @@ export default function EnhancedTable({ originalRows, headCells, type }) {
// setRows(updatedRows);
// };
const handleDragEnd = async (result) => {
console.log(result);
const { source, destination } = result;

if (!destination) return;

// Copy the current rows for manipulation
const updatedRows = [...rows];
let updatedRows = [...rows];

// Remove the dragged item from source and insert it into destination
const [movedRow] = updatedRows.splice(source.index, 1);
updatedRows.splice(destination.index, 0, movedRow);

const startIdx = Math.min(source.index, destination.index);
const endIdx = Math.max(source.index, destination.index);
for (let i = startIdx; i <= endIdx; i++) {
console.log(startIdx, endIdx);
const updateRow = {
num: updatedRows[i].num,
category: updatedRows[i].category,
orderIdx: rows[i].orderIdx,
description1: updatedRows[i].description1,
description2: updatedRows[i].description2,
manage: updatedRows[i].manage,
};
updatedRows = [...updatedRows.slice(0, i), updateRow, ...updatedRows.slice(i + 1)];

// updatedRows[i].orderIdx = rows[i].orderIdx;
console.log(updatedRows[i].orderIdx, rows[i].orderIdx);
}

for (let i = startIdx; i <= endIdx; i++) {
const target = updatedRows[i];
if (target) {
console.log(rows[i], updatedRows[i].orderIdx);
updateNewOrderIdx(rows[i], updatedRows[i].orderIdx);
updateNewOrderIdx(updatedRows[i], updatedRows[i].orderIdx);
// target.orderIdx = newOrderIdx; // Update the local state as well
}
}
Expand Down

0 comments on commit d26bca3

Please sign in to comment.