Laravel和yajrabox Datatables服务器端处理 [英] Laravel and yajrabox Datatables server side proccessing
问题描述
我正在执行服务器端处理,但是我需要在发送给客户端之前操纵数据,为此我需要首先拉出我想显示的所有数据(以操纵它)这个过程真的很长。
这是我使用的代码:
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);
}
返回线索,最后一个操作过程:
私有函数setLeadData($ leads)
{
$ rtn = [];
$ row = [];
foreach($ lead as $ lead){
$ row ['DT_RowId'] = $ lead-> _id;
if(\Auth :: user() - > canDeleteLeads()){
$ row ['checkbox'] =< input type ='checkbox'class ='bulk-delete- lead-checkbox'name ='bulk_delete_leads []'id ='。$ lead-> _id。'/>< label for ='$ lead-> _id。'>< /标签>;
}
if(Carbon :: createFromFormat(Ymd H:i:s,$ lead-> created_at) - > isSameDay(Carbon :: now() - > startOfDay()) ){
$ row ['date'] =< i class ='fa fa-clock-o'>< / i> at.Carbon :: createFromFormat(Ymd H:i:s ,$ lead-> created_at) - > timezone(Client :: getTimezone()) - >格式(H:i);
} else {
$ row ['date'] =< i class ='fa fa-clock-o'>< / i>.Carbon :: createFromFormat(Ymd 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 ]['信息'];
}
$ 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(' ('active',1)
- >其中('deleted',' 0)
- > first();
if($ reminder){
$ data ['has_reminder'] = true;
$ icon =< i class ='fa fa-bell'>< / i>;
}
$ row ['full_name'] ='< button type =buttonclass =btn btn-wm btn-white single-lead-triggerstyle =width:100%!重要的data-toggle =modaldata-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 =buttonclass =btn btn-wm btn-white single- lead-triggerstyle =width:100%!importantdata-toggle =modaldata-lead-id ='$ lead-> _id。'data-target =#single_lead> 。$ lead_detail。$ icon。'< / button>';
} else {
$ row [$ k] = $ lead_detail;
}
}
if(isset($ lead-> the_lead ['full_name'])&&空($ lead-> the_lead ['full_name'])) {
}
}
$ rtn [] = $ row;
}
return $ rtn;
}
有没有人有任何建议,如何做到正确和正确?非常感谢任何答案!欣赏它!!
这里是我的工作示例,我如何使用这个。
我的控制器方法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 :$。$ supplier-> phone。< br /> Mobile#:$ supplier-> mobile;
})
- > editColumn('manufacturer_id' $ $)$ {
// return $ supplier-> manufacturer-> name;
if($ supplier-> manufacturer_id!= 0){
return $ supplier-> - > name;
} else {
return'Not selected!';
}
})
- > addColumn('actions' ,'
< div class =btn-group>
< a href ={!!路由(supplier-edit,[id=> $ id]) !!}课=btn btn-xs btn-primary>< i class =fa fa-pen>< / i>< / a>
< a href ={!! route(ajax-delete,[type=>supplier,id=> $ id])!!}data-target = #ajax_deletedata-toggle =modalclass =btn btn-xs btn-danger>
< i class =fa fa-trash-o>< / i>
< / a>
< / div>
')
- > remove_column('mobile')
- > make(true);
}
我的视图结构 p>
< table class =table table-bordered table-striped table-condensed flip-contentid =supplier>
< thead class =flip-content>
< tr>
< th>制造商< / th>
合格人员< / th>
所有者< / th>
< th>许可证#< / th>
® Reg##thth
< th>联系人#< / th>
< th>电子邮件< / th>
< th>地址< / th>
< th>状态< / th>
<动作< / th>
< / tr>
< / thead>
< / table>
用于服务器端处理的Datatable JS
< script type =text / javascript>
var oTable;
$(document).ready(function(){
oTable = $('#supplier')。DataTable({
respond:true,
处理:true,
serverSide:true,
ajax:{!! route('supplier-data')!!},
columns
{data:'manufacturer_id',name:'manufacturer_id'},
{data:'qualified_person',name:'qualified_person'},
{data:'proprietor',name:'所有者},
{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>
这里是从多个拉里尔关系中挑选项目的示例。 p>
public function getData()
{
$ medicine =制造商'''''''''))
- > select(['id','product_name','generic_name','product_class','manufacturer_id',
'doseage_form_id','measure_unit_id','strengthht','status']);
return Datatables :: of($ medicine)
- > editColumn('status',function($ medicine){
return(($ medicine-> status = = 1)?Active:Deactive);
})
- > editColumn('manufacturer_id',function($ medicine){
$ manufacturer_name = $医药 - >制造商 - 名称;
返回$ manufacturer_name;
})
- > editColumn('product_name',function($ medicine){
return
$ medicine-> product_name。,
$ medicine-> doseageForm-> name。,
$ medicine-> strengthht $ 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 $ = $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 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 =b tn btn-xs btn-primaryalt =edit>< i class =fa fa-pencil>< / i>< / a>
< a href ='$ del_route。'data-target =#ajax_deletealt =deletedata-toggle =modalclass =btn btn-xs btn-danger>
< i class =fa fa-trash-o>< / i>
< / a>
< / div>';
return $ html;
})
- > make(true);
}
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 for Laravel.
I am implementing a server side processing but I need to manipulate the data before send it to the client, for do this I need to first pull all the data i'd like to show (to manipulate it) which make the process really long. this is the code i'm using:
public function index()
{
return view('leads.test')->with($data);
}
returns the view, and:
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);
}
return the leads, and last one, the manipulation process:
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;
}
Does any one has any suggestion about how to do it right and proper? thank you very much for any answer! Appreciate it!!
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);
}
这篇关于Laravel和yajrabox Datatables服务器端处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!