Фильтровать список ul по вводимому тексту Vue.js 2

1

Я хочу отфильтровать один список ul, когда пользователь вводит вместо имени или инициалов входной файл. Я работаю с vue.js 2. В списке отображается вызов переменной store, JSON файла.

МАГАЗИН ПЕРЕМЕН

$store.coin.coin

Пример JSON

{
 "coin": [
 {
  "initials": "DLR",
  "price": "0.727282",
  "volume": "1212",
  "change": "+1.22",
  "name": "dollar"
 }, ...
 ]
}

HTML

          <li>
            <div class="input-group search-box">
              <input id="filter-coins" v-model="search" placeholder="Filter" class="input-group-field" type="text">
            </div>
          </li>
          <li class="tabs-title" v-for="item in filteredItems" :key="item.initials" >
            <a href="#coinList" aria-selected="true">
              <div class="grid-x">
                <div class="auto cell">{{ item.initials }}</div>
                <div class="auto cell">{{ item.price }}</div>
                <div class="auto cell">{{ item.volume }}</div>
                <div class="auto cell">{{ item.change }}</div>
                <div class="auto cell">{{ item.name }}</div>
              </div>
            </a>
          </li>

МЕТОД

  export default {
    name: 'coins',
    search: '',
   computed: {
    filteredItems() {
      return this.$store.coin.coin.filter(item =>
        (item.name.toLowerCase().indexOf(this.search.toLowerCase()) > -1));
    },
   },
  };

store.js

import Vue from 'vue';
import Vuex from 'vuex';
import coin from '../data/coins-btc.json';

Vue.use(Vuex);

export default {
 state: {
  coin,
  },
 getters: {
  coin: state => state.coin,
 },
};
  • 1
    Можете ли вы сделать jsFiddle с вашим кодом?
  • 0
    если я сделаю скрипку, я не смогу загрузить данные из $ store, что мне кажется самым большим различием между stackoverflow.com/questions/41712791/… ?
Показать ещё 2 комментария
Теги:
vue.js
vuejs2
vuex

1 ответ

1
Лучший ответ

Вот пример фильтра, который работает либо с initials либо с name.

computed:{
  filteredItems(){
    let coins = this.$store.getters.coin.coin
    // if there is no filter, return everything
    if (!this.search) return coins

    // setup the filter function
    let searchValue = this.search.toLowerCase()
    let filter = coin => coin.initials.toLowerCase().includes(searchValue) ||
                         coin.name.toLowerCase().includes(searchValue)

    return coins.filter(filter)
  }
}

И вот пример этого в действии.

console.clear()

const store = new Vuex.Store({
  state: {
    coin: {
      "coin": [
        {
          "initials": "DLR",
          "price": "0.727282",
          "volume": "1212",
          "change": "+1.22",
          "name": "dollar"
        },
        {
          "initials": "EUR",
          "price": "0.727282",
          "volume": "1212",
          "change": "+1.22",
          "name": "euro"
        },
        {
          "initials": "AUS",
          "price": "0.727282",
          "volume": "1212",
          "change": "+1.22",
          "name": "australian"
        }        
      ]
    }
  },
  getters:{
    coin: state => state.coin
  }
})

new Vue({
  el: "#app",
  store,
  data:{
    search: null
  },
  computed:{
    filteredItems(){
      let coins = this.$store.getters.coin.coin
      // if there is no filter, return everything
      if (!this.search) return coins
      
      // setup the filter function
      let searchValue = this.search.toLowerCase()
      let filter = coin => coin.initials.toLowerCase().includes(searchValue) ||
                           coin.name.toLowerCase().includes(searchValue)
      
      return coins.filter(filter)
    }
  }
})
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vuex/2.4.0/vuex.js"></script>
<div id="app">
  <ul>
    <li>
      <div class="input-group search-box">
        <input id="filter-coins" v-model="search" placeholder="Filter" class="input-group-field" type="text">
      </div>
    </li>
    <li class="tabs-title" v-for="item in filteredItems" :key="item.initials" >
      <a href="#coinList" aria-selected="true">
        <div class="grid-x">
          <div class="auto cell">{{ item.initials }}</div>
          <div class="auto cell">{{ item.price }}</div>
          <div class="auto cell">{{ item.volume }}</div>
          <div class="auto cell">{{ item.change }}</div>
          <div class="auto cell">{{ item.name }}</div>
        </div>
      </a>
    </li>
  </ul>

</div>

Ещё вопросы

Сообщество Overcoder
Наверх
Меню