Skip to content

Commit 3094411

Browse files
FIX Make sure count is correct after filtering (#448)
1 parent 769d97e commit 3094411

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"require": {
2828
"php": "^8.1",
2929
"silverstripe/vendor-plugin": "^2",
30-
"silverstripe/framework": "^5"
30+
"silverstripe/framework": "^5.2"
3131
},
3232
"require-dev": {
3333
"phpunit/phpunit": "^9.6",

src/GridFieldConfigurablePaginator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class GridFieldConfigurablePaginator extends GridFieldPaginator
4646
*/
4747
protected $pageSizes = array();
4848

49+
/**
50+
* Used to make sure we only count the list once.
51+
* Required because the parent class sets totalItems to 0 by default.
52+
*/
4953
private bool $haveCheckedCount = false;
5054

5155
/**
@@ -264,6 +268,10 @@ public function getManipulatedData(GridField $gridField, SS_List $dataList)
264268
// Assign the GridField to the class so it can be used later in the request
265269
$this->setGridField($gridField);
266270

271+
// Update item count prior to limit. This ensures filtered lists have the correct count.
272+
$this->totalItems = $dataList->count();
273+
$this->haveCheckedCount = true;
274+
267275
// Retain page sizes during actions provided by other components
268276
$state = $this->getGridPagerState();
269277
if (is_numeric($state->pageSize)) {

tests/GridFieldConfigurablePaginatorTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
use SilverStripe\Dev\SapphireTest;
77
use SilverStripe\Forms\GridField\GridField;
88
use SilverStripe\Forms\GridField\GridField_FormAction;
9+
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
10+
use SilverStripe\Forms\GridField\GridFieldPaginator;
911
use SilverStripe\ORM\ArrayList;
12+
use SilverStripe\ORM\Search\BasicSearchContext;
13+
use SilverStripe\View\ArrayData;
1014
use Symbiote\GridFieldExtensions\GridFieldConfigurablePaginator;
1115

1216
class GridFieldConfigurablePaginatorTest extends SapphireTest
@@ -27,6 +31,7 @@ protected function setUp(): void
2731
}
2832

2933
$this->gridField = GridField::create('Mock', null, $data);
34+
$this->gridField->getConfig()->removeComponentsByType(GridFieldPaginator::class);
3035
}
3136

3237
public function testGetTotalItems()
@@ -37,6 +42,18 @@ public function testGetTotalItems()
3742
$this->assertSame(130, $paginator->getTotalItems());
3843
}
3944

45+
public function testGetTotalItemsDuringFilter(): void
46+
{
47+
$paginator = new GridFieldConfigurablePaginator;
48+
$this->gridField->getConfig()->addComponent($paginator);
49+
$this->gridField->getConfig()->getComponentByType(GridFieldFilterHeader::class)
50+
->setSearchContext(new BasicSearchContext(ArrayData::class, ['ID']));
51+
$this->gridField->State->GridFieldFilterHeader->Columns = ['ID' => '2'];
52+
$this->gridField->getManipulatedList();
53+
54+
$this->assertSame(31, $paginator->getTotalItems());
55+
}
56+
4057
public function testGetFirstShown()
4158
{
4259
$paginator = new GridFieldConfigurablePaginator;

0 commit comments

Comments
 (0)