bitstudio-id/bitdatatable
Composer 安装命令:
composer require bitstudio-id/bitdatatable
包简介
A simple way to implement datatable with query builder
README 文档
README
This package is created to handle server-side works of DataTables jQuery Plugin via AJAX option by using Eloquent ORM / Query Builder.
Quick Installation
composer require bitstudio-id/bitdatatable
Requirements
Javascript and CSS
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css"/> <link rel="stylesheet" href="//cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css"/> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script> <script src="//cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
Blade view
<table id="table" class="table table-bordered table-striped"> <thead> <tr> <th>#ID</th> <th>Name</th> <th>Role</th> <th>Email</th> <th>...</th> </tr> </thead> <tbody></tbody> </table>
Javascript
<script>
$(document).ready(function () {
//hide error/warning on datatable
$.fn.dataTable.ext.errMode = 'none';
var table = $('#table').DataTable({
//enable filter
bFilter: true,
processing: true,
serverSide: true,
ajax: {
url: "/dummy/dtb-v2/get",
type: 'get',
},
columns: [
{data: "id", name: "id", searchable: false, orderable: false},
{data: "employee.code"},
{data: "name", name: "user_name"},
{data: "employee.role.name"},
{data: "email"},
{data: "action", searchable: false, orderable: false}, // use searchable: false, orderable: false for custom column
],
});
});
</script>
How to use with eloquent
use BITStudio\BITDataTable\BITDataTable; ... ... public function dtbGetV2(Request $request) { $dtb = new BITDataTable(); // Set request $dtb->setRequest($request); $user = User::query()->with('employee', 'employee.role'); $dtb->from($user); $state = "admin"; $dtb->addCol(function ($user){ $user->action = "<a target='_blank' href='//lorem.com/{$user->id}' class='btn btn-danger'>action-{$item->id}</a>"; return $user; }); return $dtb->generate(); }
How to use logic on addCol
$state = "admin";
$dtb->addCol(function ($user) use ($state){
//use logic on addCol
//set as empty default
$user->admin_col = "";
if($state == $user->role->name) {
$user->admin_col .= "admin-col";
}
return $user;
});
How to use with Query Builder
use BITStudio\BITDataTable\BITDataTable; ... ... public function dtbGetV2(Request $request) $dtb = new BITDataTable(); $dtb->setRequest($request); $q = DB::table("orders as o"); $q->select("o.*", "o.no_cs as customer_number", "e.employee_name as emp_name"); $q->leftJoin("employee as e", "e.id", "=", "o.employee_id"); $dtb->from($q); //add custom column $dtb->addCol(function ($user){ $user->action = "<a target='_blank' href='//google.com/{$item->id}' class='btn btn-danger'>action-{$item->id}</a>"; return $user; }); return $dtb->generate(); }
How to show index number for numbering on view
$dtb->setRowIndex(true);
this will append property DT_RowIndex on json response
columns: [
{data: "DT_RowIndex", name: "id" searchable: false, orderable: false},
...
]
dont forget to set searchable = false
add value class attribute
$dtb->addClass("text-danger"); //insert before genereate
add value id attribute
//create custom from collection property
$dtb->setRowId("id");
//create custom from addCol or setRowId for custom id attribute
$dtb->setRowId(function($item) {
$item->DT_RowId = "id-".$item->id;
return $item;
});
License
The MIT License (MIT). Please see License File for more information.
bitstudio-id/bitdatatable 适用场景与选型建议
bitstudio-id/bitdatatable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 236 次下载、GitHub Stars 达 6, 最近一次更新时间为 2019 年 07 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bitstudio-id/bitdatatable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bitstudio-id/bitdatatable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 236
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-07-12