Skip to content

Instantly share code, notes, and snippets.

@tranch
Last active March 13, 2018 06:11
Show Gist options
  • Select an option

  • Save tranch/df6460844c2a2f2dcd67dbf4d4264fdb to your computer and use it in GitHub Desktop.

Select an option

Save tranch/df6460844c2a2f2dcd67dbf4d4264fdb to your computer and use it in GitHub Desktop.
<?php
class GridView extends \kartik\grid\GridView {
const TYPE_NONE = 'none';
public $toolbar = [];
public $bordered = false;
public $export = false;
public $layout = "{items}\n{pager}\n{pageSize}\n{summary}";
public $summaryOptions = [
'class' => 'pull-right'
];
public $panel = [
'heading' => false
];
public $filterSelector = 'select[name="per-page"]';
public $panelFooterTemplate = <<< HTML
<div class="kv-panel-pager">
{pager}
<div class="page-info">
{pageSize}
{summary}
</div>
<div class="clearfix"></div>
</div>
{footer}
<div class="clearfix"></div>
HTML;
public function renderSection($name) {
switch ($name) {
case "{pageSize}":
return $this->renderPageSize();
default:
return parent::renderSection($name);
}
}
public function renderPageSize() {
$pagination = $this->dataProvider->getPagination();
return $pagination !== false ? Html::tag('div', PageSize::widget([
'defaultPageSize' => $pagination->getPageSize()
]), ['class' => 'per-page pull-right']) : null;
}
}
<?php
/**
* PageSize widget is an addition to the \yii\grid\GridView that enables
* changing the size of a page on GridView.
*
* To use this widget with a GridView, add this widget to the page:
*
* ~~~
* <?php echo \nterms\PageSize::widget(); ?>
* ~~~
*
* and set the `filterSelector` property of GridView as shown in
* following example.
*
* ~~~
* <?= GridView::widget([
* 'dataProvider' => $dataProvider,
* 'filterModel' => $searchModel,
* 'filterSelector' => 'select[name="per-page"]',
* 'columns' => [
* ...
* ],
* ]); ?>
* ~~~
*
* Please note that `per-page` here is the string you use for `pageSizeParam` setting of the PageSize widget.
*
* @author Saranga Abeykoon <[email protected]>
* @since 1.0
*/
class PageSize extends \yii\base\Widget
{
/**
* @var integer the defualt page size. This page size will be used when the $_GET['per-page'] is empty.
*/
public $defaultPageSize = 20;
/**
* @var string the name of the GET request parameter used to specify the size of the page.
* This will be used as the input name of the dropdown list with page size options.
*/
public $pageSizeParam = 'per-page';
/**
* @var array the list of page sizes
*/
public $sizes = [10 => 10, 20 => 20, 50 => 50, 100 => 100, 200 => 200];
/**
* @var array the list of options for the drop down list.
*/
public $options;
/**
* Runs the widget and render the output
*/
public function run()
{
if(empty($this->options['id'])) {
$this->options['id'] = $this->id;
}
$perPage = !empty($_GET[$this->pageSizeParam]) ? $_GET[$this->pageSizeParam] : $this->defaultPageSize;
$listHtml = Html::dropDownList($this->pageSizeParam, $perPage, $this->sizes, $this->options);
$output = Yii::t('app', '每页 {listHtml} 则记录', ['listHtml' => $listHtml]);
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment