Skip to content

Commit e5bdde1

Browse files
author
Adam Segal
committed
Update readme with an extra example for dataFullSupport.
1 parent bc2ad67 commit e5bdde1

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,80 @@ return Datatables::of($todo)
169169
->make();
170170
```
171171

172+
**Example 6: Advanced usage of dataFullSupport**
173+
174+
To better utilize dataTables mData (1.9), now columns.data (1.10) feature you may enable dataFullSupport by either setting it to true in the config file, or passing true to the second initialization argument `Datatables::of($query, true)`
175+
176+
Creating a table with a searchable and sortable joined table:
177+
```javascript
178+
//html
179+
<table id="user-list"></table>
180+
//script.js
181+
$("#users-list").dataTable({
182+
"processing": true,
183+
"serverSide": true,
184+
"ajax": "/api/user/datatables",
185+
"order": [[1,'desc']],
186+
"columnDefs": [ { //this prevents errors if the data is null
187+
"targets": "_all",
188+
"defaultContent": ""
189+
} ],
190+
"columns": [
191+
//title will auto-generate th columns
192+
{ "data" : "id", "title" : "Id", "orderable": true, "searchable": false },
193+
{ "data" : "profile.last_name","title" : "Name", "orderable": true, "searchable": true },
194+
{ "data" : "username", "title" : "Username", "orderable": true, "searchable": true },
195+
{ "data" : "email", "title" : "Email", "orderable": true, "searchable": true },
196+
{ "data" : "created_date", "title" : "Created", "orderable": true, "searchable": true },
197+
]
198+
});
199+
```
200+
201+
```php
202+
$users = Models\User::select()->ModelJoin('profile');
203+
return $dataTables = Datatables::of($users)
204+
->filter_column('profile.last_name','where',\DB::raw('CONCAT(profile.last_name,\' \',profile.first_name'),'LIKE','$1')
205+
->filter_column('created_at','where','users.created_at','LIKE','$1')
206+
//for the blade template only the array data results is provided, it is `extracted` into the template
207+
->edit_column('profile.last_name', '{{ $profile["first_name"]." ".$profile["last_name"] }}')
208+
->edit_column('created_at', function($result_obj) {
209+
//in a callback, the Eloquent object is returned so carbon may be used
210+
return $result_obj->created_at->format('d/m/Y - h:ia');
211+
})
212+
->add_column('manage', '<a href="/user/edit/{{$id}}" >Edit</a>', 3)
213+
->remove_column('profile.photo_id')
214+
->set_index_column('row-{{ $id }}')
215+
->make();
216+
```
217+
```php
218+
//helper scope method in base Model class
219+
public function scopeModelJoin($query, $relation_name, $operator = '=', $type = 'left', $where = false) {
220+
$relation = $this->$relation_name();
221+
$table = $relation->getRelated()->getTable();
222+
$one = $relation->getQualifiedParentKeyName();
223+
$two = $relation->getForeignKey();
224+
225+
if (empty($query->columns)) {
226+
$query->select($this->getTable().".*");
227+
}
228+
229+
//$join_alias = $table;
230+
$prefix = $query->getQuery()->getGrammar()->getTablePrefix();
231+
$join_alias = $relation_name;
232+
foreach (\Schema::getColumnListing($table) as $related_column) {
233+
$query->addSelect(\DB::raw("`$prefix$join_alias`.`$related_column` AS `$join_alias.$related_column`"));
234+
}
235+
$two = str_replace($table . ".", $join_alias . ".", $two);
236+
return $query->join("$table AS $prefix$relation_name", $one, $operator, $two, $type, $where); //->with($relation_name);
237+
}
238+
```
239+
240+
**Notes on columns.data:**
241+
- When using the columns.data option the order the data is returned in doesn't matter.
242+
- You may return extra rows that you use in some tables, and ones you don't without needing to worry about ignoring them.
243+
- When the data is returned within enbeded arrays, datatables lets you access it using dot notation.
244+
This allows the columns.data element to address items in your table. The values of the data items are expected to match the columns (or aliases) of the table. For example, if you sort by "profile.last_name" it will use that to sort by the last_name column in the "profiles" table, so make sure that table is joined so the reference exists.
245+
- If you don't do a direct join you can't sort or search those columns so make sure to set those options to false in the columns array
246+
- If you eager load the data via Eloquent, you will still get those items back and they may be edit via edit_column just the same without needing to alias the names.
247+
172248
**License:** Licensed under the MIT License

0 commit comments

Comments
 (0)