[英]Filtering items using a condition (vue.js)
This is now solved现在解决了
The problem问题
I am trying to make a filter that does this.我正在尝试制作一个可以做到这一点的过滤器。 Gets 'name' from session storage and matches it with 'name' on product.
从 session 存储中获取“名称”并将其与产品上的“名称”匹配。
I'm storing all 'logged in' user data on 'sessionStorage' for practice.我将所有“登录”用户数据存储在“sessionStorage”中以供练习。
I'm storing all the 'products' in 'localStorage'我将所有“产品”存储在“localStorage”中
I'm just doing this as a small project to understand and practice vue & js.我只是把这个作为一个小项目来理解和实践vue&js。
Basically if the product has been created by user x, upon login, user x should only be able to see the products he listed and not the rest of the products.基本上如果产品是由用户x创建的,在登录时,用户x应该只能看到他列出的产品,而不是产品的rest。
This is what I can do这是我能做的
I can get the user info from sessionStorage and when creating a product, the user 'name' is passed on along with the product details.我可以从 sessionStorage 获取用户信息,并且在创建产品时,用户“名称”与产品详细信息一起传递。
I can also retrieve all the products from localStorage.我还可以从 localStorage 中检索所有产品。
I don't know how to come up with a filter using vue.js that does this.我不知道如何使用 vue.js 提出一个过滤器来做到这一点。
Some code sample would really help me understand whats going on.一些代码示例真的可以帮助我理解发生了什么。
Additionally, I want to display the results in html.此外,我想在 html 中显示结果。
Thanks in advance.提前致谢。
},
computed: {
filteredProducts() {
this.products.filter(x => x.name === this.loggedUser[0].user);
}
},
});```
[1]: https://i.stack.imgur.com/W7PMf.png
computed: {
filteredProducts() {
return this.products.filter(x => x.name === this.loggedUser[0].text); // text or user, whichever field is for username
},
},
After that show the list in html use v-for
.之后使用
v-for
显示 html 中的列表。
<p v-if="filteredProducts.length === 0">No products found</p>
<li v-for="(product, key) in filteredProducts" :key="key">Product: {{ product.description }}, Price {{ product.price }} <button @click="deleteProduct(key)">Delete</button></li>
add delete method添加删除方法
methods: {
// ...
deleteProduct(index) {
this.products.splice(index, 1);
},
}
I think this may work from you (given what you have provided above)我认为这可能对您有用(鉴于您在上面提供的内容)
computed: {
filteredProducts () {
return this.products.filter(function (item) {
return (item.name === this.loggedUser[0].name)
}
}
}
Basically, we are filtering the products based on the name that is tagged on that list with the loggedUser
name.基本上,我们是根据在该列表中使用
loggedUser
名称标记的名称过滤产品。
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.