jQuery
์์ฝ:
jQuery๋ ์ฝ์์ฐฝ์์ ํน์ id๋ฅผ ๊ฐ๋ ์์์ ๋ช ๋ น์ ๋์ง๋ ๊ฒ์ด๋ค. Input Box์ ๊ฐ์ ๊ฐ์ ธ์ค๊ฑฐ๋ ์์ ํ ์ ์๊ณ , <div>์ ๋ณด์ด๊ฑฐ๋ ์จ๊ธฐ๊ฑฐ๋ ํ ์ ์๋ค. ๋๋ถ์ด CSS์ ์์ฑ๊ฐ๋ ๊ฐ์ ธ์ฌ ์ ์๋ค.
์ญ์ ๋ฌธ์ ๋ฅผ ํ๋ฉด์ ์ค์ ๋ก ๋ด๊ฐ ์๋์ง ๋ชจ๋ฅด๋์ง ํ์ธํ๋ ๊ฒ ๊ฐ์ฅ ์ ํํ๋ค. ๋ฌธ์ ๋ฅผ ํ๋ฉด์ jQuery, JS์ ๋ํด์ ๋ ์๋ฟ๊ฒ ๋์๋ค.
CSS ๋ฌธ๋ฒ ํ์๊ณผ JQ, JS๋ ์ฐ์์ด ๋ค๋ฅด๊ธฐ์ ๋ฌธ๋ฒ์ด ๋ค๋ฅด๊ณ (๋น์ฐํ์ง๋ง, ์ ์ฌํ ๊ธฐ๋ฅ(display ๋ฑ)์ ํ๋ ๋ถ๋ถ์ด ์์ด์ ํท๊ฐ๋ ธ๋ค) CSS๋ก ์ค์ ์ ํด์ค์ผ ํ ์ง, JQ, JS๋ก ์กฐ์์ ํด์ผ ํ ์ง ๋จผ์ ์๊ฐํด ๋ณด๊ณ ์ฝ๋ฉํ๋ ๊ฒ์ด ์ข๊ฒ ๋ค.
์์
- ํ Div ํ๊ทธ๋ฅผ ์ฒ์ ํ์ด์ง loading ์ ํน์ ์ํ๋ก ์ ์งํ ์ ์๋๋ก ํ ๋
#post-box {
display: block;
}
- Div ํ๊ทธ์ 'display' ์ํ๋ฅผ ์๊ณ ์ถ์ ๋
let status = $('#post-box').css('display') // block or none ์ผ๋ก ๋ฐํ
์ ์ฒ๋ผ ์ ์ฌ ๊ธฐ๋ฅ์ด๋๋ผ๋ ์กฐ์ํด์ค์ผ ํ๋ ๋ถ๋ถ(CSS, JQ, JS)์ด ๋ค๋ฅด๊ธฐ์ ์ ๋ ํ ํ์๊ฐ ์๋ค.
Ajax
- ์ฌ์ฉ ์์น: HTML <script> ... </script>
- ๊ธฐ๋ณธ ๊ณจ๊ฒฉ ๊ตฌ์กฐ:
$.ajax({
type: "GET",
url: "์ฌ๊ธฐ์URL์์
๋ ฅ",
data: {},
success: function(response){
console.log(response)
}
})
์ ๊ตฌ์กฐ์์ 'response'๋ dictionary ํํ๋ก ๋ฐํ๋ฐ๊ฒ ๋๋ค. ํต์ dic, list ํผํฉ ๊ตฌ์กฐ์ผ ๊ฒ์ผ๋ก ์๊ฐ๋จ.
์ด๋ฅผ ์ ์ ํ ์กฐ์ํ์ฌ ์ํ๋ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค.
์์
function q1() {
$.ajax({
type: "GET",
url: "http://openapi.seoul.go.kr:8088/6d4d776b466c656533356a4b4b5872/json/RealtimeCityAir/1/99",
data: {},
success: function(response){
$('#names-q1').empty()
dic = response["RealtimeCityAir"]["row"]
for (i = 0; i < dic.length ; i++) {
gu_name = dic[i]["MSRSTE_NM"]
dust = dic[i]["IDEX_MVL"]
if (dust >= 120) {
dust_check = "dust_over";
}
else {
dust_check = "dust_lower";
}
temp_html = `<li >${gu_name}: <span class = ${dust_check}>${dust}</span></li>`
$('#names-q1').append(temp_html)
}
}
})
}
for ๋ฌธ์ ํตํด์ <ul>....</ul> ํ๊ทธ์ ๋ชจ๋ ์ ๋ณด๋ฅผ ์ ๋ ฅํด ์ฃผ๋ ์กฐ์
์ ์กฐ์์ ํตํด์ response์ ์๋ ํ์ํ ์ ๋ณด๋ง์ ๊ฐ์ ธ์ค๊ฒ ๋๋ค.
temp_html์ ์กฐ์ ์์
temp_html ์กฐ์์ ํตํด ๋ธ๋ผ์ฐ์ ์์ ๋์ด์ฃผ๋ style ์กฐ์์ ๋ฐ๊ฟ์ค ์๋ ์๋ค. ํน์ ์กฐ๊ฑด(๋ฏธ์ธ๋จผ์ง ์์น 120 ์ด์)์ ๋ฐ๋ผ class๋ฅผ ๋ค๋ฅด๊ฒ ๋ฐ๊ณ , <style>...</style>์์ ๊ฐ class์ ๋ฐ๋ฅธ style์ ์ง์ ํ๋ ๋ฐฉ์์ด๋ค.
//case1
function q1() {
$.ajax({
type: "GET",
url: "http://openapi.seoul.go.kr:8088/6d4d776b466c656533356a4b4b5872/json/RealtimeCityAir/1/99",
data: {},
success: function(response){
$('#names-q1').empty()
dic = response["RealtimeCityAir"]["row"]
for (i = 0; i < dic.length ; i++) {
gu_name = dic[i]["MSRSTE_NM"]
dust = dic[i]["IDEX_MVL"]
if (dust >= 120) {
dust_check = "dust_over";
}
else {
dust_check = "dust_lower";
}
temp_html = `<li class = ${dust_check} >${gu_name}: <span >${dust}</span></li>`
$('#names-q1').append(temp_html)
}
}
})
}
//case2
function q1() {
$.ajax({
type: "GET",
url: "http://openapi.seoul.go.kr:8088/6d4d776b466c656533356a4b4b5872/json/RealtimeCityAir/1/99",
data: {},
success: function(response){
$('#names-q1').empty()
dic = response["RealtimeCityAir"]["row"]
for (i = 0; i < dic.length ; i++) {
gu_name = dic[i]["MSRSTE_NM"]
dust = dic[i]["IDEX_MVL"]
if (dust >= 120) {
dust_check = "dust_over";
}
else {
dust_check = "dust_lower";
}
temp_html = `<li >${gu_name}: <span class = ${dust_check}>${dust}</span></li>`
$('#names-q1').append(temp_html)
}
}
})
}
Ajax, jQuery ์ฐจ์ด์ ๋ํด์
Ajax๋ ๋น๋๊ธฐ ๋ฐฉ์์ผ๋ก HTML ์ฌ๋ก๋ฉ ์์ด ์๋ฒ์์ API๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ JSONํ์์ผ๋ก ๊ฐ์ ธ์ค๋ ๊ฒ.
jQuery๋ HTML ์กฐ์์ ์ฉ์ดํ๊ฒ ํด์ฃผ๋ ๊ฒ.
MongoDB
MongoDB๋ NoSQL ํ์ ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ๋ก๊ทธ๋จ์ด๋ฉฐ, JSON๊ณผ ์ ์ฌํ ํํ๋ก DB์ ์ ๋ฆฌํ๋ค. DB Tree ๊ตฌ์กฐ๋ db > ์ปฌ๋ ์ > ๋ํ๋จผํธ ์ด๋ค.
๋ํ๋จผํธ์๋ JSON๊ณผ ์ ์ฌํ ํ์์ผ๋ก Key, Value๊ฐ ๋๋์ด์ ๊ตฌ์ฑ๋๋ค.
doc = {
'name':'์์',
'age':24
}
์ปฌ๋ ์ ์ ์ด์ ๊ฐ์ ๋ํ๋จผํธ๊ฐ ํ ๊ฐ ์ด์ ํฉ์ณ์ ธ์ ๊ตฌ์ฑ๋๋ค.
# 'users'๋ผ๋ collection์ {'name':'bobby','age':21}๋ฅผ ๋ฃ์ต๋๋ค.
db.users.insert_one({'name':'์ํฌ','age':30})
db.users.insert_one({'name':'์ฒ ์','age':20})
db.users.insert_one({'name':'john','age':30})
๋ฐ์ดํฐ๋ฅผ ๋ณผ ๋๋ ์๋์ ๊ฐ์ด ๋ณผ ์ ์๋ค.
all_users = list(db.users.find({},{'_id':False}))
print(all_users[0]) # 0๋ฒ์งธ ๊ฒฐ๊ณผ๊ฐ์ ๋ณด๊ธฐ
print(all_users[0]['name']) # 0๋ฒ์งธ ๊ฒฐ๊ณผ๊ฐ์ 'name'์ ๋ณด๊ธฐ
for a in all_users: # ๋ฐ๋ณต๋ฌธ์ ๋๋ฉฐ ๋ชจ๋ ๊ฒฐ๊ณผ๊ฐ์ ๋ณด๊ธฐ
print(a)
'๐งญ KAIST JUNGLE' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๐งญ๏ธJungle] Own project(Finished)- TIL (0) | 2024.01.15 |
---|---|
[๐งญ๏ธJungle] Own project - TIL (0) | 2024.01.14 |
[๐งญ๏ธJungle] Web Scraper, Flask, Own project - TIL (1) | 2024.01.13 |
[๐งญ๏ธJungle] Bootstrap, JS - TIL (0) | 2024.01.10 |
[๐งญ๏ธJungle] HTML, CSS - TIL (0) | 2024.01.08 |