Views: 3518
Last Modified: 18.01.2022
You can organize page navigation in AJAX-action by implementing in the method parameters \Bitrix\Main\UI\PageNavigation and return \Bitrix\Main\Engine\Response\DataType\Page.
Example:
use \Bitrix\Main\Engine\Response;
use \Bitrix\Main\UI\PageNavigation;
public function listChildrenAction(Folder $folder, PageNavigation $pageNavigation)
{
$children = $folder->getChildren([
'limit' => $pageNavigation->getLimit(),
'offset' => $pageNavigation->getOffset(),
]);
return new Response\DataType\Page('files', $children, function() use ($folder) {
//lazy evaluation of total records as per filter
return $folder->countChildren();
});
}
To pass page number in JS API, take note of navigation.
BX.ajax.runAction('vendor:someController.listChildren', {
data: {
folderId: 12
},
navigation: {
page: 3
}
});
Attention! Response\DataType\Page($id, $items, $totalCount)
$totalCount can be both an integer and \Closure, which can be a lazy evaluation. It's done for improved performance.
For example, in case of REST, calculation of total records is always required, but for standard AJAX it's optional. More performance and convenient is to use a separate AJAX-action for getting total records as per specific filter.