Algolia initial search parameters instantsearch.js

醉酒当歌 提交于 2019-12-07 05:32:19

问题


I have an index where I am trying to pass initial values for the query in algolia. I am using the instantsearch.js and right now it just loads everything from my index. How can I pass initial values to the index on page load?

For example Select * from index where Category='Careers' (passing careers as a value on the load)

I've searched the documentation for this and cant figure it out.

Also, I need to pass multiple values, so something like:

select * from index where Category = 'Careers' or 'Skills' or 'Interests' (with or statements)

Thanks!


回答1:


I assume you are using a refinementList widget: https://community.algolia.com/instantsearch.js/documentation/#refinementlist. On the 'category' attribute of your data.

If so, you can do this:

var preselectedCategories = ['Careers', 'Skills'];

var search = instantsearch(applicationID, apiKey, {
  ...other parameters,
  searchParameters: {
    disjunctiveFacetsRefinements: {
      category: preselectedCategories
    }
  }
})

You will also need to do this in the refinementList instantiation:

var refinementList = instantsearch.widgets.refinementList({
  transformData: {
    item: function(item) {
      if (preselectedCategories.indexOf(item.name) !== -1) {
        item.cssClasses.label += ' pre-selected';
      }

      return item;
    }
  }
});

Then all the preselected categories items will have the "pre-selected" css class by default.

Then you can use css and this class name to do:

.pre-selected {
  display: none;
}

Let me know



来源:https://stackoverflow.com/questions/35437004/algolia-initial-search-parameters-instantsearch-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!