Skip to content

Handle both client side and server side datatable js operations

License

Notifications You must be signed in to change notification settings

iYogesharma/datatable-laravel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Latest Stable Version Total Downloads License

jQuery Datatables For Laravel

A simple package to ease datatable.js server side operations

This package is created to handle server-side and client-side works of DataTables jQuery Plugin via AJAX option by using Eloquent ORM, Query Builder or Collection.

datatable-laravel 4.x

Version 4.x continues the improvements in version 3.x by introducing some new features

New

  • Added support for select raw queries

  • Auto guess column names if no columns are provided in request

  • Auto guess column names if * is provided in column names

  • Added support for group by and havig clause

  • Example

      echo  datatable(User::select('users.name','users.email','users.contact_no','users.role_id')
        ->selectRaw("
            Max(id) as total
       ")
       ->groupBy('users.name', 'users.email', 'users.contact_no'))->init();
    
      echo   datatable(User::select('users.*'))->init();
  • Added Support For Data Filtering From Client Side

  • Added Column Wise Search Query Support Using Below Api

  • Example

    {
      "columns": [
        {
          "data": "name",
          "name": "name",
          "searchable": true,
          "orderable": true,
          "search": {
            "value": "",
            "regex": false
          }
        }
      ],
      "start": 0,
      "length": 10,
      "search": {
        "value": "Yoges",
        "regex": false
      },
      "filters": {
        "role_id" : [1,2],// role id in 1,2
        "created_at": [date1, date2], // createde at is between date1 and date2,
        "name": "iyogesh" // where name = iyogesh
      }
    }
    

Modified

Modified datatable function to support server side export to xls,csv and json

You just need to pass 2 new arguments in query-string/body export and ext

if export = true it will return download file response for ext default value is xlsx

    https://datatable-url?export=true&ext=xlsx

Using Helper Function

echo datatable()->of(User::query())->init();
echo datatable()->of(DB::table('users')->join1()->join2()->select(column1,column2,...columnK))->init();
echo datatable()->of(DB::table('users'))->init();
echo datatable()->of(User::all())->init();

echo datatables(User::query());
echo datatables(DB::table('users')->join1()->join2()->select(column1,column2,...columnK));
echo datatables(DB::table('users'));
echo datatables(User::all());

Using Facade

use Datatable;

echo Datatable::of(User::query())->init();
echo Datatable::of(DB::table('users')->join1()->join2()->select(column1,column2,...columnK))->init();
echo Datatable::of(DB::table('users'))->init();
echo Datatable::of(User::all())->init();

echo Datatable::make(User::query());
echo Datatable::make(DB::table('users')->join1()->join2()->select(column1,column2,...columnK));
echo Datatable::make(DB::table('users'));
echo Datatable::make(User::all());

Add/Edit Column

use Datatable;

echo Datatable::of(User::query())->add(columnName,function($user){
    return "<a href='' id='$user->id'>$user->name</a>";
})->init();

Using Helper Function

echo datatable()->of(User::query())->add(columnName,function($user){
    return "<a href='' id='$user->id'>$user->name</a>";
})->init();

Add/Edit Multiple Columns

use Datatable;

echo Datatable::of(User::query())->addColumns([columnName1=>function($user){
    return "<a href='' id='$user->id'>$user->name</a>";
},columnName2=>function($user){
    return "<a href='' id='$user->id'>$user->name</a>";
}...])->init();

Remove Column

use Datatable;

echo Datatable::of(User::query())->remove(columnName)->init();

Remove Multiple Columns

use Datatable;

echo Datatable::of(User::query())->remove([columnName1,columnName2,...])->init();

Requirements

Quick Installation

$ composer require iyogesharma/datatable-laravel:"~1.0"

Service Provider & Facade (Optional on Laravel 5.5)

Register provider and facade on your config/app.php file.

'providers' => [
    ...,
    YS\Datatable\DatatableServiceProvider::class,
]

'aliases' => [
    ...,
    'Datatable' => YS\Datatable\Facades\Datatable::class,
     "Table"=>YS\\Datatable\\Facades\\Table::class
]

load css files

before </body> tag add

    {{table()->css()}}

load script files

before </body> tag add

    {{table()->scripts()}}

load Dependencies (css/js)

before </body> tag add

    {{table()->dependencies()}}

Initialize Basic DataTable

In HTMl file inside document .ready function write

    {{table()->basic()}}

Initialize Ajax DataTable

In HTMl file inside document .ready function write

    {{table()->ajax($url,$columns,$configs)}}

Example

    {{table()->dependencies()}}

     <script>
        $(document).ready(function(){
                {{ table()->ajax('ddd/ddd',
                    [   'name',
                        'email',
                        'office'
                    ],[
                        'paging'=>'true',
                        'fixedheader'=>'true',
                        'buttons'=>['colvis','copy','csv','print'],
                        'order'=>[[0,'desc']],
                        'lengthMenu'=> [[ 10, 20, 30, 40, 50], [ 10, 20, 30, 40, 50]],
                    ]
                )}}
        })
    </script>

License

The MIT License (MIT). Please see License File for more information.