適應(yīng)于: 已使用layui的頁面 && 想用后端生成頁碼 && 又使用了 webman的 這種illuminate數(shù)據(jù)庫 的情況
<?php
namespace app\services;
class LayuiPaginate
{
// 默認配置
private static $defaultConfig = [
'layout' => ['prev', 'page', 'next', 'count'], // 分頁元素布局
'prev_text' => '上一頁', // 上一頁文本
'next_text' => '下一頁', // 下一頁文本
'count_text' => '共 :total 條', // 總數(shù)文本模板
'ellipsis_text' => '…', // 省略號文本
'first_text' => '首頁', // 首頁文本
'last_text' => '尾頁', // 尾頁文本
'theme' => 'default', // 主題樣式
'show_first_last' => false, // 是否顯示首頁尾頁
'show_ellipsis' => true, // 是否顯示省略號
'show_pages' => 5, // 顯示頁碼數(shù)量
];
private $paginate = null;
private $config = [];
public function __construct($paginate, array $config = [])
{
$this->paginate = $paginate;
$this->paginate->appends(request()->get());
$this->config = array_merge(self::$defaultConfig, $config);
}
public static function make($paginate, array $config = [])
{
return new self($paginate, $config);
}
public function __toString()
{
$html = '<div class="layui-box layui-laypage layui-laypage-' . $this->config['theme'] . '">';
foreach ($this->config['layout'] as $item) {
switch ($item) {
case 'prev':
$html .= $this->renderPrev();
break;
case 'page':
$html .= $this->renderPages();
break;
case 'next':
$html .= $this->renderNext();
break;
case 'count':
$html .= $this->renderCount();
break;
case 'first':
$html .= $this->renderFirst();
break;
case 'last':
$html .= $this->renderLast();
break;
}
}
$html .= '</div>';
return $html;
}
/**
* 渲染上一頁按鈕
*/
protected function renderPrev()
{
if ($this->paginate->onFirstPage()) {
return '<a class="layui-laypage-prev layui-laypage-disabled">' . $this->config['prev_text'] . '</a>';
}
return '<a class="layui-laypage-prev" href="' . $this->paginate->previousPageUrl() . '">' . $this->config['prev_text'] . '</a>';
}
/**
* 渲染頁碼按鈕
*/
protected function renderPages()
{
$html = '';
$currentPage = $this->paginate->currentPage();
$lastPage = $this->paginate->lastPage();
$showPages = $this->config['show_pages'];
// 計算起始和結(jié)束頁碼
$start = max(1, $currentPage - floor($showPages / 2));
$end = min($lastPage, $start + $showPages - 1);
// 調(diào)整起始頁碼
if ($end - $start + 1 < $showPages) {
$start = max(1, $end - $showPages + 1);
}
// 顯示第一頁和省略號
if ($start > 1 && $this->config['show_ellipsis']) {
$html .= '<a href="' . $this->paginate->url(1) . '">1</a>';
if ($start > 2) {
$html .= '<span class="layui-laypage-spr">' . $this->config['ellipsis_text'] . '</span>';
}
}
// 使用 getUrlRange 方法獲取范圍內(nèi)的頁碼URL
foreach ($this->paginate->getUrlRange($start, $end) as $page => $url) {
if ($page == $currentPage) {
$html .= '<span class="layui-laypage-curr"><em class="layui-laypage-em"></em><em>' . $page . '</em></span>';
} else {
$html .= '<a href="' . $url . '">' . $page . '</a>';
}
}
// 顯示最后一頁和省略號
if ($end < $lastPage && $this->config['show_ellipsis']) {
if ($end < $lastPage - 1) {
$html .= '<span class="layui-laypage-spr">' . $this->config['ellipsis_text'] . '</span>';
}
$html .= '<a href="' . $this->paginate->url($lastPage) . '">' . $lastPage . '</a>';
}
return $html;
}
/**
* 渲染下一頁按鈕
*/
protected function renderNext()
{
if (!$this->paginate->hasMorePages()) {
return '<a class="layui-laypage-next layui-laypage-disabled">' . $this->config['next_text'] . '</a>';
}
return '<a class="layui-laypage-next" href="' . $this->paginate->nextPageUrl() . '">' . $this->config['next_text'] . '</a>';
}
/**
* 渲染總數(shù)信息
*/
protected function renderCount()
{
$text = str_replace(':total', $this->paginate->total(), $this->config['count_text']);
return '<span class="layui-laypage-count">' . $text . '</span>';
}
/**
* 渲染首頁按鈕
*/
protected function renderFirst()
{
if (!$this->config['show_first_last'] || $this->paginate->onFirstPage()) {
return '';
}
return '<a class="layui-laypage-first" href="' . $this->paginate->url(1) . '">' . $this->config['first_text'] . '</a>';
}
/**
* 渲染尾頁按鈕
*/
protected function renderLast()
{
if (!$this->config['show_first_last'] || $this->paginate->currentPage() == $this->paginate->lastPage()) {
return '';
}
return '<a class="layui-laypage-last" href="' . $this->paginate->url($this->paginate->lastPage()) . '">' . $this->config['last_text'] . '</a>';
}
}
我的使用的部分示例:
function index(Request $request){
$limit=$request->get('limit',8);
$model = new XXXModel();
//
$min_price = $request->get("min_price", '');
if (is_numeric($min_price)) {
$model = $model->where("price", '>=', $min_price);
}
//
$category = $request->get("category", '');
if ($category) {
$model = $model->where("category", '=', $category);
}
//省略 ...
$paginate = $model->paginate($limit);
return $this->layout('', [
'items' => $paginate->items(),
'pager'=>\app\services\LayuiPaginate::make($paginate),//這里這里這里
"category" => $category,
"keyword" => $keyword,
'min_price' => $min_price,
//...
]);
}
html部分:
<div>
<?=var_dump($items)?>
</div>
<div class="layui-row">
<div class="layui-col-md12" style="text-align: center; margin-top: 30px;">
<?=$pager?><!--這里這里這里-->
</div>
</div>
get參數(shù)有很多:
這是鼠標放上去的 效果截圖:
分享webman admin 字典 修改 頁面,添加排序調(diào)整:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>更新字典</title>
<link rel="stylesheet" href="/app/admin/component/layui/css/layui.css?v=2.8.12" />
<link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
<link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
</head>
<body>
<style>
.mainBox {
width: auto !important;
}
.layui-tab .layui-table-cell {
overflow:visible !important;
}
.layui-table-body ,.layui-table-box{
overflow:visible !important;
}
.layui-tab .layui-form-select dl {
max-height: 190px;
}
.layui-table-body .layui-table-col-special:last-child {
width: 100% !important;
border-right: 1px solid #eee !important;
}
xm-select {
min-height: 38px;
line-height: 38px;
}
xm-select .xm-body .xm-option .xm-option-icon {
font-size: 18px !important;
}
</style>
<form class="layui-form" action="" lay-filter="create-dict-form">
<div class="mainBox">
<div class="main-container mr-5">
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label required" style="width:auto">字典名</label>
<div class="layui-input-inline">
<input type="text" name="name" required lay-verify="required" disabled autocomplete="off" class="layui-input" placeholder="請輸入英文字母組合">
</div>
</div>
</div>
<div>
<!-- 字段屬性 -->
<table id="column-table" lay-filter="column-table"></table>
<script type="text/html" id="column-toolbar">
<button type="button" class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
<i class="layui-icon layui-icon-add-1"></i>新增
</button>
<button type="button" class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
<i class="layui-icon layui-icon-delete"></i>刪除
</button>
</script>
<script type="text/html" id="col-value">
<input type="text" name="value[{{ d.LAY_NUM-1 }}][value]" placeholder="值" autocomplete="off" class="layui-input" value="{{ d.value }}">
<input type="hidden" name="value[{{ d.LAY_NUM-1 }}][_field_id]" value="{{ d._field_id }}">
</script>
<script type="text/html" id="col-name">
<input type="text" name="value[{{ d.LAY_NUM-1 }}][name]" placeholder="標題" autocomplete="off" class="layui-input" value="{{ d.name }}">
</script>
</div>
</div>
</div>
<div class="bottom">
<div class="button-container">
<button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit=""
lay-filter="save">
提交
</button>
<button type="reset" class="pear-btn pear-btn-md">
重置
</button>
</div>
</div>
</form>
<script type="text/html" id="barDemo">
<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="up">向上</a>
</script>
<script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
<script src="/app/admin/component/pear/pear.js"></script>
<script src="/app/admin/admin/js/permission.js"></script>
<script>
const DICT_NAME = layui.url().search.name;
const UPDATE_API = "/app/admin/dict/update";
const SELECT_API = "/app/admin/dict/get/" + DICT_NAME;
// 字段設(shè)置
layui.use(["table", "common", "popup"], function () {
let table = layui.table;
let common = layui.common;
let $ = layui.$;
let cols = [
{
type: "checkbox",
width: 50,
},
{
title: "值",
field: "value",
templet: "#col-value"
},
{
title: "標題",
field: "name",
templet: "#col-name",
},
{
toolbar:'#barDemo',
title:'操作',
width:100
}
];
$('input[name="name"]').val(DICT_NAME);
window._field_id = 0;
let data = [];
$.ajax({
url: SELECT_API,
dataType: "json",
async: false,
success: function (res) {
data = res.data;
layui.each(data, function (k, v) {
data[k]["_field_id"] = _field_id++;
})
// ajax產(chǎn)生錯誤
if (res.code) {
layui.popup.failure(res.msg);
}
}
});
table.render({
elem: "#column-table",
cols: [cols],
data: data,
cellMinWidth: 40,
skin: "line",
size: "lg",
limit: 10000,
page: false,
toolbar: "#column-toolbar",
defaultToolbar: [],
});
table.on('tool(column-table)', function(obj){
if(obj.event === 'up'){
if(obj.index){
var temp = {
...data[obj.index]
};
data[obj.index].name = data[obj.index-1].name;
data[obj.index].value = data[obj.index-1].value;
data[obj.index-1].name = temp.name;
data[obj.index-1].value = temp.value;
table.reloadData("column-table", {data: data});
}
}
});
table.on("toolbar(column-table)", function(obj) {
if (obj.event === "add") {
add();
} else if (obj.event === "batchRemove") {
batchRemove(obj);
}
});
let add = function() {
syncTableData();
let options = table.getData("column-table");
options.push({
_field_id: _field_id++,
value : "",
name: "",
});
table.reloadData("column-table", {data:options});
}
let batchRemove = function(obj) {
var checkIds = common.checkField(obj,"_field_id");
if (checkIds === "") return layui.popup.warning("未選中數(shù)據(jù)");
let data = table.getData("column-table");
let newData = [];
let deleteIds = checkIds.split(",");
layui.each(data, function (index, item) {
if (deleteIds.indexOf(item._field_id + "") === -1) {
newData.push(item);
}
});
table.reloadData("column-table", {data: newData})
}
});
layui.use(["form", "popup"], function () {
//提交事件
layui.form.on("submit(save)", function () {
let data = layui.form.val("create-dict-form");
layui.$.ajax({
url: UPDATE_API,
type: "POST",
dateType: "json",
data: data,
success: function (res) {
if (res.code) {
return layui.popup.failure(res.msg);
}
return layui.popup.success("操作成功", function () {
parent.refreshTable();
parent.layer.close(parent.layer.getFrameIndex(window.name));
});
}
});
return false;
});
});
window.syncTableData = function () {
let tableData = layui.form.val("create-dict-form");
let columnTableData = [];
let len = Object.keys(tableData).length;
let id = 0;
window._key_id = 0;
while (id < len) {
// column data
if (typeof tableData["value[" + id + "][_field_id]"] !== "undefined") {
columnTableData.push({
_field_id: tableData["value[" + id + "][_field_id]"],
name : tableData["value[" + id + "][name]"],
value: tableData["value[" + id + "][value]"],
});
}
_key_id++;
id++;
}
layui.table.reloadData("column-table", {data: columnTableData});
}
</script>
</body>
</html>
感謝分享