I am trying to implement "Load more" functionality instead the basic pagination in Coveo. My button code is below. Only the console logs work, the code don't send ajax to the service.
I have searched the Coveo documentation for load more button/functions but didn't find any.
//html code of button
<div data-firstres="0" class="load_more_btn">Load More</div>
//js
$('.load_more_btn').on('click', function() {
var this_btn = $(this);
var firstResult = this_btn.attr('data-firstres');
var q = $('.magic-box-input input').val();
console.log(q);
var new_res = parseInt(firstResult) + 10;
this_btn.attr('data-firstres', new_res);
console.log(new_res);
var root = document.body;
Coveo.SearchEndpoint.endpoints['default'] = new Coveo.SearchEndpoint({
restUri: 'https://platform.cloud.coveo.com/rest/search',
queryStringArguments: {
organizationId: 'replaced_val',
numberOfResults: '10',
firstResult:firstResult,
q:q
},
accessToken: acc_token,
});
Coveo.init(root);
});
答案 0 :(得分:2)
首先,您的代码将无法满足您的要求。
Coveo.init
仅应调用一次,它用于初始化搜索界面。 SearchEndpoint
只能在初始化之前设置一次,而不能在每次单击“ LoadMore”之后设置。
然后应使用JavaScript Search Events和JavaScript API calls更改查询。
因此,在您的用例中,您可以利用CoveoResultList
组件的displayMoreResults method。
您还可以遵循Coveo人员在ShowMore中创建的JavaScript Search Framework Custom Components repository自定义组件的示例。这是自定义组件调用的displayMoreResults()
方法的相关部分:
public loadMore() {
this.resultList ? this.resultList.displayMoreResults(this.options.count || 10) : null;
}
public get resultList(): ResultList | null {
const resultListElement = $$(this.root).find('.CoveoResultList');
if (resultListElement) {
return get(resultListElement) as ResultList;
}
return null;
}
答案 1 :(得分:1)
displayMoreResults对我有用
$('.load_more_btn').on('click', function() {
Coveo.$('.CoveoResultList').coveo('displayMoreResults', 5);
});