1 Commits

Author SHA1 Message Date
=
f2aa7ae0c0 hw 4 done 2026-04-03 18:10:46 +10:00
3 changed files with 71 additions and 14 deletions

View File

@@ -36,8 +36,16 @@ const Filter = (props) => {
//передаем родительскому компоненту новое состояние - отфильтрованный массив //передаем родительскому компоненту новое состояние - отфильтрованный массив
props.filtering(arr); props.filtering(arr);
} }
const doReset=(event)=>{
event.target.querySelectorAll("input[type=\"number\"],input[type=\"text\"]").forEach(element => {
element.value=null;
});
handleSubmit(event);
}
return ( return (
<form onSubmit={handleSubmit} onReset={handleSubmit}> <form onSubmit={handleSubmit} onReset={doReset}>
<p> <p>
<label>Type:</label> <label>Type:</label>
<input name="type" type="text" /> <input name="type" type="text" />

View File

@@ -19,26 +19,28 @@ const SortLevel = (props) => {
const Sorting = (props) => { const Sorting = (props) => {
const [selections, updateSelections] = useState([-1]); const [selections, updateSelections] = useState([-1]);
const [checkboxes, updateCheckboxes] = useState([0]); const [checkboxes, updateCheckboxes] = useState([0]);
const updateOption = (x, id) => { const updateOption = (x, id) => {
selections[id] = x; selections[id] = x;
console.log("fixup function called") // console.log("fixup function called")
let checkboxesCopy = [...checkboxes]; let checkboxesCopy = [...checkboxes];
let selectionsCopy = selections.map((x, i, arr) => { let selectionsCopy = selections.map((x, i, arr) => {
if (i < id) { if (i < id) {
console.log("a", i, x) // console.log("a", i, x)
return x; return x;
} }
if (x != -1 && arr.indexOf(x) != i) { if (x != -1 && arr.indexOf(x) != i) {
let unusedoption = let unusedOption = Array.from({ length: props.keys.length }, (_, i) => i).filter((x) => !selections.slice(0, i).includes(x));
console.log("b", i, [0]) // console.log(unusedOption)
return unusedOption; // console.log("b", i, [0])
return unusedOption[0];
} }
console.log("c", i,x) // console.log("c", i,x)
return x return x
}) })
checkboxesCopy = checkboxesCopy.filter((x, i) => selectionsCopy[i] != -1); checkboxesCopy = checkboxesCopy.filter((x, i) => selectionsCopy[i] != -1);
selectionsCopy = selectionsCopy.filter((x, i) => x != -1); selectionsCopy = selectionsCopy.filter((x, i) => x != -1);
@@ -57,15 +59,54 @@ const Sorting = (props) => {
const array = props.array; const array = props.array;
const keys = props.keys; 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 ( return (
<> <>
{ {
selections.map((x, i, arr) => { 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) => { const upadteOptionWrap = (event) => {
// console.log(event); // console.log(event);
updateOption(Number(event.target.value) - 1, i); updateOption(Number(event.target.value) - 1, i);
@@ -76,12 +117,14 @@ const Sorting = (props) => {
checkboxesCopy[i] = Number(event.target.checked); checkboxesCopy[i] = Number(event.target.checked);
updateCheckboxes(checkboxesCopy); updateCheckboxes(checkboxesCopy);
}; };
if (curenSelectiontArr.length > 0) { if (curentSelectiontArr.length > 0) {
return <SortLevel key={i} id={"SortLevel" + i} keys={keys} visible={curenSelectiontArr} updateCallback={upadteOptionWrap} checkboxUpdate={updateCheckBoxesWrap} selected={x + 1} reverse={checkboxes[i]} /> return <SortLevel key={i} id={"SortLevel" + i} keys={keys} visible={curentSelectiontArr} updateCallback={upadteOptionWrap} checkboxUpdate={updateCheckBoxesWrap} selected={x + 1} reverse={checkboxes[i]} />
} }
}) })
} }
<button onClick={applySort}>Apply Sort</button>
<button onClick={clearSort}>Clear Sort</button>
</> </>
) )
} }

View File

@@ -14,8 +14,14 @@ const Table = (props) => {
const [activePage, setActivePage] = useState("1"); const [activePage, setActivePage] = useState("1");
const [dataTable, setDataTable] = useState(props.data); 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) => { const changeActive = (event) => {
setActivePage(event.target.innerHTML); setActivePage(event.target.innerHTML);
}; };
@@ -38,7 +44,7 @@ const Table = (props) => {
<h4>Sort by</h4> <h4>Sort by</h4>
<Sorting data = {dataTable} keys = {Object.keys(props.data[0])}/> <Sorting data = {dataTable} keys = {Object.keys(props.data[0])} returnDataCallback={setDataTable} setApplySort={setApplySort}/>
<table> <table>
<TableHead head={Object.keys(props.data[0])} /> <TableHead head={Object.keys(props.data[0])} />