AlertThis post is over a year old, some of this information may be out of date.
Adding paging numbers to the control list with paging template
When you are using the Content Search Web Part, you have by default a control template list with paging available. Now this template only shows left and right buttons to navigate to the next or previous page:
These two buttons do not give you very much visual information about paging. For example: you do not know on which page you currently are and you can only go back one page at a time.
If you go to a SharePoint search center the paging works a bit differently compared to the CSWP template. In the default control template of the search result web part the same left and right paging buttons are included, but you also have page numbers which makes it easier to navigate.
As this is just some additional JavaScript it can be easily added to your own templates so that you can create a control template with paging numbers for the CSWP.
Solution
The code you need to add to get these paging numbers in your control template is the following:
<!--#_
// Show the paging numbers
for (var i = 0; i < pagingInfo.length; i++) {
var pi = pagingInfo[i];
if(!$isNull(pi)) {
if (pi.pageNumber !== -1 && pi.pageNumber !== -2) {
var pageLinkId = "PageLink_" + pi.pageNumber;
// Check if it is the current selected page
if (pi.startItem === -1) {
_#--><strong>_#= $htmlEncode(pi.pageNumber) =#_</strong><!--#_
} else {
_#--><aid="_#= $htmlEncode(pageLinkId) =#_"href="#"title="_#= $htmlEncode(pi.title) =#_"onclick="$getClientControl(this).page(_#= $htmlEncode(pi.startItem) =#_);return Srch.U.cancelEvent(event);">_#= $htmlEncode(pi.pageNumber) =#_</a><!--#_
}
}
}
}
_#-->
ย
The code loops over all the available pages. You need to be aware that there are two special numbers that you can retrieve which is:
“-1”: move to the previous page
“-2”: move to the next page
These special numbers are used for the left and right buttons, so they do not need to be included in the paging numbers. That is why there is a check to see if the pageNumber is not equal to -1 or -2.
This code needs to be added in between the left and right buttons: