質問編集履歴

1

変更

2019/02/27 08:34

投稿

Chandler_Bing
Chandler_Bing

スコア673

test CHANGED
File without changes
test CHANGED
@@ -11,3 +11,441 @@
11
11
 
12
12
 
13
13
  ちなみにAjaxの処理自体は成功しています。
14
+
15
+
16
+
17
+ これがコードです。
18
+
19
+
20
+
21
+ ```JS
22
+
23
+ $(function() {
24
+
25
+ var array = [];
26
+
27
+ var hotel = null;
28
+
29
+ var process = null;
30
+
31
+ var top = 0;
32
+
33
+ var normaltop = 0;
34
+
35
+ var sorttop = 0;
36
+
37
+ var normalFlag = false;
38
+
39
+ var sortflag = false;
40
+
41
+
42
+
43
+ function hotelClone() {
44
+
45
+ var hotel = $("<div>", {
46
+
47
+ "class": "hotel"
48
+
49
+ });
50
+
51
+ var hotelpics = $("<div>", {
52
+
53
+ "class": "hotelpics"
54
+
55
+ });
56
+
57
+ var mainpic = $("<div>", {
58
+
59
+ "class": "mainpic"
60
+
61
+ });
62
+
63
+ var others = $("<div>", {
64
+
65
+ "class": "others"
66
+
67
+ });
68
+
69
+ var hotelinfo = $("<div>", {
70
+
71
+ "class": "hotelinfo"
72
+
73
+ });
74
+
75
+ var hotelname = $("<a>", {
76
+
77
+ "class": "hotelname"
78
+
79
+ });
80
+
81
+ var tags = $("<div>", {
82
+
83
+ "class": "tags"
84
+
85
+ });
86
+
87
+ var rating = $("<div>", {
88
+
89
+ "class": "rating"
90
+
91
+ });
92
+
93
+ var rateposition = $("<span>", {
94
+
95
+ "class": "rateposition"
96
+
97
+ });
98
+
99
+ var price = $("<div>", {
100
+
101
+ "class": "price"
102
+
103
+ });
104
+
105
+ var clear = $("<div>", {
106
+
107
+ "class": "clear"
108
+
109
+ });
110
+
111
+
112
+
113
+ hotelpics.append(mainpic, others);
114
+
115
+ rating.append(rateposition);
116
+
117
+ hotelinfo.append(hotelname, tags, rating, price);
118
+
119
+ result = hotel.append(hotelpics, hotelinfo, clear);
120
+
121
+ return result;
122
+
123
+ };
124
+
125
+
126
+
127
+ function getHotels(process, top) {
128
+
129
+ return $.post('ajax.php', {
130
+
131
+ word: word,
132
+
133
+ kind: 'hotels',
134
+
135
+ process: process,
136
+
137
+ top: top
138
+
139
+ });
140
+
141
+ };
142
+
143
+
144
+
145
+ function getPics() {
146
+
147
+ return $.post('ajax.php', {
148
+
149
+ word: word,
150
+
151
+ kind: 'pics'
152
+
153
+ });
154
+
155
+ };
156
+
157
+
158
+
159
+ function getTags() {
160
+
161
+ return $.post('ajax.php', {
162
+
163
+ word: word,
164
+
165
+ kind: 'tags'
166
+
167
+ });
168
+
169
+ };
170
+
171
+
172
+
173
+ function getRates() {
174
+
175
+ return $.post('ajax.php', {
176
+
177
+ word: word,
178
+
179
+ kind: 'rate'
180
+
181
+ });
182
+
183
+ };
184
+
185
+
186
+
187
+ function displayHotels(process) {
188
+
189
+ // if (process === 'normal') {
190
+
191
+ // normalFlag = true;
192
+
193
+ // sortflag = false;
194
+
195
+ // top = normaltop;
196
+
197
+ // } else if (process === 'sortbyprice') {
198
+
199
+ // sortflag = true;
200
+
201
+ // normalFlag = false;
202
+
203
+ // top = sorttop;
204
+
205
+ // }
206
+
207
+ getHotels(process, 0).done(function(result) {
208
+
209
+ hotels = result;
210
+
211
+ console.log(hotels)
212
+
213
+ // if (hotels.length > 0) {
214
+
215
+ // if (normalFlag === true) {
216
+
217
+ // normaltop = normaltop + hotels.length;
218
+
219
+ // } else if (sortflag === true) {
220
+
221
+ // sorttop = sorttop + hotels.length;
222
+
223
+ // }
224
+
225
+
226
+
227
+ getPics().done(function(result) {
228
+
229
+ hotelpics = result;
230
+
231
+
232
+
233
+ getTags().done(function(result) {
234
+
235
+ hoteltags = result;
236
+
237
+
238
+
239
+ getRates().done(function(result) {
240
+
241
+ hotelrates = result;
242
+
243
+
244
+
245
+ var hotelsClone = []; //これから表示するホテルが入る
246
+
247
+ var hotelName = null;
248
+
249
+ var href = null;
250
+
251
+ var hotelMainPic = null;
252
+
253
+ var others1 = null;
254
+
255
+ var others2 = null;
256
+
257
+ var others3 = null;
258
+
259
+ var tags = [];
260
+
261
+ var rateWord = null;
262
+
263
+ var rate = null;
264
+
265
+ var price = null;
266
+
267
+
268
+
269
+ $.each(hotels, function(hotelindx, hotel) {
270
+
271
+ hotelsClone.push(hotelClone());
272
+
273
+ hotelName = hotel.hotel_name;
274
+
275
+ href = 'about.php?hotelid=' + hotel.hotel_id + '&countrycode=' + hotel.country_code;
276
+
277
+ hotelsClone[hotelindx].find('.hotelname').append(hotelName);
278
+
279
+ hotelsClone[hotelindx].find('.hotelname').attr('href', href);
280
+
281
+
282
+
283
+ tags = [];
284
+
285
+ $.each(hoteltags, function(index, hoteltag) {
286
+
287
+ if (hotel.hotel_id === hoteltag.hotel_id && hotel.country_code === hoteltag.country_code) {
288
+
289
+ tags.push(hoteltag.tag);
290
+
291
+ }
292
+
293
+ });
294
+
295
+
296
+
297
+ for (var i = 0; i < tags.length; i++) {
298
+
299
+ hotelsClone[hotelindx].find('.tags').append('<span>' + tags[i]);
300
+
301
+ }
302
+
303
+
304
+
305
+ price = hotel.price + '/1泊(1人)';
306
+
307
+ $.each(hotelrates, function(index, hotelrate) {
308
+
309
+ if (hotel.hotel_id === hotelrate.hotel_id && hotel.country_code === hotelrate.country_code) {
310
+
311
+ rateWord = hotelrate.ratingresult
312
+
313
+ rate = hotelrate.rate;
314
+
315
+ hotelsClone[hotelindx].find('.rating .rateposition').append('<span>' + rateWord);
316
+
317
+ hotelsClone[hotelindx].find('.rating .rateposition').append('<span class="rate">' + rate);
318
+
319
+ }
320
+
321
+ });
322
+
323
+ hotelsClone[hotelindx].find('.price').append('<span>' + price);
324
+
325
+
326
+
327
+ $.each(hotelpics, function(index, hotelpic) {
328
+
329
+ if (hotel.hotel_id === hotelpic.hotel_id && hotel.country_code === hotelpic.country_code) {
330
+
331
+ hotelMainPic = '<img' + ' src' + '="' + 'imgs/' + hotelpic.main_pic + '">';
332
+
333
+ others1 = '<img' + ' src' + '="' + 'imgs/' + hotelpic.others1 + '">';
334
+
335
+ others2 = '<img' + ' src' + '="' + 'imgs/' + hotelpic.others2 + '">';
336
+
337
+ others3 = '<img' + ' src' + '="' + 'imgs/' + hotelpic.others3 + '">';
338
+
339
+ hotelsClone[hotelindx].find('.mainpic').append(hotelMainPic);
340
+
341
+ hotelsClone[hotelindx].find('.others').append(others1);
342
+
343
+ hotelsClone[hotelindx].find('.others').append(others2);
344
+
345
+ hotelsClone[hotelindx].find('.others').append(others3);
346
+
347
+ }
348
+
349
+ });
350
+
351
+ hotelsClone[hotelindx].find('.mainpic img').height(300).width(300);
352
+
353
+ hotelsClone[hotelindx].find('.others img').height(85).width(85);
354
+
355
+ });
356
+
357
+ for (var i = 0; i < hotelsClone.length; i++) {
358
+
359
+ $('.mainrequirements').after(hotelsClone[i]);
360
+
361
+ }
362
+
363
+ hotelsClone = [];
364
+
365
+ top = 0; //念の為初期化
366
+
367
+ });
368
+
369
+ });
370
+
371
+ });
372
+
373
+ // }
374
+
375
+ });
376
+
377
+ }
378
+
379
+
380
+
381
+ //最初の処理
382
+
383
+ displayHotels('normal');
384
+
385
+
386
+
387
+
388
+
389
+ //ここからクリック処理
390
+
391
+ $('#sortbyprice').on('click', function() {
392
+
393
+ if (!$(this).hasClass('selected')) {
394
+
395
+ $('mainrequirement').removeClass('selected');
396
+
397
+ $(this).addClass('selected');
398
+
399
+ process = $(this).attr('id');
400
+
401
+ $('.hotel').remove();
402
+
403
+ displayHotels(process);
404
+
405
+ return false;
406
+
407
+ }
408
+
409
+ });
410
+
411
+
412
+
413
+ //ここからスクロール処理
414
+
415
+ $(window).on('scroll', function() {
416
+
417
+ if ((normaltop + sorttop) % 5 === 0) {
418
+
419
+ var doch = $(document).innerHeight(); //ページ全体の高さ
420
+
421
+ var winh = $(window).innerHeight(); //ウィンドウの高さ
422
+
423
+ var bottom = doch - winh; //ページ全体の高さ - ウィンドウの高さ = ページの最下部位置
424
+
425
+
426
+
427
+ if (bottom <= $(window).scrollTop()) {
428
+
429
+ //一番下までスクロールした時に実行
430
+
431
+ if (normalFlag === true) {
432
+
433
+ displayHotels('normal');
434
+
435
+ } else if (sortflag === true) {
436
+
437
+ displayHotels('sortbyprice');
438
+
439
+ }
440
+
441
+ }
442
+
443
+ }
444
+
445
+ });
446
+
447
+ });
448
+
449
+
450
+
451
+ ```