From caafe4a214374e6f998f44995c6ca7f14624b99f Mon Sep 17 00:00:00 2001 From: vojtechbuba Date: Sat, 16 Jul 2022 22:41:15 +0200 Subject: [PATCH] Effective DataModel when used with Rest API based datasource When Datagrid is used with common database like data sources, it is common, that receiving total count for paging cost extra one query. When working with REST API datasource it is best practice that response for getting resource collection contains information about total count. For the purposes of creating query only offset and limit is necessary. The total count is necessary for two reasons. First is to show UI component with list of pages. Second to check if requested page number is not higher that collection maximum. The problem with current model is, that the order in witch get total count is received cost two calls on Rest API endpoint instead of one. I suggest to move call of getCount() method after getting data. When working with DB, it does not matter if the getCount() call is sooner or later with performance in mind. If this kind of change is too big BC break, another possibility would be to create interface for data model and enable change of DataModel. One will be more strict in terms of pagination overflow and another less. --- src/DataModel.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/DataModel.php b/src/DataModel.php index d6a4394f0..28708aabd 100644 --- a/src/DataModel.php +++ b/src/DataModel.php @@ -139,7 +139,6 @@ public function filterData( */ if ($paginatorComponent !== null) { $paginator = $paginatorComponent->getPaginator(); - $paginator->setItemCount($this->dataSource->getCount()); $this->dataSource->sort($sorting)->limit( $paginator->getOffset(), @@ -148,7 +147,10 @@ public function filterData( $this->onAfterPaginated($this->dataSource); - return $this->dataSource->getData(); + $data = $this->dataSource->getData(); + $paginator->setItemCount($this->dataSource->getCount()); + + return $data; } return $this->dataSource->sort($sorting)->getData();