begien / simplepaginator
简单分页器
1.0.0
2023-11-02 11:14 UTC
Requires
- php: >=7.2.0
Requires (Dev)
- phpunit/phpunit: 8.0.*
This package is auto-updated.
Last update: 2024-10-01 00:20:44 UTC
README
这是一个用于显示简单分页器的库。
使用composer安装
$ composer require begien/simplepaginator:~1.0
或者
composer.json
{ "require": { "begien/simplepaginator": "~1.0" } }
$ composer install
示例
<?php require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/config/config.php'; use Begien\Paginator; // 👈 このパッケージをインポートします $pdo = connectDb(); // 👈 PDOインスタンス $result_view_count = 2; // 👈 検索結果の最大表示数 $paginate_margin = 2; // 👈 ページネータで表示するボタンの余白の数 if (isset($_GET['max'])) { $result_view_count = (int)h($_GET['max']); } $sql = 'select * from users order by id asc'; // 👈 "LIMIT"以外のsqlを作成 // 👇 ページネータ取得 $paginator = new Paginator( $pdo, $sql, $result_view_count, $paginate_margin, ); $result = $paginator->result; // 👈 検索結果 $result_count = $paginator->result_count // 👈 ヒットしたデータの数 ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>ページネーションテスト</title> </head> <body> <style> .paginate_button { margin: 0 auto; } </style> <div> <?php echo $result_count; ?>件ヒットしました。 </div> <form action="" method="get" name="form"> <input type="hidden" name="page" value=""><!-- 👈 submitするフォームにhiddenで指定してください --> <label for="max">表示件数</label> <select id="max" name="max"> <?php for ($i = 1;5 > $i;++$i) : ?> <option value="<?php echo $i; ?>" <?php if ((int)$result_view_count === (int)$i) :?> selected="selected" <?php endif; ?> > <?php echo $i; ?></option> <?php endfor; ?> </select> </form> <ul> <?php foreach ($result as $key => $user) : ?> <li><?php echo $user['name'] ?></li> <?php endforeach; ?> </ul> <?php $paginator->paginate(); ?><!-- 👈 ページネータを表示します --> <script> let max = document.getElementById('max'); let max_form = document.querySelector('[name="form"]'); max.addEventListener('change', function (e) { max_form.submit(); }); </script> </body> </html>
显示分页器
<?php $paginator->paginate(); ?>
显示分页器
需要的标签
<form name="form" method="get">
点击分页器的按钮时,会在模板内的form中查找并执行提交处理。
默认情况下,会搜索并提交带有name="form"的form。
<input type="hidden" name="page" value="">
必须指定一个input来设置按钮的数字,该input将用于点击按钮时。
默认情况下,会搜索带有name="page"的input并设置其value。
选项
/** * @var array $options{ * paginate_path: string, * visible_prev_next: bool, * visible_start_end: bool, * form_name: string, * page_name: string, * background_color: array{ * default: string, * selected: string, * }, * color: array{ * default: string, * selected: string, * }, * } */ $options = Paginator::getDefaultOptions(); // 👈 デフォルトのオプションを取得できます
-
paginate_path
如果您想使用除了默认模板以外的模板,请指定该模板的路径。
默认情况下,已指定了此库提供的模板路径。
以下是默认模板。在创建自定义模板时请参考。
<?php $is_visible_next = false; $is_visible_prev = false; $is_visible_start = false; $is_visible_end = false; ?> <style> .begien_paginate_wrapper button { border: 1px solid; border-radius: 5px; cursor: pointer; background-color: <?php echo $this->background_color['default']; ?>; color: <?php echo $this->color['default']; ?>; } .begien_paginate_wrapper button.selected { background-color: <?php echo $this->background_color['selected']; ?>; color: <?php echo $this->color['selected']; ?>; } </style> <?php if ($this->count > 0) :?> <div class="begien_paginate_wrapper"> <?php for ($i = 1;$this->count >= $i;++$i) : ?> <?php if ($this->page > 1) : ?> <?php if ($this->visible_start_end && !$is_visible_start) : ?> <?php $is_visible_start = true; ?> <button type="button" value="<?php echo 1; ?>" class="paginate_button" > << </button> <?php endif; ?> <?php if ($this->visible_prev_next && !$is_visible_prev) : ?> <?php $is_visible_prev = true; ?> <button type="button" value="<?php echo $this->page - 1; ?>" class="paginate_button " > < </button> <?php endif; ?> <?php endif;?> <?php if (($this->page - $this->margin) > $i) : ?> <?php continue; ?> <?php elseif (($this->page + $this->margin) < $i) : ?> <?php break; ?> <?php endif; ?> <button type="button" value="<?php echo $i; ?>" class=" paginate_button <?php if ((int)$this->page === (int)$i) :?> selected <?php endif; ?> " > <?php echo $i; ?> </button> <?php endfor; ?> <?php if ($this->page < $this->count) : ?> <?php if ($this->visible_prev_next && !$is_visible_next) : ?> <?php $is_visible_next = true; ?> <button type="button" value="<?php echo $this->page + 1; ?>" class="paginate_button" > > </button> <?php endif; ?> <?php if ($this->visible_start_end && !$is_visible_end) : ?> <?php $is_visible_end = true; ?> <button type="button" value="<?php echo $this->count; ?>" class="paginate_button" > >> </button> <?php endif; ?> <?php endif; ?> </div> <?php endif; ?> <script> let buttons = document.getElementsByClassName('paginate_button'); let page_input = document.querySelector('[name="<?php echo $this->page_name; ?>"]'); for (let i = 0;buttons.length > i;++i) { buttons[i].addEventListener('click', function (e) { let page = e.target.value; let form = document.querySelector('[name="<?php echo $this->form_name; ?>"]'); if (!form) { form = this.closest('form') ?? document.querySelector('form'); } page_input.value = page; form.submit(); }); } </script>
-
visible_prev_next
指定是否显示分页器的"上一页"和"下一页"。
默认: true -
visible_start_end
指定是否显示分页器的"第一页"和"最后一页"。
默认: true -
form_name
指定点击分页器按钮时想要提交的
<form>
的name。
默认: "form" -
page_name
指定用于设置分页器按钮value的<input>
的name。 -
默认: "page"
background_color
指定分页器按钮的背景色。
默认: 未选择时的颜色
默认: buttonface
选择: aqua -
color
background_color
指定分页器按钮的背景色。
默认: 未选择时的颜色
指定分页器按钮数字的颜色。
默认: black
LICENCE
MIT
作者
https://github.com/kamotetu
https://begien.com
如果您愿意玩玩,我将很高兴。