-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchDog1.html
139 lines (117 loc) · 3.45 KB
/
searchDog1.html
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<!DOCTYPE html>
<html lang="en">
<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">
<link rel="stylesheet" href="./css/searchDog.css" />
<script src="./searchDog.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<title>Document</title>
</head>
<body>
<div id="banner-message">
<p>견종별 정보</p>
<select class="breed_select">
<option></option>
</select>
</div>
<div id="breed_data">
<img id="breed_image" src="" />
<p>
정보
</p>
<table id="breed_data_table">
</table>
</div>
</body>
<script>
var breeds;
$('#breed_search').on('input', function(e) {
var search_str = $(this).val();
searchBreeds(search_str);
});
function searchBreeds(search_str) {
var string_length = search_str.length;
search_str = search_str.toLowerCase();
for (var i = 0; i < breeds.length; i++)
{
var breed_name_snippet = breeds[i].name.substr(0, string_length).toLowerCase();
if (breed_name_snippet == search_str) {
getDogByBreed(breeds[i].id);
return;
}
}
}
// Setup the Controls
var $breed_select = $('select.breed_select');
$breed_select.change(function() {
var id = $(this).children(":selected").attr("id");
getDogByBreed(id);
});
// 견종 부르기
function getBreeds() {
ajax_get('https://api.thedogapi.com/v1/breeds?api_key=828cc0e2-6c72-4f57-be60-4a34272b5453', function(data) {
populateBreedsSelect(data);
breeds = data;
});
}
// 견종 셀렉트에 넣기
function populateBreedsSelect(breeds) {
$breed_select.empty().append(function() {
var output = '';
$.each(breeds, function(key, value) {
output += '<option id="' + value.id + '">' + value.name + '</option>';
});
return output;
});
}
// 셀렉트 선택하면 바꾸기
function getDogByBreed(breed_id) {
//id랑 비교해서 이미지 찾기
ajax_get('https://api.thedogapi.com/v1/images/search?include_breed=1&breed_id=' + breed_id, function(data) {
if (data.length == 0) {
//이미지 없으면
clearBreed();
$("#breed_data_table").append("<tr><td>Sorry, no Image for that breed yet</td></tr>");
} else {
//이미지 있으면 반환
displayBreed(data[0]);
}
});
}
//테이블 클리어
function clearBreed() {
$('#breed_image').attr('src', "");
$("#breed_data_table tr").remove();
}
//이미지, 데이터 디스플레이
function displayBreed(image) {
$('#breed_image').attr('src', image.url);
$("#breed_data_table tr").remove();
var breed_data = image.breeds[0];
$.each(breed_data, function(key, value) {
if (key == 'weight' || key == 'height') value = value.metric;
$("#breed_data_table").append("<tr><td>" + key + "</td><td>" + value + "</td></tr>");
});
}
function ajax_get(url, callback) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log('responseText:' + xmlhttp.responseText);
try {
var data = JSON.parse(xmlhttp.responseText);
} catch (err) {
console.log(err.message + " in " + xmlhttp.responseText);
return;
}
callback(data);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
getBreeds();
</script>
</html>