cz_filter.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // 選擇屋型處理
  2. function change_type() {
  3. type = this.value;
  4. // 更新畫面資料
  5. update_info(type, ping, budget, rooms);
  6. }
  7. // 選擇坪數處理
  8. function change_ping() {
  9. ping = this.value;
  10. // 更新畫面資料
  11. update_info(type, ping, budget, rooms);
  12. }
  13. // 選擇預算處理
  14. function change_budget() {
  15. budget = this.value;
  16. // 更新畫面資料
  17. update_info(type, ping, budget, rooms);
  18. }
  19. // 選擇格局處理
  20. function change_rooms() {
  21. rooms = this.value;
  22. // 更新畫面資料
  23. update_info(type, ping, budget, rooms);
  24. }
  25. // 更新畫面資料
  26. function update_info(type, ping, budget, room) {
  27. console.log(type + ',' + ping + ',' + budget + ',' + room); // test (大樓,全部坪數,全部預算,全部格局)
  28. var items = []; // 作品集資料清單
  29. // 讀取作品集資料(json)
  30. fetch('https://bhouse.ptt.cx/api/contents?url=/collection')
  31. .then(response => response.json())
  32. .then(items => {
  33. console.log('讀取作品集資料(json)....');
  34. var content = "";
  35. console.log('載入符合篩選條件(' + type + ',' + ping + ',' + budget + ',' + rooms + ')的作品...........');
  36. var filter_num = 0; // 篩選條件數量
  37. if (!type.includes("全部")) {
  38. filter_num += 1;
  39. }
  40. if (!ping.includes("全部")) {
  41. filter_num += 1;
  42. }
  43. if (!budget.includes("全部")) {
  44. filter_num += 1;
  45. }
  46. if (!rooms.includes("全部")) {
  47. filter_num += 1;
  48. }
  49. console.log('篩選條件數: ' + filter_num); // test
  50. for (var i = 0; i < items.length; i++) {
  51. if (items[i]['url'] && items[i]['url'] != '/collection') {
  52. var tags = items[i]['tags'].replace('"', ''); // 各作品tag
  53. var matched_num = 0; // 符合篩選條件數量
  54. if (tags.includes(type)) {
  55. matched_num += 1;
  56. }
  57. if (tags.includes(ping)) {
  58. matched_num += 1;
  59. }
  60. if (tags.includes(budget)) {
  61. matched_num += 1;
  62. }
  63. if (tags.includes(rooms)) {
  64. matched_num += 1;
  65. }
  66. console.log('符合篩選條件數: ' + matched_num); // test
  67. if (matched_num == 4) {
  68. content += ' \
  69. <div class="col"> \
  70. <div class="card h-100"> \
  71. <a class="link-collection" href="' + items[i]['url'] + '"> \
  72. <amp-img \
  73. alt="' + items[i]['title'] + '" \
  74. src="' + items[i]['image'] + '" \
  75. height="1" \
  76. width="1.4" \
  77. layout="responsive"> \
  78. </amp-img> \
  79. </a> \
  80. <div class="card-body"> \
  81. <a class="link-collection" href="' + items[i]['url'] + '"> \
  82. <div class="title-size"><b>' + items[i]['title'] + '</b></div> \
  83. </a> \
  84. <p class="card-text" style="color:#4D4D4D;">' + items[i]['description'] + '</p> \
  85. </div> \
  86. </div> \
  87. </div>';
  88. }
  89. }
  90. }
  91. // 載入符合篩選條件的作品
  92. var update_info = document.querySelectorAll('.update_info');
  93. if (content) {
  94. // 查無資料畫面
  95. console.log('有資料'); // test
  96. } else {
  97. console.log('無資料'); // test
  98. content += ' \
  99. <div class="card bg-primary text-white text-center"> \
  100. <div class="card-body p-5"> \
  101. <div class="mb-3"> \
  102. <b>找不到符合條件的作品</b> \
  103. </div> \
  104. <div class="mb-3"> \
  105. ' + type + ' \
  106. </div> \
  107. <div class="mb-3"> \
  108. ' + ping + ' \
  109. </div> \
  110. <div class="mb-3"> \
  111. ' + budget + ' \
  112. </div> \
  113. <div class="mb-3"> \
  114. ' + rooms + ' \
  115. </div> \
  116. <div> \
  117. <a href="/collection" class="btn btn-primary">重新查詢</a> \
  118. </div> \
  119. </div> \
  120. </div>';
  121. }
  122. // 更新畫面
  123. for (var i = 0; i < update_info.length; i++) {
  124. update_info[i].innerHTML = content;
  125. }
  126. }).catch((error) => {
  127. console.error('Error:', error);
  128. });
  129. }
  130. // 預設選項值
  131. var type = "全部類型";
  132. var ping = "全部坪數";
  133. var budget = "全部預算";
  134. var rooms = "全部格局";
  135. // 類型
  136. var items = document.getElementsByClassName('type');
  137. for (var i = 0; i < items.length; i++) {
  138. console.log('選擇類型..........');
  139. items[i].addEventListener("change", change_type);
  140. }
  141. // 坪數
  142. var items = document.getElementsByClassName('ping');
  143. for (var i = 0; i < items.length; i++) {
  144. console.log('選擇坪數..........');
  145. items[i].addEventListener("change", change_ping);
  146. }
  147. // 預算
  148. var items = document.getElementsByClassName('budget');
  149. for (var i = 0; i < items.length; i++) {
  150. console.log('選擇預算..........');
  151. items[i].addEventListener("change", change_budget);
  152. }
  153. // 格局
  154. var items = document.getElementsByClassName('rooms');
  155. for (var i = 0; i < items.length; i++) {
  156. console.log('選擇格局..........');
  157. items[i].addEventListener("change", change_rooms);
  158. }