|
1 | | -#BITDataTable : jQuery DataTables for Laravel 5 |
| 1 | +# BITDataTable : jQuery DataTables for Laravel |
2 | 2 |
|
| 3 | +This package is created to handle server-side works of DataTables jQuery Plugin via AJAX option by using Eloquent ORM / Query Builder. |
| 4 | + |
| 5 | +## Quick Installation |
3 | 6 | ``` |
4 | 7 | composer require bitstudio-id/bitdatatable |
5 | 8 | ``` |
6 | 9 |
|
7 | 10 | ## Requirements |
8 | | -- [PHP >= 7.0](http://php.net/) |
| 11 | +- [PHP >= 5.6.4](http://php.net/) |
9 | 12 | - [Laravel >= 5.4](https://github.com/laravel/framework) |
10 | | -- [jQuery DataTables v1.10.x](http://datatables.net/) |
| 13 | +- [jQuery DataTables v1.10.x](http://datatables.net/) |
| 14 | + |
| 15 | + |
| 16 | +## Javascript and CSS |
| 17 | +```html |
| 18 | +<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css"/> |
| 19 | +<link rel="stylesheet" href="//cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css"/> |
| 20 | + |
| 21 | +<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> |
| 22 | +<script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script> |
| 23 | +<script src="//cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script> |
| 24 | +``` |
| 25 | + |
| 26 | +## Blade view |
| 27 | +```html |
| 28 | +<table id="table" class="table table-bordered table-striped"> |
| 29 | + <thead> |
| 30 | + <tr> |
| 31 | + <th>#ID</th> |
| 32 | + <th>Name</th> |
| 33 | + <th>Role</th> |
| 34 | + <th>Email</th> |
| 35 | + <th>...</th> |
| 36 | + </tr> |
| 37 | + </thead> |
| 38 | + <tbody></tbody> |
| 39 | + </table> |
| 40 | +``` |
| 41 | + |
| 42 | +## Javascript |
| 43 | +``` |
| 44 | +<script> |
| 45 | + $(document).ready(function () { |
| 46 | + //hide error/warning on datatable |
| 47 | + $.fn.dataTable.ext.errMode = 'none'; |
| 48 | + |
| 49 | + var table = $('#table').DataTable({ |
| 50 | + //enable filter |
| 51 | + |
| 52 | + bFilter: true, |
| 53 | + processing: true, |
| 54 | + serverSide: true, |
| 55 | + ajax: { |
| 56 | + url: "/dummy/dtb-v2/get", |
| 57 | + type: 'get', |
| 58 | + }, |
| 59 | + columns: [ |
| 60 | + {data: "id", name: "id", searchable: false, orderable: false}, |
| 61 | + {data: "employee.code"}, |
| 62 | + {data: "name", name: "user_name"}, |
| 63 | + {data: "employee.role.name"}, |
| 64 | + {data: "email"}, |
| 65 | + {data: "action", searchable: false, orderable: false}, // use searchable: false, orderable: false for custom column |
| 66 | + ], |
| 67 | + }); |
| 68 | + }); |
| 69 | +</script> |
| 70 | +``` |
| 71 | + |
| 72 | +## How to use with eloquent |
| 73 | +```php |
| 74 | +use BITStudio\BITDataTable\BITDataTable; |
| 75 | +... |
| 76 | +... |
| 77 | +public function dtbGetV2(Request $request) |
| 78 | +{ |
| 79 | + $dtb = new BITDataTable(); |
| 80 | + |
| 81 | + // Set request |
| 82 | + $dtb->setRequest($request); |
| 83 | + |
| 84 | + $user = User::query()->with('employee', 'employee.role'); |
| 85 | + |
| 86 | + $dtb->from($user); |
| 87 | + |
| 88 | + $state = "admin"; |
| 89 | + |
| 90 | + $dtb->addCol(function ($user){ |
| 91 | + $user->action = "<a target='_blank' href='//lorem.com/{$user->id}' class='btn btn-danger'>action-{$item->id}</a>"; |
| 92 | + return $user; |
| 93 | + }); |
| 94 | + |
| 95 | + return $dtb->generate(); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## How to use logic on addCol |
| 100 | +``` |
| 101 | +$state = "admin"; |
| 102 | +
|
| 103 | +$dtb->addCol(function ($user) use ($state){ |
| 104 | + //use logic on addCol |
| 105 | + |
| 106 | + //set as empty default |
| 107 | + $user->admin_col = ""; |
| 108 | + |
| 109 | + if($state == $user->role->name) { |
| 110 | + $user->admin_col .= "admin-col"; |
| 111 | + } |
| 112 | + return $user; |
| 113 | +}); |
| 114 | +``` |
| 115 | + |
| 116 | +## How to use with Query Builder |
| 117 | +```php |
| 118 | +use BITStudio\BITDataTable\BITDataTable; |
| 119 | +... |
| 120 | +... |
| 121 | +public function dtbGetV2(Request $request) |
| 122 | + $dtb = new BITDataTable(); |
| 123 | + |
| 124 | + $dtb->setRequest($request); |
| 125 | + |
| 126 | + $q = DB::table("orders as o"); |
| 127 | + $q->select("o.*", "o.no_cs as customer_number", "e.employee_name as emp_name"); |
| 128 | + $q->leftJoin("employee as e", "e.id", "=", "o.employee_id"); |
| 129 | + |
| 130 | + $dtb->from($q); |
| 131 | + |
| 132 | + |
| 133 | + //add custom column |
| 134 | + $dtb->addCol(function ($user){ |
| 135 | + $user->action = "<a target='_blank' href='//google.com/{$item->id}' class='btn btn-danger'>action-{$item->id}</a>"; |
| 136 | + |
| 137 | + return $user; |
| 138 | + }); |
| 139 | + |
| 140 | + return $dtb->generate(); |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +### How to show index number for numbering on view |
| 145 | +``` |
| 146 | +$dtb->setRowIndex(true); |
| 147 | +``` |
| 148 | +this will append property DT_RowIndex on json response |
| 149 | +``` |
| 150 | +columns: [ |
| 151 | + {data: "DT_RowIndex", name: "id" searchable: false, orderable: false}, |
| 152 | + ... |
| 153 | + ] |
| 154 | +``` |
| 155 | +dont forget to set searchable = false |
| 156 | + |
| 157 | +#### add value class attribute |
| 158 | +``` |
| 159 | +$dtb->addClass("text-danger"); //insert before genereate |
| 160 | +``` |
| 161 | + |
| 162 | +#### add value id attribute |
| 163 | +``` |
| 164 | +//create custom from collection property |
| 165 | +$dtb->setRowId("id"); |
| 166 | +
|
| 167 | +//create custom from addCol or setRowId for custom id attribute |
| 168 | +$dtb->setRowId(function($item) { |
| 169 | + $item->DT_RowId = "id-".$item->id; |
| 170 | + return $item; |
| 171 | +}); |
| 172 | +``` |
| 173 | +## License |
| 174 | +The MIT License (MIT). Please see License File for more information. |
0 commit comments