board.ejs 4.75 KB
<!DOCTYPE html>
<html>
<head>
    <title><%= title %></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

    <script>
        $(function(){ // pagination 현재 페이지 활성화 시킴
            var page = location.href.split("page=")[1]; // url에 page 넘버로 구분
            var index = page-1; // 0부터 시작이므로 1 빼줌

            if(page == null) { // 메인화면에서는 page 쿼리가 없으므로 빈값일 때
                $(".pagination a:eq(0)").attr('class', 'current-page');
            }

            $(".pagination a:eq(" + index + ")").attr('class', 'current-page');
        });

        function searchContent() {
            if($('#searchWord').val == ''){
                alert("검색어를 입력해주세요!!!");
            } else {
                $('#searchAction').submit();
            }
        }
        function submitContents() {
            var name = $('#addContentName').val();
            var company = $('#addContentCompany').val();
            var category = $('#addContentCategory').val();
            var price = $('#addContentPrice').val();

            // 새 글 등록 시
            if(name == '' || company == '' || category == '' || price == '') {
                alert("이름, 회사, 카테고리, 가격 모두 있어야합니다.");
                return;
            } else {
                $('#writeAction').submit();
            }
        }
    </script>
</head>
<body>
<div class="main">
    <a href="/boards"><h2>게시판</h2></a>
    <table class="board_list">
        <tr>
            <th>Rank</th>
            <th>Name</th>
            <th>Company</th>
            <th>Category</th>
            <th>Price</th>
        </tr>
        <%if(contents.length>0){%>
        <%var i = 0;%>
        <%contents.forEach(function(item){%>
        <%i++;%>
        <tr>
            <td class="number"><%=(page-1)*10+i%></td>
            <td class="name"><a href="/boards/view?id=<%=item._id%>"><%=item.name%>  [<%=item.comments.length%>]&nbsp;&nbsp;</a></td>
            <td class="company"><%=item.company%></td>
            <td class="category"><%=item.category%></td>
            <td class="average"><%=item.average%></td>
        </tr>
        <%})%>
        <%} else {%>
        <tr>
            <td colspan="5">게시물이 없습니다.</td>
        </tr>
        <%}%>
    </table>

    <div class="pagination">
        <%
        if(searchWord != '') {
            for(var i=1; i<=pagination; i++){
        %>
            <a href="/boards/search?searchWord=<%=searchWord%>&page=<%=i%>" class="next-page"><%=i%></a>
        <%
            }
        } else {
        for(var i=1; i<=pagination; i++){
        %>
        <a href="/boards?page=<%=i%>" class="next-page"><%=i%></a>
        <%}}%>
    </div>

    <div class="btn_group">
        <div class="search">
            <form action="/boards/search" method="get" id="searchAction" name="searchAction">
                <input type="text" class="search_word" id="searchWord" name="searchWord">
        <input type="radio" name="company" value="CU">CU</input>
        <input type="radio" name="company" value="GS25">GS25</input>
        <input type="radio" name="company" value="SevenEleven">SevenEleven</input>
        <input type="radio" name="company" value="all" style="visibility:hidden" checked="true"></input>
        <br>
        <input type="radio" name="category" value="주먹밥">주먹밥</input>
        <input type="radio" name="category" value="샌드위치">샌드위치</input>
        <input type="radio" name="category" value="도시락">도시락</input>
        <input type="radio" name="category" value="all" style="visibility:hidden" checked="true"></input>
        <a href="#" onclick="searchContent();"><div class="search_btn">검색</div></a>
    </form>
    </div>
    </div>
    <!-- new content write form-->
    <div class="write_form">
        <form id="writeAction" action="/boards?mode=add" method="post">
            <input type="text" class="inputName" id="addContentName" name="addContentName" placeholder="name">
            <input type="text" class="inputCompany" id="addContentCompany" name="addContentCompany" placeholder="company">
            <input type="text" class="inputCategory" id="addContentCategory" name="addContentCategory" placeholder="category">
            <input type="text" class="inputPrice" id="addContentPrice" name="addContentPrice" placeholder="price"></textarea>

            <div id = "new" class="addBtngroup">
                <a onclick="submitContents();"><div>SUBMIT</div></a>
                <a onclick="cancelWriteForm('cancel');"><div>CANCEL</div></a>
            </div>
        </form>
    </div>
    <!-- write form end-->

</div>
</body>
</html>