问题
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