From 5290b81c5504640062b09d3258711712d2f2bd4b Mon Sep 17 00:00:00 2001 From: Normann Lou Date: Fri, 18 May 2012 11:18:55 +1200 Subject: [PATCH] ENHANCEMENT: don't show the paginating elements in the paginator when only one page, and keep 'view 1 - 5 of 5' there --- forms/gridfield/GridFieldPaginator.php | 92 +++++++++++-------- templates/Includes/GridFieldPaginator_Row.ss | 12 ++- .../gridfield/GridFieldPaginatorTest.php | 46 ++++++++++ 3 files changed, 105 insertions(+), 45 deletions(-) create mode 100644 tests/forms/gridfield/GridFieldPaginatorTest.php diff --git a/forms/gridfield/GridFieldPaginator.php b/forms/gridfield/GridFieldPaginator.php index 438086507..bb4ec3ffd 100755 --- a/forms/gridfield/GridFieldPaginator.php +++ b/forms/gridfield/GridFieldPaginator.php @@ -141,7 +141,7 @@ class GridFieldPaginator implements GridField_HTMLProvider, GridField_DataManipu $totalRows = $this->totalItems; if(!$totalRows) return array(); - $totalPages = ceil($totalRows/$this->itemsPerPage); + $totalPages = (int)ceil($totalRows/$this->itemsPerPage); if($totalPages == 0) $totalPages = 1; $firstShownRecord = ($state->currentPage - 1) * $this->itemsPerPage + 1; @@ -150,47 +150,59 @@ class GridFieldPaginator implements GridField_HTMLProvider, GridField_DataManipu $lastShownRecord = $state->currentPage * $this->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 + // we are not render those in to the paginator. + if($totalPages === 1){ + $forTemplate = new ArrayData(array( + 'OnlyOnePage' => true, + 'FirstShownRecord' => $firstShownRecord, + 'LastShownRecord' => $lastShownRecord, + 'NumRecords' => $totalRows + )); + }else{ + // First page button + $firstPage = new GridField_FormAction($gridField, 'pagination_first', 'First', 'paginate', 1); + $firstPage->addExtraClass('ss-gridfield-firstpage'); + if($state->currentPage == 1) + $firstPage = $firstPage->performDisabledTransformation(); + + // Previous page button + $previousPageNum = $state->currentPage <= 1 ? 1 : $state->currentPage - 1; + $previousPage = new GridField_FormAction($gridField, 'pagination_prev', 'Previous', 'paginate', $previousPageNum); + $previousPage->addExtraClass('ss-gridfield-previouspage'); + if($state->currentPage == 1) + $previousPage = $previousPage->performDisabledTransformation(); + + // Next page button + $nextPageNum = $state->currentPage >= $totalPages ? $totalPages : $state->currentPage + 1; + $nextPage = new GridField_FormAction($gridField, 'pagination_next', 'Next', 'paginate', $nextPageNum); + $nextPage->addExtraClass('ss-gridfield-nextpage'); + if($state->currentPage == $totalPages) + $nextPage = $nextPage->performDisabledTransformation(); + + // Last page button + $lastPage = new GridField_FormAction($gridField, 'pagination_last', 'Last', 'paginate', $totalPages); + $lastPage->addExtraClass('ss-gridfield-lastpage'); + if($state->currentPage == $totalPages) + $lastPage = $lastPage->performDisabledTransformation(); - // First page button - $firstPage = new GridField_FormAction($gridField, 'pagination_first', 'First', 'paginate', 1); - $firstPage->addExtraClass('ss-gridfield-firstpage'); - if($state->currentPage == 1) - $firstPage = $firstPage->performDisabledTransformation(); - - // Previous page button - $previousPageNum = $state->currentPage <= 1 ? 1 : $state->currentPage - 1; - $previousPage = new GridField_FormAction($gridField, 'pagination_prev', 'Previous', 'paginate', $previousPageNum); - $previousPage->addExtraClass('ss-gridfield-previouspage'); - if($state->currentPage == 1) - $previousPage = $previousPage->performDisabledTransformation(); - - // Next page button - $nextPageNum = $state->currentPage >= $totalPages ? $totalPages : $state->currentPage + 1; - $nextPage = new GridField_FormAction($gridField, 'pagination_next', 'Next', 'paginate', $nextPageNum); - $nextPage->addExtraClass('ss-gridfield-nextpage'); - if($state->currentPage == $totalPages) - $nextPage = $nextPage->performDisabledTransformation(); - - // Last page button - $lastPage = new GridField_FormAction($gridField, 'pagination_last', 'Last', 'paginate', $totalPages); - $lastPage->addExtraClass('ss-gridfield-lastpage'); - if($state->currentPage == $totalPages) - $lastPage = $lastPage->performDisabledTransformation(); - - - // Render in template - $forTemplate = new ArrayData(array( - 'FirstPage' => $firstPage, - 'PreviousPage' => $previousPage, - 'CurrentPageNum' => $state->currentPage, - 'NumPages' => $totalPages, - 'NextPage' => $nextPage, - 'LastPage' => $lastPage, - 'FirstShownRecord' => $firstShownRecord, - 'LastShownRecord' => $lastShownRecord, - 'NumRecords' => $totalRows - )); + // Render in template + $forTemplate = new ArrayData(array( + 'OnlyOnePage' => false, + 'FirstPage' => $firstPage, + 'PreviousPage' => $previousPage, + 'CurrentPageNum' => $state->currentPage, + 'NumPages' => $totalPages, + 'NextPage' => $nextPage, + 'LastPage' => $lastPage, + 'FirstShownRecord' => $firstShownRecord, + 'LastShownRecord' => $lastShownRecord, + 'NumRecords' => $totalRows + )); + } return array( 'footer' => $forTemplate->renderWith('GridFieldPaginator_Row', array('Colspan'=>count($gridField->getColumns()))), ); diff --git a/templates/Includes/GridFieldPaginator_Row.ss b/templates/Includes/GridFieldPaginator_Row.ss index ee246c129..11d8224f4 100644 --- a/templates/Includes/GridFieldPaginator_Row.ss +++ b/templates/Includes/GridFieldPaginator_Row.ss @@ -1,9 +1,11 @@ -
- $FirstPage $PreviousPage Page of $NumPages $NextPage $LastPage -
- - View $FirstShownRecord - $LastShownRecord of $NumRecords + <% if $OnlyOnePage %> + <% else %> +
+ $FirstPage $PreviousPage Page of $NumPages $NextPage $LastPage +
+ <% end_if %> + View $FirstShownRecord - $LastShownRecord of $NumRecords \ No newline at end of file diff --git a/tests/forms/gridfield/GridFieldPaginatorTest.php b/tests/forms/gridfield/GridFieldPaginatorTest.php new file mode 100644 index 000000000..bf9b6b07f --- /dev/null +++ b/tests/forms/gridfield/GridFieldPaginatorTest.php @@ -0,0 +1,46 @@ +list = new DataList('GridFieldTest_Team'); + $config = GridFieldConfig::create()->addComponent(new GridFieldPaginator(2)); + $this->gridField = new GridField('testfield', 'testfield', $this->list, $config); + $this->form = new Form(new Controller(), 'mockform', new FieldList(array($this->gridField)), new FieldList()); + } + + function testThereIsPaginatorWhenMoreThanOnePage() { + $fieldHolder = $this->gridField->FieldHolder(); + $content = new CSSContentParser($fieldHolder); + // Check that there is paginator render into the footer + $this->assertEquals(1, count($content->getBySelector('.datagrid-pagination'))); + } + + function testThereIsNoPaginatorWhenOnlyOnePage() { + // We set the itemsPerPage to an reasonably big number so as to avoid test broke from small changes on the fixture YML file + $total = $this->list->count(); + $this->gridField->getConfig()->getComponentByType("GridFieldPaginator")->setItemsPerPage($total); + $fieldHolder = $this->gridField->FieldHolder(); + $content = new CSSContentParser($fieldHolder); + + // Check that there is no paginator render into the footer + $this->assertEquals(0, count($content->getBySelector('.datagrid-pagination'))); + + // Check that there is still 'View 1 - 4 of 4' part on the left of the paginator + $this->assertEquals(1, count($content->getBySelector('.pagination-records-number'))); + } +}