(3-1)動作拆解-品牌受眾
分為資料渲染與觸發方法兩部分說明。
1.下拉選項內容
使用 v-for="item in restaurants" 對 restaurants.type 品牌受眾進行多筆資料渲染。延伸思考:當 restaurants.type 值有重覆時要怎麼不重覆
2.當欄位「品牌受眾」被操作時
<select></select> 加上 v-model="tempRes.selectType" 將會員所選的項目內容為值去變更 data 裡的屬性 tempRes.selectType 。<template>
  ...
  <form>
  ...
  <label for="type" class="col-sm-3 col-form-label">品牌受眾</label>
  <select
    name="品牌受眾"
    id="type"
    class="form-select col-sm"
    @change="selectTypeChange"
    v-model="tempRes.selectType">
    <option value="" disabled selected>請選擇品牌受眾</option>
    <option value="全部受眾">全部受眾</option>
    <option :value="item.type" v-for="item in restaurants" v-bind:key="item.id">
      {{ type }}
    </option>
  </select>
  ...
  </form>
  ...
</template>
<script>
...
export default {
  data() {
    return {
      restaurants: [],
      filterRes: {
        filterBrandName: [],
        filterType: [],
        filterAddress: [],
      },
      tempRes: {
        selectBrandName: "",
        selectType: "",
      }
    };
  }
...
  methods: {
    selectTypeChange() {
      ...
      this.filterRes.filterType = this.tempRes.selectType;
      if (this.tempRes.selectType !== "全部受眾") {
        // 會員選擇受眾時,依照所選受眾去restaurants找相應的品牌名稱
        this.filterRes.filterBrandName = this.restaurants
          .filter((item) => item.type === this.tempRes.selectType)
          .map((item) => item.brandName);
      } else {
        // 會員選擇全部受眾時,列出restaurants全部品牌名稱
        this.filterRes.filterBrandName = this.restaurants.map(
          (item) => item.brandName);
      }
      // 當會員回頭更改選品牌受眾。請讓其他兩個欄位重置成預設值
      this.tempRes.selectBrandName = "";
      this.filterRes.filterAddress = [];
    },
  },
};
...
</script>