티스토리 뷰

4. 리액트

NoticeList.js 에서 list.map() 사용

패스트코드블로그 2020. 8. 18. 20:02

https://github.com/parksrazor/hawaii-react-aws/blob/master/src/pickle/teacher/notices/NoticeList.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React, {useEffect, useState} from "react";
import {Card, CardBody, CardTitle} from 'reactstrap';
import './notice.css'
import {NavLink} from "react-router-dom";
import axios from 'axios'
 
const NoticeList = ({match, history}) => {
    const [list, setList] = useState([])
    const [pages, setPages] = useState([])
    const [pager, setPager] = useState({})
    const [category, setCategory] = useState(["전체 보기""전체 공지"])
    const [selectedCate, setSelectedCate] = useState("")
    const [isVisible, setIsVisible] = useState(true)
    useEffect(() => {
        transPage('all', match.params.id - 1)
        axios.get(`http://localhost:5000/notice/getCategory`)
            .then(({data}) => {
                setCategory([...category].concat(data))
            })
    }, [])
    const transPage = (selectedCate, currentPage) => {
        let value = selectedCate
        if (selectedCate === '' || selectedCate === '전체 보기') value = 'all'
        axios.get(`http://localhost:5000/notice/list/${value}/${currentPage}`)
            .then(({data}) => {
                setList(data.list)
                setPager(data.pagination)
                let tempPages = []
                let temp = data.pagination
                if (temp.rowCount !== 0) {
                    setIsVisible(true)
                    let i = temp.startPage
                    for (; i <= temp.endPage; i++) {
                        tempPages.push(i + 1)
                    }
                } else {
                    setIsVisible(false)
                }
                setPages(tempPages)
            })
            .catch(err => {
                console.log(`${err}`)
            })
    }
    const handleSelectChange = value => {
        history.push(`/teacher/notice/list/1`)
        setSelectedCate(value)
        let temp = ''
        if (value === '전체 보기') {
            temp = ''
        } else {
            temp = value
        }
        transPage(temp, 0)
    }
    const onClickNext = () => {
        history.push(`/teacher/notice/list/${pager.nextBlock + 1}`)
        transPage(selectedCate, pager.nextBlock)
    }
    const onClickPrev = () => {
        history.push(`/teacher/notice/list/${pager.preBlock+1}`)
        transPage(selectedCate, pager.preBlock)
    }
    return <Card>
        <CardBody>
            <CardTitle>공지사항</CardTitle><br/>
            <div className="text-right">
                <select className="col-sm-2 t-notice-selectbox" onChange={e => handleSelectChange(e.target.value)}>
                    {category.map((i, index) => (<option key={index} value={i}>{i}</option>))}
                </select>
            </div>
            <table className="table t-notice-margin0" id={"t-notice-table"}>
                <thead>
                <tr>
                    <th>글번호</th>
                    <th>분류</th>
                    <th>제목</th>
                    <th>작성자</th>
                    <th>날짜</th>
                </tr>
                </thead>
                <tbody>
                {list.map((i, index) => (
                    <tr key={index}>
                        <td className={"t-notice-width100"}>{i.id}</td>
                        <td className={"t-notice-width150"}>{i.category}</td>
                        <td className={"t-notice-textAlign-left"}><NavLink
                            to={`/teacher/notice/detail/${i.id}`}>{i.title}</NavLink>
                            <span className="m-2 t-notice-comment">[{i.commentCount}]</span>
                        </td>
                        <td className={"t-notice-width150"}>{i.makerName}</td>
                        <td className={"t-notice-width200"}>{i.createDate.slice(010)}</td>
                    </tr>
                ))}
                </tbody>
            </table>
            <div className="text-right">
                <NavLink to={`/teacher/notice/write`}>
                    <button className="btn border-0 btn-outline-primary">쓰기</button>
                </NavLink>
            </div>
            <div className="btn-cover text-center">
                {isVisible ? <>
                        {pager.existPrev && (
                            <button onClick={() => onClickPrev()}
                                    className="btn border-0 btn-outline-primary">
                                이전
                            </button>
                        )}
                        {pages.map((i, index) =>
                            <NavLink to={`/teacher/notice/list/${i}`} key={index}>
                                <button onClick={() => {
                                    transPage(selectedCate, i - 1)
                                }} className="btn border-0 btn-outline-primary">
                                    {i}
                                </button>
                            </NavLink>
                        )}
                        {pager.existNext && (
                            <button onClick={() => onClickNext()}
                                    className="btn border-0 btn-outline-primary">
                                다음
                            </button>
                        )}
                    </>
                    :
                    <span>해당 글이 존재하지 않습니다</span>
                }
 
 
            </div>
        </CardBody>
    </Card>
}
export default NoticeList
cs

 

 

26: setList(data.list)

110: map 사용

{pages.map((i, index) =>

                            <NavLink to={`/teacher/notice/list/${i}`} key={index}>

                                <button onClick={() => {

                                    transPage(selectedCate, i - 1)

                                }} className="btn border-0 btn-outline-primary">

                                    {i}

                                </button>

                            </NavLink>

                        )}

 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함