From f2aa7ae0c034024187b6d9819da513896c4ae83a Mon Sep 17 00:00:00 2001 From: = <=> Date: Fri, 3 Apr 2026 18:10:46 +1000 Subject: [PATCH] hw 4 done --- site/src/components/Filter.jsx | 10 ++++- site/src/components/Sorting.jsx | 65 +++++++++++++++++++++++++++------ site/src/components/Table.jsx | 10 ++++- 3 files changed, 71 insertions(+), 14 deletions(-) diff --git a/site/src/components/Filter.jsx b/site/src/components/Filter.jsx index 0f67deb..6ac63d8 100644 --- a/site/src/components/Filter.jsx +++ b/site/src/components/Filter.jsx @@ -36,8 +36,16 @@ const Filter = (props) => { //передаем родительскому компоненту новое состояние - отфильтрованный массив props.filtering(arr); } + const doReset=(event)=>{ + event.target.querySelectorAll("input[type=\"number\"],input[type=\"text\"]").forEach(element => { + element.value=null; + }); + handleSubmit(event); + + } + return ( -
+

diff --git a/site/src/components/Sorting.jsx b/site/src/components/Sorting.jsx index 2f7316d..96c904f 100644 --- a/site/src/components/Sorting.jsx +++ b/site/src/components/Sorting.jsx @@ -19,26 +19,28 @@ const SortLevel = (props) => { const Sorting = (props) => { const [selections, updateSelections] = useState([-1]); const [checkboxes, updateCheckboxes] = useState([0]); + const updateOption = (x, id) => { selections[id] = x; - console.log("fixup function called") + // console.log("fixup function called") let checkboxesCopy = [...checkboxes]; let selectionsCopy = selections.map((x, i, arr) => { if (i < id) { - console.log("a", i, x) + // console.log("a", i, x) return x; } if (x != -1 && arr.indexOf(x) != i) { - let unusedoption = - console.log("b", i, [0]) - return unusedOption; + let unusedOption = Array.from({ length: props.keys.length }, (_, i) => i).filter((x) => !selections.slice(0, i).includes(x)); + // console.log(unusedOption) + // console.log("b", i, [0]) + return unusedOption[0]; } - console.log("c", i,x) + // console.log("c", i,x) return x }) - + checkboxesCopy = checkboxesCopy.filter((x, i) => selectionsCopy[i] != -1); selectionsCopy = selectionsCopy.filter((x, i) => x != -1); @@ -57,15 +59,54 @@ const Sorting = (props) => { const array = props.array; const keys = props.keys; + const clearSort = (event) => { + updateSelections([-1]); + updateCheckboxes([0]); + applySort(); + } + const applySort = (event) => { + console.log("calling sort") + + let inverse = [...checkboxes]; + let selectionsCopy = [...selections]; + inverse = inverse.filter((x, i) => selectionsCopy[i] != -1); + selectionsCopy = selectionsCopy.filter((x, i) => x != -1); + let keys = selectionsCopy.map((x) => props.keys[x]); + let sortArr = inverse.map((x, i) => {return { "column": keys[i], "direction":x }}) + // console.log(sortArr) + let tempArr = [...props.data]; + tempArr.sort((first, second) => { + for (let { column, direction } of sortArr) { + const firstCell = first[column]; + const secondCell = second[column]; + let comparison = null; + if (typeof (firstCell) != "string") { + comparison = Number(firstCell) - Number(secondCell); + } + else { + comparison = firstCell.localeCompare(secondCell); + } + // учитываем направление сортировки + if (comparison !== 0) { + return (direction ? -comparison : comparison); + } + } + return 0; + }); + props.returnDataCallback(tempArr); + // return tempArr; + // console.log(tempArr) + + } + props.setApplySort({f:clearSort}); return ( <> { - selections.map((x, i, arr) => { - let curenSelectiontArr = keys.filter((selection) => !arr.slice(0, i).includes(keys.indexOf(selection))) + let curentSelectiontArr = keys.filter((selection) => !arr.slice(0, i).includes(keys.indexOf(selection))) const upadteOptionWrap = (event) => { // console.log(event); updateOption(Number(event.target.value) - 1, i); @@ -76,12 +117,14 @@ const Sorting = (props) => { checkboxesCopy[i] = Number(event.target.checked); updateCheckboxes(checkboxesCopy); }; - if (curenSelectiontArr.length > 0) { - return + if (curentSelectiontArr.length > 0) { + return } }) } + + ) } diff --git a/site/src/components/Table.jsx b/site/src/components/Table.jsx index c9d949b..ab2c8ca 100644 --- a/site/src/components/Table.jsx +++ b/site/src/components/Table.jsx @@ -14,8 +14,14 @@ const Table = (props) => { const [activePage, setActivePage] = useState("1"); const [dataTable, setDataTable] = useState(props.data); - const updateDataTable = (value) => setDataTable(value); + const [applySort, setApplySort] = useState({f:(x)=>{x}}); + + + const updateDataTable = (value) => { + applySort.f(); + setDataTable(value); + } const changeActive = (event) => { setActivePage(event.target.innerHTML); }; @@ -38,7 +44,7 @@ const Table = (props) => {

Sort by

- +