PHPcodeigniter4 & bootstrap5-table
MeteorCat这部分是作为 PHP 做集合 bootstrap5/bootstrap-table 做服务端渲染的功能
如果有能力的话建议赞助 bootstrap-table 项目, 强力推荐的 bootstrap 扩展库
项目地址: https://github.com/wenzhixin/bootstrap-table
bootstrap-table 按照官方文档引入方式如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>bootstrap-table 分页</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/bootstrap-table.min.css"> </head> <body>
<table id="table"> <thead> <tr> <th data-field="id"> ID </th> <th data-field="name"> Item Name </th> <th data-field="price"> Item Price </th> </tr> </thead> </table>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bootstrap-table.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/locale/bootstrap-table-zh-CN.min.js"></script>
<script>
const data = [ { id: 0, name: 'Item 0', _name_rowspan: 2, price: '$0' }, { id: 1, price: '$1' }, { id: 2, name: 'Item 2', price: '$2' }, { id: 3, name: 'Item 3', price: '$3' }, { id: 4, name: 'Item 4', price: '$4' }, { id: 5, name: 'Item 5', price: '$5' } ];
window.addEventListener('load', () => { const $table = $('#table'); $table.bootstrapTable({data}) });
</script> </body> </html>
|
参考文档: https://examples.bootstrap-table.com
这里提供几个比较常用的初始化功能, 方便加深对 bootstrap-table 的日常使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
<table id="table" data-toggle="table" data-show-columns="true" data-show-refresh="true" data-classes="table table-bordered table-hover text-center" data-height="460" data-side-pagination="server" data-url="https://data.meteorcat.me/data1.json" data-ajax="ajaxRequest" data-ajax-options="ajaxOptions" data-method="post" data-page-list="[5, 10, 20, 50, 100, 200]" data-pagination="true" data-data-type="json" data-data-field="rows" data-total-field="total" data-query-params="ajaxQueryParams" > <thead> <tr> <th data-field="id" data-sortable="true" data-sort-name="uid" data-formatter="uidFormatter" > ID </th> <th data-field="name"> Item Name </th> <th data-field="price"> Item Price </th> </tr> </thead> </table> <script>
window.ajaxRequest = params => { const url = 'https://examples.wenzhixin.net.cn/examples/bootstrap_table/data'
$.get(`${url}?${$.param(params.data)}`).then(function (res) { params.success(res) }) }
window.ajaxOptions = { beforeSend(xhr) { xhr.setRequestHeader('custom-auth-token', 'custom-auth-token') } }
window.ajaxQueryParams = params => { params.uid = 10001; return params; }
window.uidFormatter = function (value, row) { return `<span style="color:green">${value}</span>` } </script>
|
默认采用数据库直接 page-offset[(limit 0, 5) 查询方式, 不用换算页码], 具体分页数据参考 JSON 模板
lines1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| { "total": 800, "totalNotFiltered": 800, "rows": [ { "id": 10, "name": "Item 10", "price": "$10" }, { "id": 11, "name": "Item 11", "price": "$11" } ] }
|
响应数据比较重要的是 row(数据列表) 和 total(数据条数), 之后就是调用事件功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
function Created() { $('#table').bootstrapTable('destroy'); $('#table').bootstrapTable(); }
function Refresh() { $('#table').bootstrapTable('refresh'); }
function Config(options = {}) { $('#table').bootstrapTable('refreshOptions', options); }
|
表格初始化差不多是这样就行了, 这就是基本使用方法
注意: 设置定义的回调函数位置必须在 BootstrapTable 初始化之前, 否则可能无法获取到全局 window.* 方法