简体   繁体   English

在我的 vue.js 搜索功能中,循环数据起初没有显示

[英]Loop data is not showing at first in my vue.js search feature

I have a search feature using Vue.Js.我有一个使用 Vue.Js 的搜索功能。 The code below works when clicking the search button.单击搜索按钮时,下面的代码有效。 My concern is, all data does not appear at first, it appears only when I click search.我担心的是,所有数据一开始并没有出现,只有当我点击搜索时才会出现。 What I want, data appears at the start.我想要的,数据出现在开头。 Is there anything missing in the code I created?我创建的代码中是否缺少任何内容? please help me solving this problem请帮我解决这个问题

<template>
    <div>
        <select v-model="selectedLevel">
            <option value="" disabled selected hidden>Level</option>
            <option value="beginner">Beginner</option>
            <option value="intermediate">Intermediate</option>
            <option value="advanced">Advanced</option>
        </select>

        <select v-model="selectedTime">
            <option value="" disabled selected hidden>Time</option>
            <option value="30">30 Min</option>
            <option value="60">60 Min</option>
        </select>

        <select v-model="selectedType">
            <option value="" disabled selected hidden>Type</option>
            <option value="cycling">Cycling</option>
            <option value="boxing">Boxing</option>
        </select>

        <button @click="search">Search</button>

        <div class="list-item" v-for="item in searchResult">
            <div class="card">
              <p>Class Name: {{ item.type }}</p>
              <p>Level: {{ item.level }}</p>
              <p>Time: {{ item.time }}</p>
            </div>
        </div>
    </div>
</template>
export default {
        data() {
            return {
                selectedType: '',
                selectedTime: '',
                selectedLevel: '',
                items: [{
                      type: 'cycling',
                      time: '30',
                      level: 'beginner'
                    },
                    {
                      type: 'boxing',
                      time: '60',
                      level: 'beginner'
                    },
                    {
                      type: 'cycling',
                      time: '60',
                      level: 'advanced'
                    },
                    {
                      type: 'boxing',
                      time: '30',
                      level: 'advanced'
                    }
                ],
                searchResult: [],
            }
        },
        methods: {
            search() {
                let filterType = this.selectedType,
                filterTime = this.selectedTime,
                filterLevel = this.selectedLevel

                this.searchResult = this.items.filter(function(item) {
                    let filtered = true
                    if (filterType && filterType.length > 0) {
                        filtered = item.type == filterType
                    }
                    if (filtered) {
                        if (filterTime && filterTime.length > 0) {
                        filtered = item.time == filterTime
                        }
                    }
                    if (filtered) {
                        if (filterLevel && filterLevel.length > 0) {
                        filtered = item.level == filterLevel
                        }
                    }
                    return filtered
                })
            }
        }
    }

You need to run the search method once the component is mounted because now runs only when you click the button, try the below:安装组件后,您需要运行search方法,因为现在仅在您单击按钮时运行,请尝试以下操作:

inside export default add the below:在导出默认值内添加以下内容:

data() {
            return {
                selectedType: '',
                selectedTime: '',
                selectedLevel: '',
                items: [{
                      type: 'cycling',
                      time: '30',
                      level: 'beginner'
                    },
                    {
                      type: 'boxing',
                      time: '60',
                      level: 'beginner'
                    },
                    {
                      type: 'cycling',
                      time: '60',
                      level: 'advanced'
                    },
                    {
                      type: 'boxing',
                      time: '30',
                      level: 'advanced'
                    }
                ],
                searchResult: [],
            }
        },
mounted(){
 this.search();
}

Just add the mounted part只需添加已mounted的部分

When the page loads this.searchResult is empty array.当页面加载时this.searchResult是空数组。

You search method is called only when you click search button.仅当您单击搜索按钮时才会调用您的search方法。 and then filtered items are assigned to this.searchResult然后将过滤后的项目分配给this.searchResult

Simple solution would be to manually call the search method on component mounted or created hook.简单的解决方案是手动调用组件mountedcreated的钩子上的search方法。

Better solution would be to rewrite your search logic using pipe and computed propeties更好的解决方案是使用 pipe 和计算的属性重写您的搜索逻辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM