Merge pull request #6263 from dhensby/pulls/3/allow-unltd-grid-row-count

NEW Allow setting of unlimited row counts on GridFieldPaginator
This commit is contained in:
Damian Mooyman 2017-01-05 09:12:02 +13:00 committed by GitHub
commit 3e32b18c6b
2 changed files with 37 additions and 9 deletions

View File

@ -179,15 +179,23 @@ class GridFieldPaginator implements GridField_HTMLProvider, GridField_DataManipu
$totalRows = $this->totalItems;
if(!$totalRows) return null;
$totalPages = (int)ceil($totalRows/$this->itemsPerPage);
if($totalPages == 0)
$totalPages = 1;
$firstShownRecord = ($state->currentPage - 1) * $this->itemsPerPage + 1;
if($firstShownRecord > $totalRows)
$firstShownRecord = $totalRows;
$lastShownRecord = $state->currentPage * $this->itemsPerPage;
if($lastShownRecord > $totalRows)
$firstShownRecord = 1;
$lastShownRecord = $totalRows;
if ($itemsPerPage = $this->getItemsPerPage()) {
$totalPages = (int)ceil($totalRows / $itemsPerPage);
if ($totalPages == 0) {
$totalPages = 1;
}
$firstShownRecord = ($state->currentPage - 1) * $itemsPerPage + 1;
if ($firstShownRecord > $totalRows) {
$firstShownRecord = $totalRows;
}
$lastShownRecord = $state->currentPage * $itemsPerPage;
if ($lastShownRecord > $totalRows) {
$lastShownRecord = $totalRows;
}
}
// If there is only 1 page for all the records in list, we don't need to go further
// to sort out those first page, last page, pre and next pages, etc

View File

@ -52,6 +52,26 @@ class GridFieldPaginatorTest extends FunctionalTest {
$this->assertEquals(2, count($content->getBySelector('.pagination-records-number')));
}
public function testUnlimitedRowCount() {
$total = $this->list->count();
// set up the paginator
/** @var GridFieldPaginator $paginator */
$paginator = $this->gridField->getConfig()->getComponentByType("GridFieldPaginator");
$paginator->setThrowExceptionOnBadDataType(true);
$paginator->setItemsPerPage(1);
$paginator->getManipulatedData($this->gridField, $this->list);
$params = $paginator->getTemplateParameters($this->gridField)->toMap();
$this->assertFalse($params['OnlyOnePage']);
$this->assertEquals($total, $params['NumRecords']);
$paginator->setItemsPerPage(0);
$params = $paginator->getTemplateParameters($this->gridField)->toMap();
$this->assertTrue($params['OnlyOnePage']);
$this->assertEquals($total, $params['NumRecords']);
}
public function testPaginationAvoidsIllegalOffsets() {
$grid = $this->gridField;
$total = $this->list->count();