๐Ÿ“’ [ TIL ] 2022.08.03_75์ผ์ฐจ # ์ตœ์ข… ํ”„๋กœ์ ํŠธ (17)

๋ฌธ๋ช…์ฃผยท2022๋…„ 8์›” 4์ผ
1
post-thumbnail

[ 2022-08-03 (์ˆ˜) ์˜ค๋Š˜์˜ TIL ]

[ Today Project ]

drf ๋ฐฑ์—”๋“œ๊ฐœ๋ฐœ + ํ”„๋ก ํŠธ๊ฐœ๋ฐœ
: ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ๋Š” ํŒ€ํ”„๋กœ์ ํŠธ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

[ Today Learn ]

  • ๋กœ๊ทธ์ธํ•œ ์‚ฌ๋žŒ๋งŒ ๊ธ€ ์ž‘์„ฑํŽ˜์ด์ง€๋กœ ์ด๋™

โœ๏ธ ๋‚ด๊ฐ€ ๋ฐฐ์šด๊ฒƒ, ์–ป์€๊ฒƒ

search_result.html

<!DOCTYPE html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>๋„ˆ ๊ทธ๊ฑฐ ํญ์Šค์•ผ</title>
    
    <!-- ํŒŒ๋น„์ฝ˜ -->
    <link rel="shortcut icon" href="../static/image/favicon.png" type="image/x-icon">

    <!-- ๋ถ€ํŠธ์ŠคํŠธ๋žฉ -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
        crossorigin="anonymous"></script>

    <!--์Šคํƒ€์ผ CSS -->
    <link rel="stylesheet" href="/static/CSS/pagenation.css">
    <link rel="stylesheet" href="../static/CSS/style.css">
    <link rel="stylesheet" href="../static/CSS/board.css">
</head>


<body onload="articleDetail()" style="background-color: rgb(43, 43, 43); color: black;">
    <!-- ๋กœ๊ทธ์ธ ๊ณต๊ฐ„ -->
    <div class="login-space">
        <div class="login-block">
            <button type="button" id="login-button" class="btn btn-light btn-sm" data-bs-toggle="modal"
            data-bs-target="#staticBackdrop">๋กœ๊ทธ์ธ</button>
            <button type="button" class="btn btn-light btn-sm" id="signup-button"
                onclick="location.href='signup.html'">ํšŒ์›๊ฐ€์ž…</button>
        </div>
    </div>

    <!-- ์ƒ๋‹จ๊ณต๊ฐ„ -->
    <div class="navbar">
        <!-- ์‚ฌ์ดํŠธ ๋กœ๊ณ  -->
        <img src="../static/image/logo.png" alt="" height="100px" onclick="location.href='index.html'">
    </div>

    <!-- ๊ฒ€์ƒ‰์—”์ง„ -->
    <!-- ๋“œ๋กญ๋‹ค์šด ๊ฒ€์ƒ‰ ์ ์šฉ๋ฒ• ํ™•์ธ์ค‘, ์•ˆ๋˜๋ฉด ๊ทธ๋ƒฅ ๊ฒ€์ƒ‰์ฐฝ์œผ๋กœ ๋†”๋‘˜ ์˜ˆ์ • -->
    <div class="input-group mt-3 mb-3 search-engine">
        <select type="button" id="searchOption">
            ์ „์ฒด ๊ฒŒ์‹œ๊ธ€
            <option><a class="dropdown-item" href="#">์ œ๋ชฉ</a></option>
            <option><a class="dropdown-item" href="#">๋‚ด์šฉ</a></option>
            <option><a class="dropdown-item" href="#">์ œ๋ชฉ+๋‚ด์šฉ</a></option>
            <option><a class="dropdown-item" href="#">์ž‘์„ฑ์ž</a></option>
        </select>
        <input type="text" class="form-control" placeholder="๊ฒŒ์‹œ๊ธ€ ๊ฒ€์ƒ‰" id="searchKeywords">
        <img src="static/image/search_button.png" onclick="search()" class="search-button">
    </div>

    <!-- ๊ฒŒ์‹œํŒํ–‰ ๋ฒ„ํŠผ๋“ค -->
    <div class="Top-bar1">
        <div class="Top-bar1-buttons row">
            <img class="board" src="static/image/board03.png" onclick="location.href='board.html?boards=10๋Œ€'">
            <img class="board" src="static/image/board02.png" onclick="location.href='board.html?boards=20๋Œ€'">
            <img class="board" src="static/image/board04.png" onclick="location.href='board.html?boards=30๋Œ€'">
            <img class="board" src="static/image/board01.png" onclick="location.href='board.html?boards=์ง์žฅ์ธ'">
            <img class="board" src="static/image/board05.png" onclick="location.href='board.html?boards=์—ฐ์ธ'">
            <img class="board" src="static/image/board06.png" onclick="location.href='board.html?boards=์ž์œ '">
            <img class="board" src="static/image/board07.png" onclick="location.href='board.html?boards=HOT'">
        </div>
    </div>
    <!-- ๊ฒŒ์‹œํŒ ๊ณต๊ฐ„ -->
    <div class="board-space">
        <!-- ๊ฒŒ์‹œํŒ ๋ช… -->
        <div class="board-name" ><b id="board-name"></b></div>
        <div class="posting-space1"><button class="posting-btn" onclick="postClick()">๊ฒŒ์‹œ๋ฌผ ์ž‘์„ฑ</button></div>
        <div class="second-floor-board col" >
            <!--๊ฒŒ์‹œํŒ -->
            <div class="worker-board row justify-content-center" >

                    <!-- ์ž์œ  ๊ฒŒ์‹œ๊ธ€ for๋ฌธ -->
                    <div id="ArticlesDiv" class="articles">
                        <div id="search-post" class="list-post" ></div>
                        </div>
                </div>
            </div>
        </div>
        <div class="paging" style="cursor:pointer"></div>
            <article class="article">
                <ul class="contents"></ul>
                <div class="buttons"></div>
            </article>
        <!-- Scripts -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/paginationjs/2.1.4/pagination.min.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/paginationjs/2.1.4/pagination.css"/>
        <script type="module" src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.esm.js"></script>
        <script nomodule src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.js"></script>
        <script src="/static/JS/api.js"></script>
        <script src="/static/Scripts/search.js"></script>
        <script src="/static/Scripts/article_detail.js"></script>
    </div>

    <!-- ๋กœ๊ทธ์ธ ๋ชจ๋‹ฌ์ฐฝ -->
    <div class="modal fade login-modal" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false"
    tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content login-modal-size">
            <div class="modal-header">
                <h5 class="modal-title">๋กœ๊ทธ์ธ</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">

                <div class="form-floating mb-3">
                    <input type="text" class="form-control" id="floatingloginID" placeholder="ID">
                    <label for="floatingInput">ID</label>
                </div>
                <div class="form-floating mb-3">
                    <input type="password" class="form-control" id="floatingloginPassword"
                        placeholder="Password">
                    <label for="floatingPassword">๋น„๋ฐ€๋ฒˆํ˜ธ</label>
                </div>
                ์•„์ง <a href="signup.html">๊ณ„์ •</a>์ด ์—†์œผ์‹œ๋‹ค๋ฉด?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">๋‹ซ๊ธฐ</button>
                <button type="button" class="btn btn-primary" onclick="handleLogin()">๋กœ๊ทธ์ธ</button>
        <script src="/static/Scripts/boards.js"></script>
    </div>



</body>

</html>

search.js


// ๊ฒ€์ƒ‰๊ธฐ๋Šฅ
async function search() {
    const searchData = {
        type: document.getElementById('searchOption').value,
        search: document.getElementById('searchKeywords').value,
    }

    const response = await fetch(`${backend_base_url}/article/search/`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            "Authorization": "Bearer " + localStorage.getItem("access"),
        },
        body: JSON.stringify(searchData)
    })
    if (searchData['search'] == '') {
        alert('๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!')
    } else {
        // ์„ธ์…˜ ์ €์žฅ
        response_json = await response.json()
        result = JSON.stringify(response_json)
        num = response_json.length
        sessionStorage.setItem("result", result)
        sessionStorage.setItem("keyword", searchData.search)
        sessionStorage.setItem("type", searchData.type)
        sessionStorage.setItem("num", num)
        window.location.href = `${frontend_base_url}/search_result.html`
        return result
    }
    

    response_json = await response.json()
    return response_json.articles
}

const PagingConf = {
    totalCount: 100,
    numbersPerPage: 5,
    navPageNumber: Math.ceil(sessionStorage.getItem("num")/10),
};
sessionStorage.removeItem("num")
const feedNumber = {
    num : 0
}

$(document).ready(function () {
    setPageInfo(PagingConf.numbersPerPage, PagingConf.navPageNumber, 1);
    // setFeedNumber( $(val) );
    makePageNav();
    getPosts();
});


function makePageNav() {
    const pageInfo = getPageInfo();
    renderPaging($('.paging'), PagingConf.totalCount, pageInfo.numbersPerPage, pageInfo.navPageNumber, pageInfo.currentPage, true);
}

function renderPaging(appendElement, totalCount, numbersPerPage, navPageNumber, currentPage) {

    let totalPage = Math.ceil(totalCount / numbersPerPage); // ์ „์ฒด ํŽ˜์ด์ง€์ˆ˜
    let pageGroup = Math.ceil(currentPage / navPageNumber); // 1~10 : 1๊ทธ๋ฃน 11~20 : 2๊ทธ๋ฃน

    let last = pageGroup * navPageNumber; // ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€์ˆ˜, ์ „์ฒด ํŽ˜์ด์ง€์ˆ˜ ๋„˜์ง€์•Š๊ฒŒ ์ฒ˜๋ฆฌ
    if (last > totalPage) {
        last = totalPage;
    }

    let first = last - (navPageNumber - 1);
    let front = 1;
    let prev = first - 1;
    let next = last + 1;
    let rear = totalPage;

    let innerHtml = ''; // ์ด๋™ ํŽ˜์ด์ง€ ์ˆ˜์™€ ํ™”์‚ดํ‘œ html

    // ์™ผ์ชฝ < <<
    if (prev > 0) {
        innerHtml += '<a href=#token operator">+ front + ')"> << </a>';
        innerHtml += '<a href=#token operator">+ prev + ')"> < </a> ';
    }
    // ์ค‘๊ฐ„ ์ด๋™ ํƒœ๊ทธ
    for (let i = first; i <= last; i++) {
        if (currentPage === i) {
            innerHtml += '<a href=# class="on"token operator">+ i + ')">' + i + '</a> ';
        } else {
            innerHtml += '<a href=#token operator">+ i + ')">' + i + '</a> ';
        }
    }
    // ์˜ค๋ฅธ์ชฝ > >>
    if (last < totalPage) {
        innerHtml += '<a href=#token operator">+ next + ')"> > </a>';
        innerHtml += '<a href=#token operator">+ rear + ')"> >> </a>';
    }

    appendElement.empty();
    appendElement.append(innerHtml);
}

function movePage(currentPage) {
    const pageInfo = getPageInfo();
    setPageInfo(pageInfo.numbersPerPage, pageInfo.navPageNumber, currentPage);
    makePageNav();
    getPosts();
}

function setPageInfo(numbersPerPage, navPageNumber, currentPage) {
    const pageInfo = {
        numbersPerPage: numbersPerPage,
        navPageNumber: navPageNumber,
        currentPage: currentPage
    };
    sessionStorage.setItem('pageInfo', JSON.stringify(pageInfo));
}

function getPageInfo() {
    return JSON.parse(sessionStorage.getItem('pageInfo'));
}

function getFeedInfo() {
    return JSON.parse(sessionStorage.getItem('pageInfo'));
}
function postClick() {
    if (!localStorage.getItem("access")) {
        alert('๋กœ๊ทธ์ธํ•ด์ฃผ์„ธ์š”!')
    } else {
        window.location.href='posting.html'
    }
}

function getPosts() {

    $('#search-post').empty()

    const pageInfo = getPageInfo();

    let page = pageInfo.currentPage;
    let offset = pageInfo.numbersPerPage;
    let params = '?page=' + page;
    params += '&offset=' + offset;
    $.ajax({
        type: 'POST',
        url: `${backend_base_url}/article/search/`,
        data: {            
            type: sessionStorage.getItem("type"),
            search: sessionStorage.getItem("keyword")
        },
        async: false,
        success: function (response) {        
            console.log(response) 
            if (response.length != 0) {
                let posts = response;
                console.log(posts)
                let board_html = `<b id="board-name">'${sessionStorage.getItem("keyword")}' ๊ฒ€์ƒ‰๊ฒฐ๊ณผ</b>`
                $('#board-name').empty()
                $('#board-name').append(board_html)
                for (let i = 0; i < posts.length; i++) {
                    let cnt = i + 1
                    const id = posts[i]['id']
                    const title = posts[i]['article_title']
                    const author = posts[i]['author']
                    const date = posts[i]['article_post_date']

                    let temp_html = `<div class="post-line row" onclick="location.href='${frontend_base_url}/detail.html?id=${id}'">
                                        <div class="th number col-1" scope="col">${cnt}.</div>
                                        <div class="th title col-6" scope="col"><b>${title}</b></div>
                                        <div class="th author col col-md-col1" scope="col">${author}</div>
                                        <div class="th date col" scope="col">${date}</div>
                                    </div>`
                    $('#search-post').append(temp_html)
                }
            }
        },
        fail: function (response) {
            console.log("fail")
            // window.location.reload()
        }
    });
}
profile
ํ•˜๋ฃจ ํ•œ๊ฑธ์Œ์”ฉ ๊พธ์ค€ํžˆ ๋‚˜์•„๊ฐ€๋Š” ๊ฐœ๋ฐœ์ž๐Ÿ™†โ€โ™€๏ธ https://github.com/Moonmooj

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด