質問編集履歴

1

分からない部分を詳しく記載してみました。

2019/05/22 07:09

投稿

biginnnerrrr
biginnnerrrr

スコア17

test CHANGED
File without changes
test CHANGED
@@ -1,8 +1,26 @@
1
+ ### 前提・実現したいこと
2
+
3
+ 検索し、そのまま再び検索ボタンを押すと次の20件の商品を上に積み上げていく形で表示させたいのです。
4
+
5
+
6
+
7
+
8
+
1
- ajaxを使っ楽天BOOKSの検索ページを作っています。
9
+ ### 発生しいる問題・エラメッセー
10
+
11
+
12
+
2
-
13
+ ```
14
+
15
+ $('.lists').prepend(template);
16
+
3
- 検索した際に本のイメジを出したいのですが、何をすればいいかわかりません。
17
+ 検索した際に1〜20件、同じワドで検索した際に21〜40件目が上に表示されるようにしたいのですが
4
-
18
+
5
- 初心者為、教え頂けると幸いです
19
+ 20〜1件、そ上に40〜21件目が表示されしまう
20
+
21
+ ```
22
+
23
+
6
24
 
7
25
  ### 該当のソースコード
8
26
 
@@ -68,88 +86,374 @@
68
86
 
69
87
 
70
88
 
71
-
72
-
73
89
  $(function() {
74
90
 
91
+ //グローバル変数
92
+
93
+ var pageNum = 0;
94
+
95
+ var searchword = '';
96
+
97
+ var prevword = '';//前のワードの空の変数
98
+
75
- $('#js-search-button').on('click', function() {
99
+ $('#js-search-button').on('click', function() {//クリックされた時に実行される
76
-
100
+
77
- var searchword = searchButton();
101
+ searchword = searchButton();
102
+
103
+ pageNum = countUp();
104
+
105
+ // console.log(pageNums);
78
106
 
79
107
  console.log(searchword);
80
108
 
109
+ console.log(prevword);
110
+
111
+ execAjax();//execAjaxの実行
112
+
113
+
114
+
115
+ function countUp() {
116
+
117
+ pageNum = pageNum + 1;
118
+
119
+ return pageNum;//+になった値が上に返る
120
+
121
+ }
122
+
123
+
124
+
125
+ function searchButton() {
126
+
127
+ // '#js-search-button'がクリックされたとき,検索した名前の取得の処理がここに書かれている
128
+
129
+ var search = $('#js-search-word').val();
130
+
131
+ // console.log(searchword);
132
+
133
+ return search
134
+
135
+ }
136
+
137
+
138
+
139
+ if(prevword !== searchword) {//一致しなかった場合の処理
140
+
141
+ $('ul').empty(); // 違うワードが検索された時にulの中身を空にする
142
+
143
+ pageNum = 1;
144
+
145
+ prevword = searchword;//検索された前の値を取得
146
+
147
+ }
148
+
149
+ });
150
+
151
+
152
+
153
+ function execAjax () {//検索したワードに関連した本のデータを取得する処理
154
+
81
- execAjax(searchword);
155
+ console.log(searchword);
156
+
157
+ console.log(pageNum);
158
+
159
+ $.ajax({
160
+
161
+ url: 'https://app.rakuten.co.jp/services/api/BooksTotal/Search/20130522',//楽天APIにアクセス
162
+
163
+ type: 'GET',//取得
164
+
165
+ datatype: 'json',//形式
166
+
167
+ data: {
168
+
169
+ // 「区分:サービス固有パラメーター」を確認して、必要な情報をdata内に入れましょう。
170
+
171
+ applicationId: '',
172
+
173
+ booksGenreId: '001',
174
+
175
+ hits: 20,
176
+
177
+ page: pageNum,//変数になる
178
+
179
+ keyword: searchword
180
+
181
+ },
182
+
183
+ }).done(function(data) {
184
+
185
+ console.log(data.Items);
186
+
187
+ console.log(data.page);
188
+
189
+ $.each(data.Items, function(i,item) {
190
+
191
+ var template = '<li class='+'lists__item'+'>'+
192
+
193
+ '<div class='+'lists__item__inner'+'>'+
194
+
195
+ '<a href='+' class='+'lists__item__link'+' target='+'_blank'+'>'+
196
+
197
+ '<img src='+ item.Item.mediumImageUrl +' class='+'lists__item__img'+' alt='+'>'+
198
+
199
+ '<p class='+'lists__item__detail'+'>作品名:'+ data.Items[i].Item.title +'</p>'+
200
+
201
+ '<p class='+'lists__item__detail'+'>作者 :'+ data.Items[i].Item.author +'</p>'+
202
+
203
+ '<p class='+'lists__item__detail'+'>出版社:'+ data.Items[i].Item.publisherName +'</p>'+
204
+
205
+ '</a>'+'</div>'+'</li>';
206
+
207
+ $('.lists').prepend(template);//prepend先頭へ挿入する
82
208
 
83
209
  });
84
210
 
85
- });
86
-
87
-
88
-
89
- function searchButton() {
90
-
91
- var search = $('#js-search-word').val();
92
-
93
- // console.log(searchword);
94
-
95
- return search
211
+ });
212
+
213
+ }
214
+
215
+ });
216
+
217
+ </script>
218
+
219
+ </body>
220
+
221
+ </html>
222
+
223
+ ```
224
+
225
+
226
+
227
+ ### 試したこと
228
+
229
+
230
+
231
+ もともとはapend()を使っていましたが、検索した情報を上に積みかさねたいのでprepend()を使いました。
232
+
233
+ 結果上に積み重なったのですが、1〜20件ではなく20〜1件の順に表示されてしまいます。
234
+
235
+ 初心者なので間違えているところが多いと思いますが、ヒントなど頂けるとありがたいです。
236
+
237
+ ### 補足情報(CSS)
238
+
239
+ ```ここに言語を入力
240
+
241
+ .clearfix:after {
242
+
243
+ display: block;
244
+
245
+ clear: both;
246
+
247
+ content: '';
248
+
249
+ }
250
+
251
+ .wrapper {
252
+
253
+ padding: 20px;
254
+
255
+ }
256
+
257
+ .container {
258
+
259
+ }
260
+
261
+ .header {
262
+
263
+ width: 100%;
264
+
265
+ background-color: #43cee0;
266
+
267
+ }
268
+
269
+ .header__title {
270
+
271
+ line-height: 60px;
272
+
273
+ text-align: center;
274
+
275
+ font-size: 20px;
276
+
277
+ color: #fff;
278
+
279
+ }
280
+
281
+ .search {
282
+
283
+ overflow: hidden;
284
+
285
+ margin-bottom: 50px;
286
+
287
+ background: #fff;
288
+
289
+ box-shadow: 0 1px 5px #ccc;
290
+
291
+ }
292
+
293
+ .search__text {
294
+
295
+ width: 100%;
296
+
297
+ }
298
+
299
+ .search__text__input {
300
+
301
+ -webkit-box-sizing: border-box;
302
+
303
+ box-sizing: border-box;
304
+
305
+ width: 100%;
306
+
307
+ margin-bottom: 20px;
308
+
309
+ padding: 0 10px;
310
+
311
+ line-height: 3em;
312
+
313
+ border-top: none;
314
+
315
+ border-left: none;
316
+
317
+ border-right: none;
318
+
319
+ border-bottom: 1px solid #eee;
320
+
321
+ font-size: 20px;
322
+
323
+ }
324
+
325
+ .search__btn {
326
+
327
+ display: block;
328
+
329
+ margin: 0 auto 20px;
330
+
331
+ padding: 0 20px;
332
+
333
+ line-height: 40px;
334
+
335
+ border: none;
336
+
337
+ font-size: 18px;
338
+
339
+ color: #fff;
340
+
341
+ background-color: #43cee0;
342
+
343
+ }
344
+
345
+ .search__btn:hover {
346
+
347
+ background-color: #1eabbd;
348
+
349
+ }
350
+
351
+ .lists {
352
+
353
+ overflow: hidden;
354
+
355
+ margin-left: 20px;
356
+
357
+
358
+
359
+ }
360
+
361
+ .lists__item {
362
+
363
+ -webkit-box-sizing: border-box;
364
+
365
+ box-sizing: border-box;
366
+
367
+ display: inline-block;
368
+
369
+ width: 25%;
370
+
371
+ padding: 0 20px 20px 0;
372
+
373
+ vertical-align: top;
374
+
375
+ text-align: center;
376
+
377
+ }
378
+
379
+ .lists__item__inner {
380
+
381
+ width: 100%;
382
+
383
+ max-width: 200px;
384
+
385
+ margin: 0 auto;
386
+
387
+ }
388
+
389
+ .lists__item__link {
390
+
391
+ display: block;
392
+
393
+ text-decoration: none;
394
+
395
+ }
396
+
397
+ .lists__item__img {
398
+
399
+ margin-bottom: 20px;
400
+
401
+ width: 100%;
402
+
403
+ max-width: 150px;
404
+
405
+ -webkit-box-shadow: 0 1px 5px #ccc;
406
+
407
+ box-shadow: 0 1px 5px #ccc;
408
+
409
+ }
410
+
411
+ .lists__item__detail {
412
+
413
+ margin-bottom: 10px;
414
+
415
+ padding-left: 5em;
416
+
417
+ text-indent: -5em;
418
+
419
+ line-height: 1.5em;
420
+
421
+ text-align: left;
422
+
423
+ font-size: 12px;
424
+
425
+ }
426
+
427
+ .message {
428
+
429
+ margin-bottom: 50px;
430
+
431
+ text-align: center;
432
+
433
+ }
434
+
435
+
436
+
437
+ @media screen and (max-width: 767px) {
438
+
439
+ .lists__item {
440
+
441
+ width: 33.33%;
96
442
 
97
443
  }
98
444
 
99
- function execAjax (searchword)
100
-
101
- console.log(searchword);
102
-
103
- $.ajax({
104
-
105
- url: 'https://app.rakuten.co.jp/services/api/BooksTotal/Search/20130522',
106
-
107
- type: 'GET',
108
-
109
- datatype: 'json',
110
-
111
- data: {
112
-
113
- applicationId: '',
114
-
115
- booksGenreId: '001',
116
-
117
- hits: 20,
118
-
119
- page: 1,
120
-
121
- keyword: searchword
122
-
123
- },
124
-
125
- }).done(function(data) {
126
-
127
- console.log(data);
128
-
129
- var template = '<li class='+'lists__item'+'>'+
130
-
131
- '<div class='+'lists__item__inner'+'>'+
132
-
133
- '<a href='+' class='+'lists__item__link'+' target='+'_blank'+'>'+
134
-
135
- '<img src='+' class='+'lists__item__img'+' alt='+'>'+
136
-
137
- '<p class='+'lists__item__detail'+'>作品名:</p>'+
138
-
139
- '<p class='+'lists__item__detail'+'>作者 :</p>'+
140
-
141
- '<p class='+'lists__item__detail'+'>出版社:</p>'+
142
-
143
- '</a>'+'</div>'+'</li>';
144
-
145
- $('.lists').append(template);
146
-
147
- console.log(template);
148
-
149
- });
150
-
151
- }
445
+ }
446
+
447
+
448
+
152
-
449
+ @media screen and (max-width: 479px) {
450
+
153
- </script>
451
+ .lists__item {
452
+
453
+ width: 50%;
454
+
455
+ }
456
+
457
+ }
154
458
 
155
459
  ```