[英]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.