在Vue 3中基于下拉选择过滤数据

643ylb08  于 2023-03-31  发布在  Vue.js
关注(0)|答案(4)|浏览(256)

我有一个JSON文件,其中包含按语言/国家排序的数据列表。这些数据将显示在标准的HTML表格中。但是因为表格会变得太长,我希望能够有一个国家列表的下拉列表,并根据此选择过滤表格的结果。

[
    { "language": "English (United States)", "name": "New York" },
    { "language": "English (United States)", "name": "Chicago" },
    { "language": "English (United Kingdom)", "name": "London" },
    { "language": "English (United Kingdom)", "name": "Manchester" },
    { "language": "English (Australia)", "name": "Melbourne" },
    { "language": "English (Australia)", "name": "Sydney" }
]

这是vue文件。

<script>
  import countries from 'countries.json'

  export default {
    data() {
      return {
         countries: countries,
         filter: ''
       }
    }

  }
</script>

<template>
  <select name="filter" id="langFilter" v-model="filter">
    <option value="English (United States)">English (United States)</option>
    <option value="English (United Kingdom)">English (United Kingdom)</option>
    <option value="English (Australia)">English (Australia)</option>
  </select>

  <div class="table-responsive">
    <table class="table table-striped table-nowrap align-middle mb-0">
      <thead>
        <tr>
          <th scope="col">Language</th>
          <th scope="col">Name</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(country, index) in countries" :key="index">
          <td>{{ country.language }}</td>
          <td>{{ country.name }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>
nr9pn0ug

nr9pn0ug1#

这里有两种方法可以做到这一点。你可以使用v-if来只显示与你的过滤器匹配的行。

<template v-for="(country, index) in countries" :key="index">
    <tr v-if="country.language === filter">
        <td>{{ country.language }}</td>
        <td>{{ country.name }}</td>
    </tr>
</template>

或者,您可以使用计算值。
当过滤器更改时,filteredCountries计算值将仅返回与过滤器匹配的国家/地区。
我已经提供了一个基于你给的工作代码。

<script>
  import countries from './countries.json'

  export default {
    data() {
      return {
         countries: countries,
         filter: ''
       }
    },
    computed: {
      filteredCountries() {
        return this.countries.filter((country) => {
          return country.language === this.filter;
        })
      }
    }

  }
</script>

<template>
  <select name="filter" id="langFilter" v-model="filter">
    <option value="English (United States)">English (United States)</option>
    <option value="English (United Kingdom)">English (United Kingdom)</option>
    <option value="English (Australia)">English (Australia)</option>
  </select>

  <div class="table-responsive">
    <table class="table table-striped table-nowrap align-middle mb-0">
      <thead>
        <tr>
          <th scope="col">Language</th>
          <th scope="col">Name</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(country, index) in filteredCountries" :key="index">
            <td>{{ country.language }}</td>
            <td>{{ country.name }}</td>
          </tr>
      </tbody>
    </table>
  </div>
</template>
eyh26e7m

eyh26e7m2#

只需使用计算属性按所选语言过滤国家/地区,并将其放入表中:

const tableItems = computed(() => selectedLanguage.value 
  ? countries.filter(c => c.language === selectedLanguage.value) 
  : countries
)

x一个一个一个一个x一个一个二个x

ztyzrc3y

ztyzrc3y3#

真的没有人知道<datalist>吗?

<script>
  import countries from 'countries.json'

  export default {
    data() {
      return {
         countries: countries,
         filter: ''
       }
    }

  }
</script>

<template>
  <input list="langFilter" v-model="filter">

  <datalist id="langFilter">
    <!-- All you need to do is to for loop your countries array -->
    <option value="English (United States)">English (United States)</option>
    <option value="English (United Kingdom)">English (United Kingdom)</option>
    <option value="English (Australia)">English (Australia)</option>
    <option value="Italian (Italy)">Italian (Italy)</option>
  </datalist>
</template>

证明它的工作!

<input list="langFilter" v-model="filter">

<datalist id="langFilter">
<!-- All you need to do is to for loop your countries array -->
<option value="English (United States)">English (United States)</option>
<option value="English (United Kingdom)">English (United Kingdom)</option>
<option value="English (Australia)">English (Australia)</option>
<option value="Italian (Italy)">Italian (Italy)</option>
</datalist>
slhcrj9b

slhcrj9b4#

当你在filter变量中存储选择模型值时,你可以简单地过滤掉v-for中模板本身的countries数组。

<tr v-for="(country, index) in countries.filter(c => c.language === filter)" :key="index">
    <td>{{ country.language }}</td>
    <td>{{ country.name }}</td>
</tr>

相关问题