๋ฐ˜์‘ํ˜•
250x250
Recent Posts
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Hello creators ๐Ÿ™Œ

2-9_Quiz_Ajax ์—ฐ์Šตํ•˜๊ธฐ(1) ๋ณธ๋ฌธ

[WEB & AI] (feat. ์ทจ์ค€)/์ŠคํŒŒ๋ฅดํƒ€์ฝ”๋”ฉํด๋Ÿฝ_์›น๊ฐœ๋ฐœ์ข…ํ•ฉ๋ฐ˜

2-9_Quiz_Ajax ์—ฐ์Šตํ•˜๊ธฐ(1)

๋ถ€์‹œ๋งค๋‚˜_HA 2023. 10. 6. 07:03
728x90
๋ฐ˜์‘ํ˜•

โ… . ์ด๋ฒˆ ํšŒ์ฐจ ํ•™์Šต ๋ชฉํ‘œ (goal)


1. ๋ฐฐ์šฐ๊ณ ์ž ํ•˜๋Š” ๊ฒƒ

  • ๋”ฐ๋ฆ‰์ด ๋ฐ์ดํ„ฐ๋กœ ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค๊ธฐ
    • ์—…๋ฐ์ดํŠธ ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด -> ๋‚˜์˜ค๊ฒŒ
    • 5๊ฐœ ๋ฏธ๋งŒ์ด๋ฉด -> ๋นจ๊ฐ„์ƒ‰



โ…ก. ๋”ฐ๋ฆ‰์ด API ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ํ™•์ธ


1. ํ•ด๋ด

  • ๋”ฐ๋ฆ‰์ด API

    http://spartacodingclub.shop/sparta_api/seoulbike
  • ๋ณด๋ฉด์„œ ํ™•์ธํ•  ๊ฒƒ

    • ์ „์ฒด ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ
    • ํ•„์š”ํ•œ ํ•ญ๋ชฉ ์ด๋ฆ„ ์ฒดํฌ



โ…ข. ajax ํ€ด์ฆˆ 1


1. ๊ธฐ๋ณธ ์ œ๊ณต ์ฝ”๋“œ

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

<head>
    <meta charset="UTF-8">
    <title>JQuery ์—ฐ์Šตํ•˜๊ณ  ๊ฐ€๊ธฐ!</title>
    <!-- JQuery๋ฅผ import ํ•ฉ๋‹ˆ๋‹ค -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    <style type="text/css">
        div.question-box {
            margin: 10px 0 20px 0;
        }

        table {
            border: 1px solid;
            border-collapse: collapse;
        }

        td,
        th {
            padding: 10px;
            border: 1px solid;
        }
    </style>

    <script>
        function q1() {
            // ์—ฌ๊ธฐ์— ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
        }
    </script>

</head>

<body>
    <h1>jQuery + Ajax์˜ ์กฐํ•ฉ์„ ์—ฐ์Šตํ•˜์ž!</h1>

    <hr />

    <div class="question-box">
        <h2>2. ์„œ์šธ์‹œ OpenAPI(์‹ค์‹œ๊ฐ„ ๋”ฐ๋ฆ‰๊ธฐ ํ˜„ํ™ฉ)๋ฅผ ์ด์šฉํ•˜๊ธฐ</h2>
        <p>๋ชจ๋“  ์œ„์น˜์˜ ๋”ฐ๋ฆ‰์ด ํ˜„ํ™ฉ์„ ๋ณด์—ฌ์ฃผ์„ธ์š”</p>
        <p>์—…๋ฐ์ดํŠธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค ์ง€์› ๋‹ค ์ƒˆ๋กœ ์”Œ์—ฌ์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค.</p>
        <button onclick="q1()">์—…๋ฐ์ดํŠธ</button>
        <table>
            <thead>
                <tr>
                    <td>๊ฑฐ์น˜๋Œ€ ์œ„์น˜</td>
                    <td>๊ฑฐ์น˜๋Œ€ ์ˆ˜</td>
                    <td>ํ˜„์žฌ ๊ฑฐ์น˜๋œ ๋”ฐ๋ฆ‰์ด ์ˆ˜</td>
                </tr>
            </thead>
            <tbody id="names-q1">
                <tr>
                    <td>102. ๋ง์›์—ญ 1๋ฒˆ์ถœ๊ตฌ ์•ž</td>
                    <td>22</td>
                    <td>0</td>
                </tr>
                <tr>
                    <td>103. ๋ง์›์—ญ 2๋ฒˆ์ถœ๊ตฌ ์•ž</td>
                    <td>16</td>
                    <td>0</td>
                </tr>
                <tr>
                    <td>104. ํ•ฉ์ •์—ญ 1๋ฒˆ์ถœ๊ตฌ ์•ž</td>
                    <td>16</td>
                    <td>0</td>
                </tr>
            </tbody>
        </table>
    </div>
</body>

</html>


2. ''์–ด๋–ค html tag'' ๋ฅผ ๋„ฃ์–ด์•ผ '==์›ํ•˜๋Š” ๋ Œ๋”๋ง=='์ด ๋˜๋Š”์ง€ ํ™•์ธ

  • ๋ Œ๋”๋ง ๋œ๊ฑธ ๋ˆˆ์œผ๋กœ ๋ณด๋ฉด -> ์นธ ๋“ค์ด ๋“ค์–ด๊ฐ€์•ผ ํ•จ

  • ==์นธ== ์„ ํ‘œํ˜„ํ•˜๋Š” html tag ๋Š”? -> tr ์ด ๋…€์„๋“ค

  • ๊ทธ๊ฑธ ํฌํ•จํ•˜๋Š” tag ์— id ๋„ฃ๊ธฐ

โญโญโญโญโญ ๋ Œ๋”๋ง ๋˜์–ด์•ผ ํ•˜๋Š” ๊ฒƒ -> ๊ทธ๊ฑฐ์— ํ•ด๋‹นํ•˜๋Š” html ํƒœ๊ทธ -> ํฌ๊ด„ํ•˜๋Š” id ํ™•์ธ ์ˆœ์œผ๋กœ



3. ajax ๋กœ ๋ฐ›์•„์™€์„œ console ์— ์ฐ๊ธฐ : for ๋ฌธ์„ ๋Œ๋ฆด ์ˆ˜ ์žˆ๊ธฐ ์ „๊นŒ์ง€ ์ฐ๊ธฐ

<script>
    function q1() {
        // ์—ฌ๊ธฐ์— ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
        $.ajax({
            type: "GET",
            url: "http://spartacodingclub.shop/sparta_api/seoulbike",
            data: {},
            success: function (response) {
                console.log(response['getStationList']['row'])
            }
        })
    }
</script>
  • ์ฃผ์˜!
    • ๊ธฐ์กด์— ์ž‘์„ฑ๋œ ์ „์ฒด ์ฝ”๋“œ์—์„œ q1 ์ด ์‹คํ–‰๋˜๋Š” ์กฐ๊ฑด์€ ์—…๋ฐ์ดํŠธ ๋ฒ„ํŠผ ํด๋ฆญ ์ž„. ๋”ฐ๋ผ์„œ, ๊ทธ๊ฑธ ํด๋ฆญํ•ด์•ผ response ๋ฅผ ์ฐ์–ด๋ณผ ์ˆ˜ ์žˆ์Œ. โญโญโญโญโญ
      • console ๋กœ ์ฐ์„ ๋•Œ, for๋ฌธ ์œผ๋กœ ๋Œ๋ฆด ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๊นŒ์ง€ ํ™•์ธํ•˜๊ธฐ -> ๊ทธ๋Ÿฌ๋ฉด, ๋ณ€์ˆ˜์— ๋„ฃ๊ธฐ ์ข‹์Œ โญโญโญโญโญโญโญ


4. ๋ณ€์ˆ˜์— ๋„ฃ๊ณ  for๋ฌธ ๋Œ๊ธฐ๊ธฐ

  • ์ž‘์„ฑ ์ฝ”๋“œ
<script>
function q1() {
    // ์—ฌ๊ธฐ์— ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
    $.ajax({
        type: "GET",
        url: "http://spartacodingclub.shop/sparta_api/seoulbike",
        data: {},
        success: function (response) {
            let rows = response['getStationList']['row']

            for (let i = 0; i < rows.length; i++) {
                let name = rows[i]['stationName']
                let rack = rows[i]['rackTotCnt']
                let bike = rows[i]['parkingBikeTotCnt']

                console.log(name, rack, bike)
            }
        }
    })
}
</script>

  • ์ค‘์š”ํ•œ ๊ฒƒ
    • ์ด๋•Œ, ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๋Œ€๋กœ console ์— ์ฐํžˆ๋Š”์ง€ ๊ผญ! ํ™•์ธ โญโญโญโญโญ

  • ๊ฒฐ๊ณผ๋ฌผ


5. jquery_append ์‚ฌ์šฉํ•ด์„œ ์›ํ•˜๋Š” html ๋งŒ๋“ค์–ด์ฃผ๊ธฐ


  • ์šฐ์„  ๋Œ์•„๊ฐ€๋Š” ์ฝ”๋“œ

    <script>
    function q1() {
    
      // empty() = ์—…๋ฐ์ดํŠธ ๊ธฐ๋Šฅ ํ•ต์‹ฌ โญโญโญโญโญ 
      $('#names-q1').empty()
    
      $.ajax({
          type: "GET",
          url: "http://spartacodingclub.shop/sparta_api/seoulbike",
          data: {},
          success: function (response) {
              let rows = response['getStationList']['row']
    
              for (let i = 0; i < rows.length; i++) {
                  let name = rows[i]['stationName']
                  let rack = rows[i]['rackTotCnt']
                  let bike = rows[i]['parkingBikeTotCnt']
    
                  let temp_html = `<tr>
                                      <td>${name}</td>
                                      <td>${rack}</td>
                                      <td>${bike}</td>
                                  </tr>`
    
                  $('#names-q1').append(temp_html)
    
    
        }
    }
})

}



- ์ง€์›Œ์ฃผ๋Š”๊ฒŒ ์—†์œผ๋ฉด -> ์—…๋ฐ์ดํŠธ๊ฐ€ ์•„๋‹ˆ๋ผ '๋ˆ„์ ' ์ด ๋œ๋‹ค. โญโญโญโญโญโญ -> so, `empty()` ๊นŒ์ง€ ๊ผญ! ํ•ด์ค„ ๊ฒƒ 

<br>
<br>

### 6. 5๊ฐœ๋ณด๋‹ค ์ ์€ ๊ณณ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ์น ํ•˜๊ธฐ 

<br>

- css ์ฝ”๋“œ 
``` css
.urgent {
    color: red;
}

  • ์ž˜ ๋˜๋Š”์ง€ ํ…Œ์ŠคํŠธ
    let temp_html = `<tr class = "urgent">
                      <td>${name}</td>
                      <td>${rack}</td>
                      <td>${bike}</td>
                  </tr>`
  • ์กฐ๊ฑด๋ฌธ ์‹คํ–‰์ „ ๋ฐ˜๋“œ์‹œ ์ž˜ ๋˜๋Š”์ง€ ํ…Œ์ŠคํŠธ
    • ์ด๊ฑธ ํ•˜๋Š”๊ฒŒ ์ค‘์š”ํ•จ โญโญโญโญโญ

  • ์กฐ๊ฑด๋ฌธ ์ ์šฉํ•ด์„œ ๋„ฃ์–ด์ฃผ๊ธฐ

    <script>
    function q1() {
    
      $('#names-q1').empty()
    
      $.ajax({
          type: "GET",
          url: "http://spartacodingclub.shop/sparta_api/seoulbike",
          data: {},
          success: function (response) {
              let rows = response['getStationList']['row']
    
              for (let i = 0; i < rows.length; i++) {
                  let name = rows[i]['stationName']
                  let rack = rows[i]['rackTotCnt']
                  let bike = rows[i]['parkingBikeTotCnt']
    
                  // ๋น„์–ด์žˆ๋Š” temp html 
                  let temp_html = `   `
    
                  if (bike < 5) {
                      temp_html = `<tr class = "urgent">
                                      <td>${name}</td>
                                      <td>${rack}</td>
                                      <td>${bike}</td>
                                  </tr>`
                  } else {
                      temp_html = `<tr>
                                      <td>${name}</td>
                                      <td>${rack}</td>
                                      <td>${bike}</td>
                                  </tr>`
                  }
                  $('#names-q1').append(temp_html)
              }
          }
      })
    }
    </script>
  • ๊ต‰์žฅํžˆ ์ค‘์š”ํ•œ ํŒจํ„ด์ธ ๊ฒƒ ๊ฐ™์€๋ฐ โญโญโญโญโญโญโญโญโญโญโญโญ



๐Ÿ’ญ What i learned


  • '๋ Œ๋”๋ง ๋˜์–ด์•ผ ํ•˜๋Š” ๊ฒƒ', '์–ด๋–ค html ํƒœ๊ทธ?' '๊ทธ๊ฑธ ํฌ๊ด„ํ•˜๋Š” id' ๋ฅผ ํ™•์ธ ์ˆœ์œผ๋กœ ์ง„ํ–‰

  • [ajax ๋กœ ๋ฐ›์•„์™€์„œ console ์— ์ฐ์„ ๋•Œ] ๊ธฐ์กด์— ์ž‘์„ฑ๋œ ์ „์ฒด ์ฝ”๋“œ์—์„œ q1 ์ด ์‹คํ–‰๋˜๋Š” ์กฐ๊ฑด์€ ์—…๋ฐ์ดํŠธ ๋ฒ„ํŠผ ํด๋ฆญ ์ž„. ๋”ฐ๋ผ์„œ, ๊ทธ๊ฑธ ํด๋ฆญํ•ด์•ผ response ๋ฅผ ์ฐ์–ด๋ณผ ์ˆ˜ ์žˆ์Œ. โญโญโญโญโญ

  • [ajax ๋กœ ๋ฐ›์•„์™€์„œ console ์— ์ฐ์„ ๋•Œ] console ๋กœ ์ฐ์„ ๋•Œ, for๋ฌธ ์œผ๋กœ ๋Œ๋ฆด ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๊นŒ์ง€ ํ™•์ธํ•˜๊ธฐ -> ๊ทธ๋Ÿฌ๋ฉด, ๋ณ€์ˆ˜์— ๋„ฃ๊ธฐ ์ข‹์Œ โญโญโญโญโญโญโญ

  • [๋ณ€์ˆ˜ ๋„ฃ๊ณ  for ๋ฌธ ๋Œ๋ฆฌ๊ธฐ] for ๋ฌธ์— ๋„ฃ๊ณ  ๋ณ€์ˆ˜์— ๋Œ๋ฆด ๋•Œ, ๋‚ด๊ฐ€ ์›ํ•˜๋Š”๋Œ€๋กœ ์ฐํžˆ๋Š”์ง€ ๊ผญ ํ™•์ธ

  • [์›ํ•˜๋Š” HTML ๋งŒ๋“ค๋•Œ] ์ง€์›Œ์ฃผ๋Š”๊ฒŒ ์—†์œผ๋ฉด -> ์—…๋ฐ์ดํŠธ๊ฐ€ ์•„๋‹ˆ๋ผ '๋ˆ„์ ' ์ด ๋œ๋‹ค. โญโญโญโญโญโญ -> so, empty() ๊นŒ์ง€ ๊ผญ! ํ•ด์ค„ ๊ฒƒ

  • [5๊ฐœ ๋ฏธ๋งŒ ๋นจ๊ฐ„์ƒ‰ ํ‘œ์‹œ] ์กฐ๊ฑด๋ฌธ ์‹คํ–‰์ „ ๋ฐ˜๋“œ์‹œ ์ž˜ ๋˜๋Š”์ง€ ํ…Œ์ŠคํŠธ / ํ…Œ์ŠคํŠธ ํ•˜๋Š” ๊ฒƒ๋„ ๋Šฅ๋ ฅ์ด๋ผ๊ณ  ์ƒ๊ฐํ•จ. โญโญโญโญโญ

728x90
๋ฐ˜์‘ํ˜•
Comments