[英]Algolia instantsearch.js : how to display the results with a random order?
我正在使用Algolia instantsearch.js來展示候選人參加選舉(這里: https ://laprimaire.org/candidats/)。 我希望候選人的初始顯示是隨機的,以便每個候選人獲得或多或少相同的可見性。
我在這個答案中讀到,它不是Algolia的一個功能,但是應該可以用一點js技巧來做它: 是否可以隨機排序,並查詢字段是否存在?
問題是我正在使用instantsearch.js,我找不到如何在instantsearch.js的情況下實現上述searchFunction。
我從文檔中看到,可以使用searchFunction初始化instantsearch,該函數接收輔助函數作為參數: https ://community.algolia.com/instantsearch.js/documentation/#initialization
但是它沒有找到關於這個幫助器的文檔以及如何操作它,因此我可以將隨機函數應用於搜索結果。
任何幫助將不勝感激 ! 非常感謝
Thibauld
命中小部件有一種方法可以讓您通過transformData.allItems
和templates.allItems
參數更好地控制如何顯示命中列表。
使用這個問題的shuffle
方法我如何改組數組? :
function shuffle (o){ for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); return o; }
你可以簡單地做:
search.addWidget(
instantsearch.widgets.hits({
// Your other options
transformData: {
allItems: function (content) {
return { hits: shuffle(content.hits) };
}
},
templates: {
empty: 'No results',
allItems: '{{#hits}}<your previous templates.hit value>{{/hits}}'
}
})
);
{{#hits}}{{/hits}}
模板只是一些Hogan.js
邏輯,用於循環每個匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.