Laravel and yajrabox Datatables server side proccessing

前端 未结 1 1192
无人共我
无人共我 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." 
    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', ' ') ->remove_column('mobile') ->make(true); }

    my View structure

    Manufacturer Qualified Person Proprietor License# Reg# Contact# Email Address Status Actions

    Datatable JS for server side processing

    
    

    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)?
                                        ''
                                        :
                                        ''
                                    );
    
                    $html       =   '
    '.$status.'
    '; return $html; }) ->make(true); }

    0 讨论(0)
提交回复
热议问题