【问题标题】:Vuejs Search filterVuejs 搜索过滤器
【发布时间】:2019-03-04 15:42:15
【问题描述】:

我有一个表格,其中显示了我使用以下代码获得的项目列表:

interface getResources {
    title: string;
    category: string;
    uri: string;
    icon: string;
}
@Component
export default class uservalues extends Vue {

    resources: getResources[] = [];

    created() {
        fetch('api/Resources/GetResources')
            .then(response => response.json() as Promise<getResources[]>)
            .then(data => {
                this.resources = data;
            });
        }
    }
}

这是我的桌子:

 <div class="panel panel-default">
     <div class="panel-heading" style="font-weight:bold"><span class="glyphicon glyphicon-align-justify"></span> All Resources</div>
         <div class="row">
             <div class="search-wrapper panel-heading col-sm-12">
                 <input class="form-control" type="text" v-model="searchQuery" placeholder="Search" />
             </div>                        
         </div>
         <div class="panel-body" style="max-height: 400px;overflow-y: scroll;">
             <table v-if="resources.length" class="table">
                 <thead>
                     <tr>
                         <th>Resource</th>
                     </tr>
                 </thead>
                 <tbody>
                     <tr v-for="item in resources">
                         <td><a v-bind:href="item.uri" target="_blank">{{item.title}}</a></td>
                     </tr>
                 </tbody>
             </table>
         </div>
     </div>
</div>

我正在尝试实现为用户过滤结果的搜索栏,但我迷路了!

有什么建议吗?

【问题讨论】:

  • 您希望在输入例如B 时拥有以B 开头的项目?
  • 对不起!是的,这就是我想要的
  • 我不熟悉打字稿,但我可以使用通常的 Vue 代码给出解决方案
  • 太好了!我很感激

标签: javascript typescript vue.js vuejs2 vue-component


【解决方案1】:

您可以在这种情况下使用computed 属性,因此,我创建了一个名为filteredResources 的属性,它将在v-for 循环中使用,我使用了虚拟数据,但您可以将resources 声明为空并且调用一个 promise 函数将其填充到 created 钩子中,如果您更喜欢单个文件组件或者您通过 CDN 使用 Vue 的以下代码,请检查此 code

new Vue({
  el: '#app',
  data() {
    return {
      searchQuery:'',
    resources:[
    {title:"aaa",uri:"aaaa.com",category:"a",icon:null},
     {title:"add",uri:"aaaa.com",category:"a",icon:null},
      {title:"aff",uri:"aaaa.com",category:"a",icon:null},
    {title:"bbb",uri:"bbbb.com",category:"b",icon:null},
    {title:"bdd",uri:"bbbb.com",category:"b",icon:null},
    {title:"bsb",uri:"bbbb.com",category:"b",icon:null},
    {title:"ccc",uri:"cccc.com",category:"c",icon:null},
    {title:"ddd",uri:"dddd.com",category:"d",icon:null}
    ]
    };
  },
  computed: {
    filteredResources (){
      if(this.searchQuery){
      return this.resources.filter((item)=>{
        return item.title.startsWith(this.searchQuery);
      })
      }else{
        return this.resources;
      }
    }
  }
 

})
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" >

</head>
<body>
<div id="app">

   <div class="panel panel-default">
                <div class="panel-heading" style="font-weight:bold"><span class="glyphicon glyphicon-align-justify"></span> All Resources</div>
                <div class="row">
                    <div class="search-wrapper panel-heading col-sm-12">
                        <input class="form-control" type="text" v-model="searchQuery" placeholder="Search" />
                    </div>                        
                </div>
                <div class="panel-body" style="max-height: 400px;overflow-y: scroll;">
                    <table v-if="resources.length" class="table">
                        <thead>
                            <tr>
                                <th>Resource</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="item in filteredResources">
                                <td><a v-bind:href="item.uri" target="_blank">{{item.title}}</a></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
</div>

</body>
</html>

【讨论】:

    【解决方案2】:

    您可以使用arrayincludes() 功能来搜索句子或短语中的任何位置。

    new Vue({
      el: '#app',
      data() {
        return {
            searchQuery: null,
            resources:[
                {title:"ABE Attendance",uri:"aaaa.com",category:"a",icon:null},
                {title:"Accounting Services",uri:"aaaa.com",category:"a",icon:null},
                {title:"Administration",uri:"aaaa.com",category:"a",icon:null},
                {title:"Advanced Student Lookup",uri:"bbbb.com",category:"b",icon:null},
                {title:"Art & Sciences",uri:"bbbb.com",category:"b",icon:null},
                {title:"Auxiliares Services",uri:"bbbb.com",category:"b",icon:null},
                {title:"Basic Skills",uri:"cccc.com",category:"c",icon:null},
                {title:"Board of Trustees",uri:"dddd.com",category:"d",icon:null}
            ]
        };
      },
      computed: {
        resultQuery(){
          if(this.searchQuery){
          return this.resources.filter((item)=>{
            return this.searchQuery.toLowerCase().split(' ').every(v => item.title.toLowerCase().includes(v))
          })
          }else{
            return this.resources;
          }
        }
      }
     
    
    })
    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" >
    
    </head>
    <body>
    <div id="app">
       <div class="panel panel-default">
       <div class="panel-heading">
             <strong> All Resources</strong></div>
                <div class="row">
                     <div class="search-wrapper panel-heading col-sm-12">
                         <input class="form-control" type="text" v-model="searchQuery" placeholder="Search" />
                    </div>                        
                </div>
            <div class="table-responsive">
                <table v-if="resources.length" class="table">
                    <thead>
                        <tr>
                             <th>Resource</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="item in resultQuery">
                            <td><a v-bind:href="item.uri" target="_blank">{{item.title}}</a></td>
                        </tr>
                    </tbody>
                </table>
            </div>
       </div>
       </div>
    
    </body>
    </html>

    基于这个答案→Vue.js search keyword in array

    【讨论】:

    • 我知道它已经过时了,但是如果我想在一个搜索输入框中搜索不只是标题,还想搜索 uri、类别等其他列怎么办?
    • 试试这个函数:filteredResources (){ if(this.searchQuery.length > 3){ return this.resources.filter((item)=>{ var columns = item.title + item.uri ; 返回 this.searchQuery.toLowerCase().split(' ').every(v => columns.toLowerCase().includes(v)); }) } }
    【解决方案3】:

    这里是 Vue 3 版本的简单搜索。

    const { createApp, ref } = Vue
    
    const App = {
      setup() {
        let searchText = ref('')
        const list = [
          'orange',
          'red',
          'blue',
          'black',
          'white'
        ]
        
        function filteredList() {
          return list.filter(data => data.toLowerCase().includes(searchText.value.toLowerCase()))
        }
        
        return {searchText, filteredList}
      }
    }
    
    Vue.createApp(App).mount('#app')
    <script src="https://unpkg.com/vue@next"></script>
    
    <div id="app">
      <input type="text" v-model="searchText">
      <ul>
        <li v-for="data in filteredList()" :key="data">
          {{ data }}
        </li>
      </ul>
    </div>

    【讨论】:

      【解决方案4】:

      我找到了简单的方法...使用“@input 函数”.. 它对我有用!

      new Vue({
        el: '#app',
        data() {
          return {
            searchQuery:'',
            resources:[
              {title:"aaa",uri:"aaaa.com",category:"a",icon:null},
              {title:"add",uri:"aaaa.com",category:"a",icon:null},
              {title:"aff",uri:"aaaa.com",category:"a",icon:null},
              {title:"bbb",uri:"bbbb.com",category:"b",icon:null},
              {title:"bdd",uri:"bbbb.com",category:"b",icon:null},
            ]
          };
        },
        methods: {
          
          getTextSearch: function(textSearch) {
      
              this.filteredResources= this.resources
      
              this.filteredResources= this.filteredResources.filter(item => {
                  return item.uri.toLowerCase().includes(textSearch);
              });
      
          },
      
        }
       
      
      })
      

      这里是html:

          <!DOCTYPE html>
      <html>
      <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" >
      
      </head>
      <body>
      <div id="app">
      
         <div class="panel panel-default">
                      <div class="panel-heading" style="font-weight:bold"><span class="glyphicon glyphicon-align-justify"></span> All Resources</div>
                      <div class="row">
                          <div class="search-wrapper panel-heading col-sm-12">
                              <input class="form-control" @input="getTextSearch($event)" type="text" placeholder="Search" />
                          </div>                        
                      </div>
                      <div class="panel-body" style="max-height: 400px;overflow-y: scroll;">
                          <table v-if="resources.length" class="table">
                              <thead>
                                  <tr>
                                      <th>Resource</th>
                                  </tr>
                              </thead>
                              <tbody>
                                  <tr v-for="item in filteredResources">
                                      <td><a v-bind:href="item.uri" target="_blank">{{item.title}}</a></td>
                                  </tr>
                              </tbody>
                          </table>
                      </div>
                  </div>
      </div>
      
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 2019-07-14
        • 2020-09-15
        • 2018-12-10
        • 1970-01-01
        • 1970-01-01
        • 2019-11-11
        • 2020-01-23
        • 1970-01-01
        • 2020-01-12
        相关资源
        最近更新 更多