[英]How to send filters to an api with javascript
我有一个带有“收藏夹”选项的目录列表。 现在,我正在尝试创建一个仅显示所选收藏夹的监视列表。 数据来自相同的api,但是现在对于监视列表,我需要过滤api数据以仅显示收藏夹。
这是代码
"watchlist": {
cols: 5,
promise: function() {
return $.api("/catalog", {
count: 4,
}).then(
function(result) {
$(".dashboardpage .widget-watchlist").render('pages/dashboard/widget-watchlist', {
watchlist: result.data
}).animo("enterContent");
},
function(err) {
$(".dashboardpage .widget-watchlist").render('pages/dashboard/widget-watchlist', {
watchlist: [],
error: err
}).animo("enterContent");
}
);
}
},
这是JSON数据
data: [{score: 4, id: 49878, description: "ACT 230V aansluitkabel C13 - C14 blauw. Lengte: 3 m",…},…]
filters: {price: ["0", "9999999999"], in_stock: "0", category: "", price_limit: [1, 8], search: "",…}
category: ""
favorite: {doc_count: 15, filtered: {doc_count_error_upper_bound: 0, sum_other_doc_count: 0, buckets: []}}
doc_count: 15
这完全基于您正在与之通信的API。 该api将提供钩子以根据您的需要进行过滤。 这可以使用查询参数“ ie $ .api(“ / catalog?type = favorites””来完成,但是如果没有这些信息,要完全回答这个问题是不可能的。
过滤result.data并在promise得到解决时仅将过滤后的项目提供给监视列表。
function(result) {
$(".dashboardpage .widget-watchlist").render('pages/dashboard/widget-watchlist', {
watchlist: result.data.filter(item => favoriteItemIds.indexOf(item.id) != -1)
}).animo("enterContent");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.