使用搜索查询过滤数据?(Filter data using a search query? Wordpress JSON object + VueJS2)

系统教程 行业动态 更新时间:2024-06-14 17:01:34
使用搜索查询过滤数据?(Filter data using a search query? Wordpress JSON object + VueJS2)

编辑:添加了实时代码编辑器: https : //ide.c9.io/dosstx/wordpress

我正在尝试使用VueJS2和Wordpress REST API过滤Wordpress JSON数据对象(我的真实世界示例中有自定义帖子类型)。

我遇到了布线问题,并根据搜索框中输入的搜索条件对表进行过滤。

没有搜索功能,一切正常,但一旦我尝试使用searchterm进行过滤,没有任何反应 - 控制台中没有错误。

我有我的Vue实例:

var vm = new Vue({ el: '#app', data: { searchTerm: '', posts: [] }, computed: { filteredItems: function(){ return this.posts.filter(function(post) { return this.post.searchTerm; //i believe this line is the culprit }); } }, created: function(){ $.get('mylocalhost/wp-json/wp/v2/products/' + '?_embed=true') .done(function(data) { vm.posts = data; }); } });

我的HTML:

<div id="app"> <form> <input type="text" v-model="searchTerm"> </form>

并进一步下载我的HTML ....:

<tr v-for="post in filteredItems"> <td>{{post.title.rendered}}</td> ...snip ... </div>

任何有关如何修复的线索将不胜感激。

EDIT: Live Code Editor added: https://ide.c9.io/dosstx/wordpress

I am trying to filter a Wordpress JSON data object using VueJS2 and the Wordpress REST API (I have a custom post type in my real world example).

I'm having trouble with the wiring and getting the table to filter based on the search terms that are typed into the search box.

Without the search function, everything works fine, but once I try to filter using a searchterm, nothing happens -- no error in console.

I have my Vue instance like so:

var vm = new Vue({ el: '#app', data: { searchTerm: '', posts: [] }, computed: { filteredItems: function(){ return this.posts.filter(function(post) { return this.post.searchTerm; //i believe this line is the culprit }); } }, created: function(){ $.get('mylocalhost/wp-json/wp/v2/products/' + '?_embed=true') .done(function(data) { vm.posts = data; }); } });

My HTML:

<div id="app"> <form> <input type="text" v-model="searchTerm"> </form>

And further down my HTML....:

<tr v-for="post in filteredItems"> <td>{{post.title.rendered}}</td> ...snip ... </div>

Any clues on how to fix would be greatly appreciated.

最满意答案

您没有正确使用filter方法。

从MDN Docs中获取filter方法 :

filter()为数组中的每个元素调用一次提供的回调函数,并构造一个包含所有值的新数组,其中回调返回一个强制为true的值。

传递给filter的回调应返回一个Boolean值,以确定是否在过滤后的数组中包含该数组的元素。

在您的情况下,我假设您的post对象有一些您想要搜索的属性(比如content ),并且您希望仅包含包含搜索词的内容的帖子。 所以你可以这样做:

computed: { filteredItems: function() { return this.posts.filter(function(post) { return post.content.indexOf(this.searchTerm) != -1; }); } },

You aren't using the filter method correctly.

From the MDN Docs for the filter method:

filter() calls a provided callback function once for each element in an array, and constructs a new array of all the values for which callback returns a value that coerces to true.

The callback passed to filter should return a Boolean value to determine whether or not to include the element of the array in the filtered array.

In your case, I'm assuming your post objects have some property (say content) you want to search and that you want to only include posts with content that contain the search term. So you can do something like this:

computed: { filteredItems: function() { return this.posts.filter(function(post) { return post.content.indexOf(this.searchTerm) != -1; }); } },

更多推荐

本文发布于:2023-04-20 18:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/87bc5b8f9cbb53c26905f1c4b013d3e0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   data   search   query   Filter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!