简体   繁体   English

如何使用JavaScript将过滤器发送到api

[英]How to send filters to an api with javascript

I have a catalog list with a 'favorite' option. 我有一个带有“收藏夹”选项的目录列表。 Now I'm trying to make a watchlist that only shows the selected favorites. 现在,我正在尝试创建一个仅显示所选收藏夹的监视列表。 The data comes from the same api, but now for the watchlist I need to filter the api data to only show the favorites. 数据来自相同的api,但是现在对于监视列表,我需要过滤api数据以仅显示收藏夹。

This is the code 这是代码

    "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");
      }
    );
  }
},

This is the JSON data 这是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

This is entirely based off of the API that you are communicating with. 这完全基于您正在与之通信的API。 The api would have provide hooks to filter based on what you want. 该api将提供钩子以根据您的需要进行过滤。 This could be done using query parameters `ie $.api("/catalog?type=favorites". But this is an impossible question to completely answer without that information. 这可以使用查询参数“ ie $ .api(“ / catalog?type = favorites””来完成,但是如果没有这些信息,要完全回答这个问题是不可能的。

Filter result.data and give only filtered items to watchlist when promise gets resolved. 过滤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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM