2 글 보임 - 1 에서 2 까지 (총 2 중에서)
-
글쓴이글
-
2022년 7월 27일 17:38 #39610
송송송참가자미니 사이트를 직접 만들고 있습니다. json 파일을 직접 만들어서 그거를 불러와서 하고 있는데 날짜 검색+키워드 포함한 검색에서 계속 오류가 나서 최대한 해보려했는데,, 감을 못잡아서 질문드립니다.
날짜 선택하고, 키워드 입력 --> 처음에는 잘 나오는데, 두 번째부터 다른 것들이 추가 됨 날짜변경 코드들은 안 넣었을 때는 정상적으로 작동하다, 날짜변경 코드를 추가하니, 오류가 넘쳐나네요..
const LostList = memo(() => { // 처음 날 const [startDate, setStartDate] = useState(); // 마지막 날 const [endDate, setEndDate] = useState(); // 날짜 변환 const startTime = moment(startDate).format("YYYY.MM.DD"); const endTime = moment(endDate).format("YYYY.MM.DD"); let startDateArray = startTime.split("."); let endDateArray = endTime.split(".");
// input ref const keywordInput = useRef();
// 키워드 let [openLost, setOpenLost] = useState(false); // 모달창 열기 let [openLostModal, setOpenLostModal] = useState(false);
// 리스트 만드는 상태값 const [lostList, setLostList] = useState([]); // 리스트 개수 상태값 const [count, setCount] = useState(0);
const compareInt = (sd, ed, rd) => { if (sd <= rd && rd <= ed) { return 1; } else { return -1; } };
// 날짜 비교하기 const compareDate = (json) => { lostList == "" ? setCount(json.length) : setCount(openLost.length);
json.map((v, i) => { { // json 데이터를 분리해서 담음 let regDateArray = v.L_reg_date.split(".");
let year = compareInt( startDateArray[0], endDateArray[0], regDateArray[0] );
let month = compareInt( startDateArray[1], endDateArray[1], regDateArray[1] );
let day = compareInt( startDateArray[2], endDateArray[2], regDateArray[2] );
// 선택한 날짜 기간에 저장된 데이터가 포함되어 있으면, 배열에 추가 if (year == 1 && month == 1 && day == 1) { newArray.push(v); } } }); };
useEffect(() => { (async () => { let json = null;
try { const response = await axios.get(`http://localhost:3001/bbs_lost`);
// json에 리스트 담기 json = response.data; setLostList(json);
compareDate(json); } catch (e) { console.log(e); } })(); }, [openLost]);
const onFilterKeyword = () => { const searchKeyword = keywordInput.current.value;
// 리스트에서 input창에 입력된 값이 json에 있으면 let newFilter = lostList.filter((value) => { return value.L_item.includes(searchKeyword); });
// 검색어가 있다면 if (searchKeyword) { compareDate(newFilter); }
setOpenLost(newArray);
// 중복으로 나오니 배열 초기화 newArray = [];
console.log("클릭"); };
-
글쓴이글
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
- 답변은 로그인 후 가능합니다.