Skip to content

Commit a809709

Browse files
committed
Widget view option
1 parent 03d7638 commit a809709

File tree

7 files changed

+106
-13
lines changed

7 files changed

+106
-13
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor
2+
composer.phar
3+
composer.lock

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ echo Pagination::widget([
236236
237237
#### Customized View
238238

239-
You could customized your pagination view then set into Pagination Widget.
239+
The default widget view is for Bootstrap(`bootstrap`), you could customized your pagination view then set into Pagination Widget.
240240

241241
```php
242242
use yidas\widgets\Pagination;
@@ -247,6 +247,8 @@ echo Pagination::widget([
247247
]);
248248
```
249249

250+
> You could also choose library's template view: `bootstrap`, `simple`
251+
250252
#### Inheritance
251253

252254
You could build your application Pagination Widget with styles Inherited from `yidas\widgets\Pagination`. For example:
@@ -424,6 +426,7 @@ API DOCUMENTATION
424426
|$pageCssClass|string |The CSS class for the each page button, default value is `page-item`|
425427
|$ulCssClass |string |The CSS class for the ul element of pagination. For example, 'pagination-sm' for Bootstrap small size.|
426428
|$linkAttributes|array |HTML attributes for the link in a pager container tag, default value is ['class' => 'page-link']|
429+
|$view |string |The view name or absolute file path that can be used to render view. (Template: `bootstrap`, `simple`)|
427430

428431
---
429432

src/widgets/Pagination.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ class Pagination
9797
/**
9898
* HTML attributes for the link in a pager container tag
9999
*
100-
* @var array
100+
* @var array ['{attribute}' => '{value}']
101101
*/
102-
public $linkAttributes = ['class' => 'page-link'];
102+
public $linkAttributes = [];
103103

104104
/**
105105
* The CSS class for the ul element of pagination
@@ -109,6 +109,15 @@ class Pagination
109109
* @var string
110110
*/
111111
public $ulCssClass = '';
112+
113+
/**
114+
* The view name or absolute file path that can be used to render view.
115+
*
116+
* Template: 'bootstrap', 'simple'
117+
*
118+
* @var string view name or absolute file path
119+
*/
120+
public $view = 'bootstrap';
112121

113122
/**
114123
* Default options
@@ -227,7 +236,12 @@ public function run()
227236
$isFirst = ($page<=1) ? true : false;
228237
$isLast = ($this->pagination->pageCount<=$page) ? true : false;
229238

230-
include __DIR__ . '/views/bootstrap.php';
239+
// Choose view
240+
$viewFile = (in_array(substr($this->view, 0, 1), [DIRECTORY_SEPARATOR, '/']))
241+
? $this->view
242+
: __DIR__ . "/views/{$this->view}.php";
243+
// Render view
244+
include $viewFile;
231245

232246
return;
233247
}

src/widgets/views/bootstrap.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,37 @@
33
<?php if($this->firstPageLabel):?>
44
<li class="<?=$this->pageCssClass?> <?=$this->firstPageCssClass?> <?php if($isFirst):?>disabled<?php endif ?>">
55
<?php if($isFirst):?>
6-
<span <?=$linkAttributes?>><?=$this->firstPageLabel?></span>
6+
<span <?=$linkAttributes?> class="page-link"><?=$this->firstPageLabel?></span>
77
<?php else: ?>
8-
<a <?=$linkAttributes?> href="<?=$this->pagination->createUrl(1);?>"><?=$this->firstPageLabel?></a>
8+
<a <?=$linkAttributes?> class="page-link" href="<?=$this->pagination->createUrl(1);?>"><?=$this->firstPageLabel?></a>
99
<?php endif ?>
1010
</li>
1111
<?php endif ?>
1212
<li class="<?=$this->pageCssClass?> <?=$this->prevPageCssClass?> <?php if($isFirst):?>disabled<?php endif ?>">
1313
<?php if($isFirst):?>
14-
<span <?=$linkAttributes?>><?=$this->prevPageLabel?></span>
14+
<span <?=$linkAttributes?> class="page-link"><?=$this->prevPageLabel?></span>
1515
<?php else: ?>
16-
<a <?=$linkAttributes?> href="<?=$this->pagination->createUrl($page-1);?>"><?=$this->prevPageLabel?></a>
16+
<a <?=$linkAttributes?> class="page-link" href="<?=$this->pagination->createUrl($page-1);?>"><?=$this->prevPageLabel?></a>
1717
<?php endif ?>
1818
</li>
1919
<?php foreach ($this->_buttonStack as $key => $btnPage): ?>
2020
<li class="<?=$this->pageCssClass?> <?php if(($page)==$btnPage):?>active<?php endif ?>">
21-
<a <?=$linkAttributes?> href="<?=$this->pagination->createUrl($btnPage);?>"><?=$btnPage?></a>
21+
<a <?=$linkAttributes?> class="page-link" href="<?=$this->pagination->createUrl($btnPage);?>"><?=$btnPage?></a>
2222
</li>
2323
<?php endforeach ?>
2424
<li class="<?=$this->pageCssClass?> <?=$this->nextPageCssClass?> <?php if($isLast):?>disabled<?php endif ?>">
2525
<?php if($isLast):?>
26-
<span <?=$linkAttributes?>><?=$this->nextPageLabel?></span>
26+
<span <?=$linkAttributes?> class="page-link"><?=$this->nextPageLabel?></span>
2727
<?php else: ?>
28-
<a <?=$linkAttributes?> href="<?=$this->pagination->createUrl($page+1);?>"><?=$this->nextPageLabel?></a>
28+
<a <?=$linkAttributes?> class="page-link" href="<?=$this->pagination->createUrl($page+1);?>"><?=$this->nextPageLabel?></a>
2929
<?php endif ?>
3030
</li>
3131
<?php if($this->lastPageLabel):?>
3232
<li class="<?=$this->pageCssClass?> <?=$this->lastPageCssClass?> <?php if($isLast):?>disabled<?php endif ?>">
3333
<?php if($isLast):?>
34-
<span <?=$linkAttributes?>><?=$this->lastPageLabel?></span>
34+
<span <?=$linkAttributes?> class="page-link"><?=$this->lastPageLabel?></span>
3535
<?php else: ?>
36-
<a <?=$linkAttributes?> href="<?=$this->pagination->createUrl($this->pagination->pageCount);?>"><?=$this->lastPageLabel?></a>
36+
<a <?=$linkAttributes?> class="page-link" href="<?=$this->pagination->createUrl($this->pagination->pageCount);?>"><?=$this->lastPageLabel?></a>
3737
<?php endif ?>
3838
</li>
3939
<?php endif ?>

src/widgets/views/simple.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<div class="pagination">
2+
<?php if($this->firstPageLabel):?>
3+
<?php if($isFirst):?>
4+
<span <?=$linkAttributes?>><?=$this->firstPageLabel?></span>
5+
<?php else: ?>
6+
<a <?=$linkAttributes?> href="<?=$this->pagination->createUrl(1);?>"><?=$this->firstPageLabel?></a>
7+
<?php endif ?>
8+
<?php endif ?>
9+
10+
<?php if($isFirst):?>
11+
<span <?=$linkAttributes?>><?=$this->prevPageLabel?></span>
12+
<?php else: ?>
13+
<a <?=$linkAttributes?> href="<?=$this->pagination->createUrl($page-1);?>"><?=$this->prevPageLabel?></a>
14+
<?php endif ?>
15+
16+
<?php foreach ($this->_buttonStack as $key => $btnPage): ?>
17+
<a <?=$linkAttributes?> class="<?php if(($page)==$btnPage):?>active<?php endif ?>" href="<?=$this->pagination->createUrl($btnPage);?>"><?=$btnPage?></a>
18+
<?php endforeach ?>
19+
20+
<?php if($isLast):?>
21+
<span <?=$linkAttributes?>><?=$this->nextPageLabel?></span>
22+
<?php else: ?>
23+
<a <?=$linkAttributes?> href="<?=$this->pagination->createUrl($page+1);?>"><?=$this->nextPageLabel?></a>
24+
<?php endif ?>
25+
26+
<?php if($this->lastPageLabel):?>
27+
<?php if($isLast):?>
28+
<span <?=$linkAttributes?>><?=$this->lastPageLabel?></span>
29+
<?php else: ?>
30+
<a <?=$linkAttributes?> href="<?=$this->pagination->createUrl($this->pagination->pageCount);?>"><?=$this->lastPageLabel?></a>
31+
<?php endif ?>
32+
<?php endif ?>
33+
</div>

test/_config.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
// Debug
3+
error_reporting(E_ALL);
4+
ini_set("display_errors", 1);
5+
// Vendor
6+
$vendorPath = __DIR__ . '/../vendor/autoload.php';
7+
if (!file_exists(__DIR__ . '/../vendor/autoload.php')) {
8+
die('composer install for root directory required');
9+
}
10+
require __DIR__ . '/../vendor/autoload.php';

test/test.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
require __DIR__ . '/_config.php';
4+
5+
// Fake data
6+
$count = 1000;
7+
8+
$pagination = new \yidas\data\Pagination([
9+
'totalCount' => $count,
10+
]);
11+
12+
?>
13+
14+
<!DOCTYPE html>
15+
<html>
16+
<head>
17+
<meta charset="utf-8" />
18+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
19+
<title>Page Title</title>
20+
<meta name="viewport" content="width=device-width, initial-scale=1">
21+
</head>
22+
<body>
23+
<div>
24+
<?=\yidas\widgets\Pagination::widget([
25+
'pagination' => $pagination,
26+
// 'view' => 'simple',
27+
])?>
28+
</div>
29+
</body>
30+
</html>

0 commit comments

Comments
 (0)