Skip to content

Commit

Permalink
Datatables work
Browse files Browse the repository at this point in the history
  • Loading branch information
Halim authored and Halim committed Nov 10, 2016
1 parent 83936a5 commit 6237f0a
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Article;
use Yajra\Datatables\Services\DataTable;

class Articles3DataTable extends DataTable
class ArticlesDataTable extends DataTable
{
/**
* Display ajax response.
Expand All @@ -18,6 +18,10 @@ public function ajax()
->eloquent($this->query())
->addColumn('action', 'path.to.action.view')
->make(true);

// $articles = Article::select(['id', 'title', 'body', 'created_at', 'updated_at']);
// return Datatables::of($articles)->editColumn('title', '{{ $title."-title" }}')->make();

}

/**
Expand All @@ -27,9 +31,12 @@ public function ajax()
*/
public function query()
{
$query = Article::query();
//$query = Article::query();
$query = Article::query()->select(['id', 'title', 'created_at', 'updated_at']);

return $this->applyScopes($query);
//return $this->applyScopes($query);

return $query;
}

/**
Expand All @@ -56,6 +63,7 @@ protected function getColumns()
return [
'id',
// add your columns
'title',
'created_at',
'updated_at',
];
Expand Down
19 changes: 0 additions & 19 deletions app/Http/Controllers/Article3Controller.php

This file was deleted.

42 changes: 0 additions & 42 deletions app/Http/Controllers/Articles2Controller.php

This file was deleted.

50 changes: 50 additions & 0 deletions app/Http/Controllers/ArticlesDTController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Yajra\Datatables\Facades\Datatables;
use App\Article;

class ArticlesDTController extends Controller {
/*
*
*/

public function __construct() {
$this->middleware('auth');
}

/**
* Display a listing of the resource.
*/
public function index() {
return view('datatables.index');
}

/**
* Process datatables ajax request.
*/
public function getBasicData() {

$articles = Article::select(['id', 'title', 'body', 'created_at', 'updated_at']);
return Datatables::of($articles)->make();

//return Datatables::of(Article::query())->make();
}

/**
* Process datatables ajax request.
*/
public function AddEditRemoveColumn() {

$articles = Article::select(['id', 'title', 'body', 'created_at', 'updated_at']);

return Datatables::of($articles)->addColumn('action', 'edit')->make();

//return Datatables::of(Article::query())->make();
}


}
19 changes: 19 additions & 0 deletions app/Http/Controllers/ArticlesDTSController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\DataTables\ArticlesDataTable;

class ArticlesDTSController extends Controller {


public function index(ArticlesDataTable $dataTable) {


return $dataTable->render('datatables.index2');


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
<h2>Articles List (Using Datatables)</h2>


<table class="table table-bordered" id="users-table">
<table class="table table-bordered" id="datatable">
<thead>
<tr>
<th>Id</th>
<th>Article Title</th>
<th>Article Body</th>
<th>Created At</th>
<th>Updated At</th>

</tr>
</thead>
</table>
Expand All @@ -31,27 +32,27 @@
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
<script>
$(function () {
$('#users-table00').DataTable({
processing: true,
serverSide: true,
ajax: "{{ url('articles2/anyData') }}",
columns: [
{data: 'id', name: 'id'},
{data: 'title', name: 'title'},
{data: 'body', name: 'body'},
{data: 'created_at', name: 'created_at'},
{data: 'updated_at', name: 'updated_at'}
]
});
$(function () {
$('#users-table').DataTable({
$('#datatable0').DataTable({
processing: true,
serverSide: true,
ajax: '{{ url("articles2/anyData") }}'
ajax: '{{ url("datatable/ajaxdata") }}'
});
});
$('#datatable').DataTable({
processing: true,
serverSide: true,
ajax: '{{ url("datatable/ajaxdata") }}',
columns: [
{data: 0, name: 'id'},
{data: 1, name: 'title'},
{data: 2, name: 'body', searchable: false},
{data: 3, name: 'created_at'},
{data: 4, name: 'updated_at'}
]
});
});
</script>
@endpush
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Articles List (Using Datatables)</h2>
<h2>Articles List (Using Datatables as a Service Implementation)</h2>

{!! $dataTable->table() !!}

Expand Down
2 changes: 2 additions & 0 deletions resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

<!-- Scripts -->
<script src="/js/app.js"></script>
<!-- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>-->

<script>
$('div.alert').not('.alert-important').delay(1500).slideUp(300);
Expand Down
2 changes: 1 addition & 1 deletion resources/views/layouts/navbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<li><a href="{{ url('articles/create') }}">New Article</a></li>
<li><a href="{{ url('articles') }}">Articles List</a></li>
<li class="divider"></li>
<li><a href="{{ url('articles2') }}">Articles List (Datatables)</a></li>
<li><a href="{{ url('datatable') }}">Articles List (Datatables)</a></li>
</ul>
</li>
<li class="dropdown">
Expand Down
13 changes: 10 additions & 3 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@
/*
* Datatables routes
*/
Route::get('articles2', 'Articles2Controller@index');
Route::get('articles2/anyData', 'Articles2Controller@anyData');
Route::get('datatable', 'ArticlesDTController@index');
//Route::get('datatable/ajaxdata', 'ArticlesDTController@getBasicData'); // getBasicData, AddEditRemoveColumn
Route::get('datatable/ajaxdata', 'ArticlesDTController@AddEditRemoveColumn');


Route::resource('articles3', 'Article3Controller');


/*
* Datatables routes as Service
*/
Route::resource('datatable2', 'ArticlesDTSController');



/*
Expand Down

0 comments on commit 6237f0a

Please sign in to comment.