[3주차] ajax 복습

정우진·2022년 7월 17일
0

[주요내용]

  1. 웹페이지 로딩 후 자동 실행 기본 코드
    $(document).ready(function () {
    listing();
    });

    function listing() {
        alert('로딩시작')
    }
  2. 별점의 경우 별 박스가 따로 존재함 -> ⭐

  3. temp_html의 경우 별점을 붙일 때는 방법이 다소 다름

  • 첫번째로 let star_image = '⭐'.repeat(star) 먼저 해주고
  • temp_html에 붙일 때

    ${star_image}

    붙이면 됨
  1. 포스팅 닫기 열기 복습 코드

    function note_box() {
        count += 1
        if (count % 2 == 0) {
            $('#form-box').show()
        } else {
            $('#form-box').hide()
        }
    
        $(function () {
            $('#button1').click(function () {
                if (count % 2 == 0) {
                    $(this).html('포스팅박스 닫기');
                } else {
                    $(this).html('포스팅박스 열기');
                }
            });
        });
    }
    

[코드보기]

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
            integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
            crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
            integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
            crossorigin="anonymous"></script>

    <title>스파르타코딩클럽 | 부트스트랩 연습하기</title>
    <link href="https://fonts.googleapis.com/css2?family=Jua&display=swap" rel="stylesheet">

    <style>
        * {
            font-family: 'Gowun Dodum', sans-serif;
        }

        .card-note {
            width: 900px;
            margin: auto;
        }

        .comment {
            color: blue;
            font-weight: bold;
        }

        .form {
            width: 500px;
            margin: 0px auto 50px auto;
            border: solid;
            padding: 50px;
            border-radius: 50px;

            display: none;

        }
    </style>
    <script>
        $(document).ready(function () {
            listing();
        });

        function listing() {

            $('#card-box').empty()

            $.ajax({
                type: "GET",
                url: "http://spartacodingclub.shop/web/api/movie",
                data: {},
                success: function (response) {
                    let rows = response['movies']
                    for (let i = 0; i < rows.length; i++) {
                        let title = rows[i]['title']
                        let star = rows[i]['star']
                        let img = rows[i]['image']
                        let desc = rows[i]['desc']
                        let comment = rows[i]['comment']

                        let star_image = '⭐'.repeat(star)

                        let temp_html = `<div class="card">
                                                <img class="card-img-top"
                                                 src="${img}"
                                                 alt="Card image cap">
                                                <div class="card-body">
                                                    <a class="card-title" href="https://www.naver.com/" target="_blank">${title}</a>
                                                    <p class="card-text">${desc}</p>
                                                    <p>${star_image}</p>
                                                    <p class="card-text comment">${comment}</p>
                                                </div>
                                          </div>`

                        $('#card-box').append(temp_html)



                    }
                }
            })
        }


        let count = 0

        function note_box() {
            count += 1
            if (count % 2 == 0) {
                $('#form-box').show()
            } else {
                $('#form-box').hide()
            }

            $(function () {
                $('#button1').click(function () {
                    if (count % 2 == 0) {
                        $(this).html('포스팅박스 닫기');
                    } else {
                        $(this).html('포스팅박스 열기');
                    }
                });
            });


        }
    </script>
</head>

<body>
<div class="card-note">
    <div class="jumbotron">
        <h1 class="display-4">나홀로 링크 메모장</h1>
        <p class="lead">중요한 링크를 저장해두고, 나중에 볼 수 있는 공간입니다.</p>
        <hr class="my-4">
        <p class="lead">
            <button onclick="note_box()" class="btn btn-primary btn-lg" href="#" role="button" id="button1">포스팅박스 열기
            </button>
        </p>
    </div>
    <div class="form" id="form-box">
        <div class="form-group">
            <label for="exampleInputEmail1">아티클 URL</label>
            <input type="email" class="form-control" id="url" aria-describedby="emailHelp"
                   placeholder="url을 입력하세요">
        </div>
        <div class="form-group">
            <label for="exampleFormControlTextarea1">간단 코멘트</label>
            <textarea class="form-control" id="exampleFormControlTextarea1" rows="3"
                      placeholder="코멘트를 입력하세요"></textarea>
        </div>
        <div class="form-group">
            <label class="input-group-text" for="inputGroupSelect01">별점</label>
            <select class="form-select" id="inputGroupSelect01">
                <option selected>-- 선택하기 --</option>
                <option value="1"></option>
                <option value="2">⭐⭐</option>
                <option value="3">⭐⭐⭐</option>
                <option value="4">⭐⭐⭐⭐</option>
                <option value="5">⭐⭐⭐⭐⭐</option>
            </select>
        </div>
        <button type="submit" class="btn btn-primary">기사저장</button>
    </div>
    <div class="card-columns" id="card-box">
    </div>
</div>
</body>

</html>
profile
야하마 군산악기사 운영자

0개의 댓글