【问题标题】:Laravel and yajrabox Datatables server side proccessingLaravel 和 yajrabox 数据表服务器端处理
【发布时间】:2016-06-21 05:55:55
【问题描述】:

我正在开发一个使用 Laravel 5.1 作为服务器端框架的项目,我使用 jQuery Datatables 插件和 Laravel 的 yajrabox Datatables 插件构建了一个表格来显示潜在客户。

我正在实现服务器端处理,但我需要在将数据发送到客户端之前对其进行操作,为此我需要首先提取我想要显示的所有数据(以对其进行操作),这些数据构成了该过程真长。 这是我正在使用的代码:

public function index()
{ 
   return view('leads.test')->with($data);
}

返回视图,并且:

 public function getLeads()
{
    $end = new \MongoDate(Carbon::now()->endOfDay()->setTimezone(Client::getTimezone(5))->timestamp);
    $start = new \MongoDate(Carbon::now()->subWeek()->startOfDay()->setTimezone(Client::getTimezone(5))->timestamp);
    return \Datatables::of(collect($this->setLeadData(Lead::where('deleted',0)->where('client_id','5')->whereBetween('created_at',[$start,$end])->orderBy('created_at','desc')->get())))->make(true);
}

返回线索,最后一个,操作过程:

private function setLeadData($leads)
{
    $rtn = [];
    $row = [];
    foreach ($leads as $lead) {
        $row['DT_RowId'] = $lead->_id;
        if(\Auth::user()->canDeleteLeads()){
            $row['checkbox'] = "<input type='checkbox' class='bulk-delete-leads-checkbox' name='bulk_delete_leads[]' id='".$lead->_id."' /><label for='".$lead->_id."'></label>";
        }
        if(Carbon::createFromFormat("Y-m-d H:i:s", $lead->created_at)->isSameDay(Carbon::now()->startOfDay())){
            $row['date'] = "<i class='fa fa-clock-o'></i> at ".Carbon::createFromFormat("Y-m-d H:i:s", $lead->created_at)->timezone(Client::getTimezone())->format("H:i");
        }else{
            $row['date'] = "<i class='fa fa-clock-o'></i> ".Carbon::createFromFormat("Y-m-d H:i:s", $lead->created_at)->timezone(Client::getTimezone())->toDateTimeString();
        }
        if(is_array($lead->ga_details) && count($lead->ga_details) > 1){
            $row['user_type'] = $lead->ga_details['user_type'];
            $row['device_category'] = $lead->ga_details['device_category'];
            $row['source'] = $lead->ga_details['source'];
            $row['campaign'] = $lead->ga_details['campaign'];
            $row['ad_group'] = $lead->ga_details['ad_group'];
            $row['path'] = $lead->ga_details['path'];
        }
        $row['last_feed'] = null;
        if ($lead->feeds && count($lead->feeds)) {
            $row['last_feed'] = array_reverse($lead->feeds)[0]['message'];
        }
        $row['status'] = $this->setLeadStatusElement($lead->status,$lead->_id);
        $row['owner'] = $this->setLeadOwnerElement($lead->owner,$lead->_id);
        $data['has_reminder'] = false;
        $icon = '';
        $reminder = Notification::where('lead_id', $lead->_id)
            ->where('type', 'lead_reminder')
            ->where('due_time','<=',Carbon::now()->toDateTimeString())
            ->where('active',1)
            ->where('deleted',0)
            ->first();
        if($reminder){
            $data['has_reminder'] = true;
            $icon = " <i class='fa fa-bell'></i> ";
        }
        $row['full_name'] = '<button type="button" class="btn btn-w-m btn-white single-lead-trigger" style="width:100%!important" data-toggle="modal" data-lead-id="' .$lead->_id. '" data-target="#single_lead">No Name'.$icon.'</button>';
        if(isset($lead->the_lead) && count($lead->the_lead)){
            foreach($lead->the_lead as $k => $lead_detail){
                if($k == "full_name"){
                    $row['full_name'] = '<button type="button" class="btn btn-w-m btn-white single-lead-trigger" style="width:100%!important" data-toggle="modal" data-lead-id="' .$lead->_id. '" data-target="#single_lead">' .$lead_detail.$icon.'</button>';
                }else{
                    $row[$k] = $lead_detail;
                }
            }
            if(isset($lead->the_lead['full_name']) && !empty($lead->the_lead['full_name'])){
            }
        }

        $rtn[] = $row;
    }
    return $rtn;
}

有没有人对如何正确和正确地做这件事有任何建议?非常感谢您的任何回答!赞一个!!

【问题讨论】:

    标签: php datatables laravel-5.1


    【解决方案1】:

    这是我的工作示例,我如何使用它。

    我的 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);        
    
    }
    

    我的视图结构

    <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>
    

    用于服务器端处理的数据表 JS

    <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>
    

    这里是从多个 laravel 关系中挑选物品的例子。

    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);        
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-06
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多