Merge pull request #456 from normann/trac/7324

trac 7324: Disable GridField pagination links when only one page is visible
This commit is contained in:
Sam Minnée 2012-05-17 17:18:22 -07:00
commit 3412a0e58d
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; $totalRows = $this->totalItems;
if(!$totalRows) return array(); if(!$totalRows) return array();
$totalPages = ceil($totalRows/$this->itemsPerPage); $totalPages = (int)ceil($totalRows/$this->itemsPerPage);
if($totalPages == 0) if($totalPages == 0)
$totalPages = 1; $totalPages = 1;
$firstShownRecord = ($state->currentPage - 1) * $this->itemsPerPage + 1; $firstShownRecord = ($state->currentPage - 1) * $this->itemsPerPage + 1;
@ -151,7 +151,17 @@ class GridFieldPaginator implements GridField_HTMLProvider, GridField_DataManipu
if($lastShownRecord > $totalRows) if($lastShownRecord > $totalRows)
$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 // First page button
$firstPage = new GridField_FormAction($gridField, 'pagination_first', 'First', 'paginate', 1); $firstPage = new GridField_FormAction($gridField, 'pagination_first', 'First', 'paginate', 1);
$firstPage->addExtraClass('ss-gridfield-firstpage'); $firstPage->addExtraClass('ss-gridfield-firstpage');
@ -181,6 +191,7 @@ class GridFieldPaginator implements GridField_HTMLProvider, GridField_DataManipu
// Render in template // Render in template
$forTemplate = new ArrayData(array( $forTemplate = new ArrayData(array(
'OnlyOnePage' => false,
'FirstPage' => $firstPage, 'FirstPage' => $firstPage,
'PreviousPage' => $previousPage, 'PreviousPage' => $previousPage,
'CurrentPageNum' => $state->currentPage, 'CurrentPageNum' => $state->currentPage,
@ -191,6 +202,7 @@ class GridFieldPaginator implements GridField_HTMLProvider, GridField_DataManipu
'LastShownRecord' => $lastShownRecord, 'LastShownRecord' => $lastShownRecord,
'NumRecords' => $totalRows 'NumRecords' => $totalRows
)); ));
}
return array( return array(
'footer' => $forTemplate->renderWith('GridFieldPaginator_Row', array('Colspan'=>count($gridField->getColumns()))), 'footer' => $forTemplate->renderWith('GridFieldPaginator_Row', array('Colspan'=>count($gridField->getColumns()))),
); );

View File

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