cz_filter.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // 選擇風格處理
  2. function change_style() {
  3. style = this.value;
  4. // 更新畫面資料
  5. update_info(style, roomsize, budget);
  6. }
  7. // 選擇坪數處理
  8. function change_roomsize() {
  9. roomsize = this.value;
  10. // 更新畫面資料
  11. update_info(style, roomsize, budget);
  12. }
  13. // 選擇預算處理
  14. function change_budget() {
  15. budget = this.value;
  16. // 更新畫面資料
  17. update_info(style, roomsize, budget);
  18. }
  19. // 更新畫面資料
  20. function update_info(style, roomsize, budget) {
  21. var items = []; // 作品集資料清單
  22. // 讀取作品集資料(json)
  23. fetch('http://127.0.0.1:1313/data/test.json')
  24. .then(response => response.json())
  25. .then(data => {
  26. console.log('讀取作品集資料(json)....');
  27. console.log(data['items']);
  28. items = data['items'];
  29. console.log('載入符合篩選條件(' + style + ',' + roomsize + ',' + budget + ')的作品...........');
  30. var content = "";
  31. for(var i = 0; i < items.length; i++) {
  32. var style_check = (style == '風格不限') ? true : (style == items[i]['style']);
  33. var roomsize_check = (roomsize == '坪數不限') ? true : (roomsize == items[i]['roomsize']);
  34. var budget_check = (budget == '預算不限') ? true : (budget == items[i]['budget']);
  35. if (style_check && roomsize_check && budget_check) {
  36. console.log('作品' + i + ',符合篩選條件...');
  37. content += ' \
  38. <div class="col"> \
  39. <div class="card h-100"> \
  40. <a href="' + items[i]['url'] + '" target="_blank"> \
  41. <amp-img \
  42. class="card-img-top" \
  43. alt="' + items[i]['title'] + '" \
  44. src="' + items[i]['image'] + '" \
  45. height="533" \
  46. width="800" \
  47. layout="responsive"> \
  48. </amp-img> \
  49. </a> \
  50. <div class="card-body"> \
  51. <a href="' + items[i]['url'] + '" target="_blank"> \
  52. <h3 class="card-title"><b>' + items[i]['title'] + '</b></h3> \
  53. </a> \
  54. <p class="card-text">' + items[i]['description'] + '</p> \
  55. </div> \
  56. </div> \
  57. </div>';
  58. } else {
  59. console.log('作品' + i + ',不符合篩選條件...');
  60. }
  61. console.log(items[i]);
  62. }
  63. // 載入符合篩選條件的作品
  64. var update_info = document.querySelectorAll('.update_info');
  65. // 更新畫面
  66. for(var i = 0; i < update_info.length; i++) {
  67. update_info[i].innerHTML = content;
  68. }
  69. }).catch((error) => {
  70. console.log('error');
  71. console.log('error',error);
  72. console.error('Error:', error);
  73. });
  74. }
  75. // 預設選項值
  76. var style = "風格不限"
  77. var roomsize = "坪數不限"
  78. var budget = "預算不限"
  79. // 風格
  80. var items = document.getElementsByClassName('style');
  81. for(var i = 0; i < items.length; i++) {
  82. console.log('選擇風格..........');
  83. items[i].addEventListener("change", change_style);
  84. }
  85. // 坪數
  86. var items = document.getElementsByClassName('roomsize');
  87. for(var i = 0; i < items.length; i++) {
  88. console.log('選擇坪數..........');
  89. items[i].addEventListener("change", change_roomsize);
  90. }
  91. // 預算
  92. var items = document.getElementsByClassName('budget');
  93. for(var i = 0; i < items.length; i++) {
  94. console.log('選擇預算..........');
  95. items[i].addEventListener("change", change_budget);
  96. }