vue.js - 实时搜索组件
问题描述
我从外部 API 链接获取我的数据,我想过滤它们 - 实时搜索,所以当我输入一个字母时,包含该字母的结果将被过滤。
现在我无法做到这一点,当我点击我的输入时,我只会打印出所有结果,而当我试图过滤它们时什么也没有发生。
这是我在脚本中的逻辑:
data() {
return {
result: "",
modal: false,
results: [],
filteredResults: [],
};
},
mounted() {
axios
.get("secretDataURL")
.then((response) => {
this.filteredResults = response.data;
})
.catch((error) => (this.filteredResults = console.log(error)))
.finally(() => console.log("Data successfully loaded"));
},
methods: {
filterResults() {
if (this.result.lenght == 0) {
this.filteredResults = this.results;
}
this.filteredResults = this.results.filter((result) => {
return result.toLowerCase().startsWith(this.result.toLowerCase());
});
},
setResult(result) {
this.result = result;
this.modal = false;
},
},
watch: {
state() {
this.filterResults();
},
}
还有我的模板
<div @click="modal = false"></div>
<input
type="text"
v-model="result"
autocomplete="off"
@input="filterResults"
@focus="modal = true"
/>
<div v-if="filteredResults && modal">
<ul>
<li
v-for="(filteredResult, index) in filteredResults"
:key="index"
@click="setResult(filteredResult)"
>
{{ filteredResult.name }}
</li>
</ul>
</div>
我怎样才能使它工作,我的逻辑在哪里失败?
解决方案
推荐阅读
- python - WxPython将选择转换为数值TypeError
- scala - 找不到参数 F 的隐式值:cats.Applicative[F]
- linux - 无法在 linux 机器上的 chrome 浏览器中运行 nightwatch.js 测试
- c# - 使用 [X] 按钮打开/显示其他表单单击 C#
- javascript - 如何在应用程序中从 JSON 服务器获取一些特定数据
- node.js - Node js:libuv 线程池是如何工作的?
- java - 将变量从片段发送到 MainActivity
- sql-server - docker pull 命令不允许密码并且总是从守护进程得到错误响应:未授权:需要身份验证
- asp.net-core - 其他项目的模板 asp.net core
- java - 如何在 Infinispan 中启用 JGroups 登录