ENHANCEMENT: don't show the paginating elements in the paginator when only one page, and keep 'view 1 - 5 of 5' there

This commit is contained in:
Normann Lou 2012-05-18 11:18:55 +12:00
parent d8b5b97e28
commit 5290b81c55
3 changed files with 105 additions and 45 deletions

View File

@ -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()))),
);

View File

@ -1,9 +1,11 @@
<tr>
<td class="bottom-all" colspan="$Colspan">
<div class="datagrid-pagination">
$FirstPage $PreviousPage <span class="pagination-page-number">Page <input class="text" value="$CurrentPageNum" data-skip-autofocus="true" /> of $NumPages</span> $NextPage $LastPage
</div>
<span class="pagination-records-number">View $FirstShownRecord - $LastShownRecord of $NumRecords</span>
<% if $OnlyOnePage %>
<% else %>
<div class="datagrid-pagination">
$FirstPage $PreviousPage <span class="pagination-page-number">Page <input class="text" value="$CurrentPageNum" data-skip-autofocus="true" /> of $NumPages</span> $NextPage $LastPage
</div>
<% end_if %>
<span class="pagination-records-number">View $FirstShownRecord - $LastShownRecord of $NumRecords</span>
</td>
</tr>

View File

@ -0,0 +1,46 @@
<?php
class GridFieldPaginatorTest extends FunctionalTest {
/** @var ArrayList */
protected $list;
/** @var GridField */
protected $gridField;
/** @var string */
static $fixture_file = 'GridFieldTest.yml';
/** @var Form */
protected $form;
/** @var array */
protected $extraDataObjects = array('GridFieldTest_Team', 'GridFieldTest_Player');
public function setUp() {
parent::setUp();
$this->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')));
}
}