Skip to content

Commit 44ca434

Browse files
author
Kamil Tunkiewicz
committed
Merge pull request #115 from ktunkiewicz/master
Datatables 1.10+ update, backwards compatibile + PR #93 #117 #113
2 parents 3532e0a + 2846b9f commit 44ca434

File tree

2 files changed

+176
-89
lines changed

2 files changed

+176
-89
lines changed

README.md

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
**About**
44

5-
This bundle is created to handle server-side works of DataTables Jquery Plugin (http://datatables.net) by using Eloquent ORM or Fluent Query Builder.
5+
This bundle is created to handle the server-side processing of the DataTables Jquery Plugin (http://datatables.net) by using Eloquent ORM or Fluent Query Builder.
66

77
### Feature Overview
88
- Supporting Eloquent ORM and Fluent Query Builder
9-
- Adding or editing content of columns and removing columns
9+
- Adding or editing the contents of columns and removing columns
1010
- Templating new or current columns via Blade Template Engine
1111
- Customizable search in columns
1212

1313

1414
### Installation
1515

16-
Add the `bllim/datatables` under the `require` key after that run the `composer update`.
16+
Require `bllim/datatables` in composer.json and run `composer update`.
1717

1818
{
1919
"require": {
@@ -24,7 +24,7 @@ Add the `bllim/datatables` under the `require` key after that run the `composer
2424
...
2525
}
2626

27-
Composer will download the package. After package downloaded, open "app/config/app.php" and edit like below:
27+
Composer will download the package. After the package is downloaded, open `app/config/app.php` and add the service provider and alias as below:
2828

2929
'providers' => array(
3030
...
@@ -49,53 +49,52 @@ $ php artisan config:publish bllim/datatables
4949
It is very simple to use this bundle. Just create your own fluent query object or eloquent object without getting results (that means don't use get(), all() or similar methods) and give it to Datatables.
5050
You are free to use all Eloquent ORM and Fluent Query Builder features.
5151

52-
It is better, you know these:
53-
- When you use select method on Eloquent or Fluent Query, you choose columns
54-
- You can easily edit columns by using edit_column($column,$content)
55-
- You can remove any column by using remove_column($column) method
56-
- You can add columns by using add_column($column_name, $content, $order)
57-
- You can use Blade Template Engine in your $content values
58-
- The name of columns is set by returned array.
59-
- That means, for 'posts.id' it is 'id' and also for 'owner.name as ownername' it is 'ownername'
60-
- You can set the "index" column (http://datatables.net/reference/api/row%28%29.index%28%29) using set_index_column($name)
61-
- You can add search filters for each column to override default search functionality
62-
52+
Some things you should know:
53+
- When you call the `select` method on Eloquent or Fluenty Query, you choose columns.
54+
- Modifying columns
55+
- You can easily edit columns by using `edit_column($column, $content)`
56+
- You can remove any column by using `remove_column($column)`
57+
- You can add columns by using `add_column($column_name, $content, $order)
58+
- You **can** use the Blade Template Engine in your `$content` values.
59+
- You may pass a function as the $content to the `add_column` or `edit_column` calls. The function receives a single parameter: the query row/model record. (see Example 2)
60+
- The column identifiers are set by the returned array.
61+
- That means, for `posts.id` the relevant identifier is `id`, and for `owner.name as ownername` it is `ownername`
62+
- You can set the "index" column (http://datatables.net/reference/api/row().index()) using `set_index_column($name)`
63+
- You can add customized search filters for each column to override the default search functionality
64+
- You can call `make(true)` to return an array of objects instead of an array of arrays. (see Example 4)
6365

6466
### Examples
6567

66-
**Example 1:**
68+
**Example 1: Simple use**
6769

6870
$posts = Post::select(array('posts.id','posts.name','posts.created_at','posts.status'));
6971

7072
return Datatables::of($posts)->make();
7173

7274

73-
**Example 2:**
75+
**Example 2: Adding and editing columns**
7476

7577
$place = Place::left_join('owner','places.author_id','=','owner.id')
76-
->select(array('places.id','places.name','places.created_at','owner.name as ownername','places.status'));
78+
->select(array('places.id','places.name','places.created_at','owner.name as ownername','places.status'));
7779

7880

7981
return Datatables::of($place)
80-
->add_column('operations','<a href="{{ URL::route( \'admin.post\', array( \'edit\',$id )) }}">edit</a>
81-
<a href="{{ URL::route( \'admin.post\', array( \'delete\',$id )) }}">delete</a>
82-
')
83-
->edit_column('status','@if($status)
84-
Active
85-
@else
86-
Passive
87-
@endif')
88-
// you can also give a function as parameter to edit_column and add_column instead of blade string
89-
->edit_column('ownername','Author of this post is {{ $ownername }}')
90-
->remove_column('id')
91-
->make();
82+
->add_column('operations', '<a href="{{ URL::route( \'admin.post\', array( \'edit\',$id )) }}">edit</a>
83+
<a href="{{ URL::route( \'admin.post\', array( \'delete\',$id )) }}">delete</a>
84+
')
85+
->edit_column('status', '{{ $status ? 'Active' : 'Passive' }}')
86+
->edit_column('ownername', function($row) {
87+
return "The author of this post is {$row->ownername}";
88+
})
89+
->remove_column('id')
90+
->make();
9291

93-
**Notice:** If you use double quotes while giving content of add_column or edit_column, you should escape variables with backslash (\) else you get error. For example:
92+
**Notice:** If you use double quotes while assigning the $content in an `add_column` or `edit_column` call, you should escape variables with a backslash (\\) to prevent an error. For example:
9493

95-
edit_column('id',"- {{ \$id }}") .
94+
edit_column('id', "{{ \$id }}") .
9695

9796

98-
**Example 3:**
97+
**Example 3: Using filter_column**
9998

10099
$clients = Client::select(array(
101100
'Client.id',
@@ -119,18 +118,41 @@ It is better, you know these:
119118

120119
**Notes on filter_column:**
121120

122-
Usage: `filter_column ( $column_name, $method, $param_1, $param_2 ... $param_n )`
121+
Usage: `filter_column ( $column_name, $method, $param_1, $param_2, ..., $param_n )`
123122
* `$column_name` - the column name that search filter is be applied to
124123
* `$method` - can be any of QueryBuilder methods (where, whereIn, whereBetween, having etc.).
125124
* Note: For global search these methods are automaticaly converted to their "or" equivalents (if applicable, if not applicable, the column is not searched).
126-
* If you do not want some column to be searchable in global search set the last where's parameter to "and" (see line 17 in example above). Doing this way the filter cannot be switched into its "or" equivalent therefore will not be searched in global search .
127-
* `$param_1 ... $param_n` - these are parameters that will be passed to the selected where function. Possible types:
125+
* If you do not want some column to be searchable in global search, set the last parameter to "and" (see line 17 in example above). Doing this, the filter cannot be switched into its "or" equivalent and therefore will not be searched in global search .
126+
* `$param_1 ... $param_n` - these are parameters that will be passed to the selected where function (`$method`). Possible types:
128127
* `string`
129-
* `DB::raw()` - The DB::raw() can output literaly everything into the query, for example subqueries or branching if you need some really sophisticated wheres.
128+
* `DB::raw()` - The DB::raw() can output literaly everything into the query. For example, subqueries or branching if you need some really sophisticated wheres.
130129
* `function` - or any other callable
131-
* `array` of any above
132-
* the search value is passed to the query by `$1` string placed anywhere in parameters. If callable (function) is used the searched value is passed to callable as first parameter. The callable must returns value that will be passed to the QueryBuilder's function.
130+
* `array` of any of the above
131+
* The search value is passed to the query by the string `$1` placed anywhere in parameters. If a callable (function) is used the searched value is passed to callable as first parameter the first parameter (again see line 17).
132+
* The callable must return a value that will be passed to the QueryBuilder's function.
133+
134+
135+
**Example 4: Returning an array of objects*
136+
137+
$posts = Post::select(array('posts.id','posts.name','posts.created_at','posts.status'));
133138

139+
return Datatables::of($posts)->make(true);
134140

141+
This returns a JSON array with data like below:
135142

143+
data: {
144+
{
145+
id: 12,
146+
name: 'Dummy Post',
147+
created_at: '1974-06-20 13:09:51'
148+
status: true
149+
}
150+
{
151+
id: 15,
152+
name: 'Test post please ignore',
153+
created_at: '1974-06-20 13:15:51',
154+
status: true
155+
}
156+
}
157+
136158
**License:** Licensed under the MIT License

0 commit comments

Comments
 (0)