Laravel and yajrabox Datatables server side proccessing

前端 未结 1 1190
无人共我
无人共我 2021-01-16 12:22

I\'m working on a project which uses Laravel 5.1 as the server side framework, I built a tables to show leads using jQuery Datatables plugin and yajrabox Datatables add-on

相关标签:
1条回答
  • 2021-01-16 13:16

    here is my working example, that How I am using this.

    My controller method for Datatable

    public function getData()
    {
    
        $supplier  = Supplier::with('manufacturer')->select(['id','proprietor','qualified_person','manufacturer_id','license_no','nth_registration_no','phone','mobile','email','address','status']);
    
    
        return Datatables::of($supplier)
                ->editColumn('status', function($supplier){
                 return (($supplier->status == 1)?"Active":"Deactive");
                })
                ->editColumn('phone', function($supplier){
                 return "Phone#: ".$supplier->phone." <br /> Mobile#: ".$supplier->mobile;
                })
                ->editColumn('manufacturer_id', function($supplier){
                 //return $supplier->manufacturer->name;
                    if($supplier->manufacturer_id != 0){
                        return $supplier->manufacturer->name;
                    }else{
                        return 'Not selected!';
                    }
    
                })
                ->addColumn('actions', '
                    <div class="btn-group">
                        <a href="{!!route("supplier-edit",["id"=>$id ])!!}" class="btn btn-xs btn-primary"><i class="fa fa-pencil"></i></a>
                        <a href="{!!route("ajax-delete",["type"=>"supplier","id"=>$id ])!!}" data-target="#ajax_delete" data-toggle="modal" class="btn btn-xs btn-danger">
                            <i class="fa fa-trash-o"></i>
                        </a>
                    </div>
                    ')
                ->remove_column('mobile')
                ->make(true);        
    
    }
    

    my View structure

    <table class="table table-bordered table-striped table-condensed flip-content" id="supplier">
        <thead class="flip-content">
            <tr>
                <th>Manufacturer</th>
                <th>Qualified Person</th>
                <th>Proprietor</th>
                <th>License#</th>
                <th>Reg#</th>
                <th>Contact#</th>
                <th>Email</th>
                <th>Address</th>
                <th>Status</th>
                <th>Actions</th>
            </tr>
        </thead>
    </table>
    

    Datatable JS for server side processing

    <script type="text/javascript">
        var oTable;
    
        $(document).ready(function() {
            oTable = $('#supplier').DataTable({
                "responsive": true,
                "processing": true,
                "serverSide": true,
                "ajax": "{!!route('supplier-data')!!}",
                "columns": [
                    {data: 'manufacturer_id',              name: 'manufacturer_id'},
                    {data: 'qualified_person',              name: 'qualified_person'},
                    {data: 'proprietor',              name: 'proprietor'},
                    {data: 'license_no',              name: 'license_no'},
                    {data: 'nth_registration_no',              name: 'nth_registration_no'},
                    {data: 'phone',             name: 'phone'},
                    {data: 'email',             name: 'email'},
                    //{data: 'mobile',            name: 'mobile'},
                    {data: 'address',           name: 'address'},
                    {data: 'status',            name: 'status'},
                    {data: 'actions',           name: 'actions'},
                ]
            });
        });
    </script>
    

    here is the example of picking items from multiple laravel relationship.

    public function getData()
    {
    
        $medicine  = Medicine::with(['manufacturer','doseageForm','measureUnit','supplier'])
                    ->select(['id','product_name','generic_name','product_class','manufacturer_id', 
                              'doseage_form_id','measure_unit_id','strenght','status']);
    
        return Datatables::of($medicine)
                ->editColumn('status', function($medicine){
                    return (($medicine->status == 1)?"Active":"Deactive");
                })
                ->editColumn('manufacturer_id', function($medicine){
    
                    $manufacturer_name   =   $medicine->manufacturer->name;
                    return $manufacturer_name;
                })
                ->editColumn('product_name', function($medicine){
                    return 
                        $medicine->product_name.", ".
                            $medicine->doseageForm->name.", ".
                            $medicine->strenght.$medicine->measureUnit->name;
                })
                ->addColumn('supplier',function($medicine){
    
                    if($medicine->supplier->count() > 0){
                        return $medicine->supplier->first()->qualified_person;
                    }else{
                        return '---';
                    }
    
    
                })
                ->addColumn('actions', function($medicine){
    
                    $edit_route =   route('medicine-edit',['id'=>$medicine->id ]);
                    $del_route  =   route("ajax-delete",["type"=>"medicine","id"=>$medicine->id ]);
    
                    $status     =   (($medicine->status == 1)?
                                        '<a href="" class="btn btn-xs btn-warning"><i class="fa fa-eye"></i></a>'
                                        :
                                        '<a href="" class="btn btn-xs btn-warning"><i class="fa fa-eye-slash"></i></a>'
                                    );
    
                    $html       =   '<div class="btn-group">
                                        '.$status.'
                                        <a href="'.$edit_route.'" class="btn btn-xs btn-primary" alt="edit"><i class="fa fa-pencil"></i></a>
                                        <a href="'.$del_route.'" data-target="#ajax_delete" alt="delete" data-toggle="modal" class="btn btn-xs btn-danger">
                                            <i class="fa fa-trash-o"></i>
                                        </a>
                                    </div>';
    
                    return $html;
                })
                ->make(true);        
    
    }
    
    0 讨论(0)
提交回复
热议问题