亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Vue,html 表的多重過濾器

Vue,html 表的多重過濾器

LEATH 2021-10-07 10:30:48
我正在使用 vue 從 axios 調用中獲取返回的對象并用它填充一個 html 表。我有我想要的表格,但我想知道是否有辦法(不想將整個事情轉換為數據表)使每個標題成為表格的過濾器,以便可以過濾整個表格的多個列. 基本上,假設行具有“資源”列的“卡車”、“拖車”和“容器”等項目。我正在考慮在該列的標題上設置一個下拉過濾器,該過濾器將顯示所有資源的行,或者我可以選擇“卡車”,以便在表格中只顯示帶有“卡車”的行。那有意義嗎?Vue 是否有一種固有的方法可以做到這一點?<table style="width:100%; text-align:center;"><thead>    <tr>        <th>Title</th>        <th>Resource</th>        <th>Location</th>        <th>Status</th>    </tr></thead><tbody  v-for="dateEvent in dateEvents">    <tr>        <td v-if="dateEvent.id === '2'">{{ dateEvent.title }}</td>        <td v-if="dateEvent.id === '2'">{{ dateEvent.resource }}</td>        <td v-if="dateEvent.id === '2'">{{ dateEvent.location }}</td>        <td v-if="dateEvent.id === '2'">{{ dateEvent.status }}</td>    </tr>  </tbody></table>data () {return {    dateEvents: []},created() {    this.fetchItems();},methods: {    fetchItems() {        axios.get('/home/resource_items')        .then(response => {          // handle success          console.log(response.data)          this.dateEvents = response.data        })        .catch(function(error) {          console.log(error)        })        .finally(function() {})    } }
查看完整描述

1 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

您可以使用computed功能自動計算:

  • dateEvents顯示在表格中的過濾數組

  • titles要在過濾器中顯示的數組

  • resources要在過濾器中顯示的數組

  • locations要在過濾器中顯示的數組

  • statuses要在過濾器中顯示的數組

下面是一個例子:

...


<select v-model="filters.title">

  <option v-for="title in titles" :value="title">{{ title }}</option>

</select>


<select v-model="filters.resource">

  <option v-for="resource in resources" :value="resource">{{ resource }}</option>

</select>


<select v-model="filters.location">

  <option v-for="location in locations" :value="location">{{ location }}</option>

</select>


<select v-model="filters.status">

  <option v-for="status in statuses" :value="status">{{ status }}</option>

</select>


<button @click="reset">Reset</button>


...


  <tbody v-for="dateEvent in filtered">

    <tr>

      <td>{{ dateEvent.title }}</td>

      <td>{{ dateEvent.resource }}</td>

      <td>{{ dateEvent.location }}</td>

      <td>{{ dateEvent.status }}</td>

    </tr>

  </tbody>


...



...


data() {

  return {

    dateEvents: [],

    filters: {

      title: null,

      resource: null,

      location: null,

      status: null,

    },

  };

},



computed() {


  filtered() {

    return this.dataEvents

      .filter(dataEvent => !this.filters.title || dataEvent.title === this.filters.title),

      .filter(dataEvent => !this.filters.resource || dataEvent.resource === this.filters.resource),

      .filter(dataEvent => !this.filters.location || dataEvent.location === this.filters.location),

      .filter(dataEvent => !this.filters.status || dataEvent.status === this.filters.status);

  },


  titles() {

    return this.dataEvents

      .map(dataEvent => dataEvent.title)

      .filter((title, index, self) => self.indexOf(title) === index);

  },


  resources() {

    return this.dataEvents

      .map(dataEvent => dataEvent.resource)

      .filter((resource, index, self) => self.indexOf(resource) === index);

  },


  locations() {

    return this.dataEvents

      .map(dataEvent => dataEvent.location)

      .filter((location, index, self) => self.indexOf(location) === index);

  },


  statuses() {

    return this.dataEvents

      .map(dataEvent => dataEvent.status)

      .filter((status, index, self) => self.indexOf(status) === index);

  },

},



methods: {


  reset() {

    this.filters.title = null;

    this.filters.resource = null;

    this.filters.location = null;

    this.filters.status = null;

  },


},


...


查看完整回答
反對 回復 2021-10-07
  • 1 回答
  • 0 關注
  • 235 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號