Laravel and yajrabox Datatables server side proccessing(Laravel 和 yajrabox 数据表服务器端处理)
问题描述
我正在开发一个使用 Laravel 5.1 作为服务器端框架的项目,我使用 jQuery Datatables 插件和 Laravel 的 yajrabox Datatables 插件构建了一个表格来显示潜在客户.
我正在实施服务器端处理,但我需要在将数据发送到客户端之前对其进行操作,为此我需要首先提取我想要显示(操作它)的所有数据,这使得该过程真的好长这是我正在使用的代码:
公共函数索引(){return view('leads.test')->with($data);}
返回视图,并且:
公共函数 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 ($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("Ymd H:i:s", $lead->;created_at)->timezone(Client::getTimezone())->format("H:i");}别的{$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]['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('删除',0)-> 第一();如果($提醒){$data['has_reminder'] = true;$icon = " <i class='fa fa-bell'></i> ";}$row['full_name'] = '<button type="button" class="btn btn-wm 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-wm 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>';}别的{$row[$k] = $lead_detail;}}if(isset($lead->the_lead['full_name']) && !empty($lead->the_lead['full_name'])){}}$rtn[] = $row;}返回 $rtn;}
有没有人对如何正确正确地做这件事有任何建议?非常感谢您的任何答复!欣赏!!
这是我的工作示例,即我如何使用它.
我的数据表控制器方法
公共函数getData(){$supplier = Supplier::with('manufacturer')->select(['id','proprietor','qualified_person','manufacturer_id','license_no','nth_registration_no','phone','mobile','电子邮件','地址','状态']);返回数据表:: 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){//返回 $supplier->manufacturer->name;if($supplier->manufacturer_id != 0){返回 $supplier->manufacturer->name;}别的{return '未选中!';}})->addColumn('动作', '<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>
')->remove_column('移动')->make(true);}
我的视图结构
<thead class="flip-content"><tr><th>制造商</th>合格人员 <th>所有者</th><th>License#</th><th>Reg#</th><th>联系人#</th><th>电子邮件</th><th>地址</th><th>状态</th><th>动作</th></tr></thead>
用于服务器端处理的Datatable JS
这里是从多个 Laravel 关系中挑选项目的例子.
公共函数getData(){$medicine = Medicine::with(['manufacturer','doseageForm','measureUnit','supplier'])->select(['id','product_name','generic_name','product_class','manufacturer_id','doseage_form_id','measure_unit_id','强度','状态']);返回数据表:: of($medicine)->editColumn('status', function($medicine){return (($medicine->status == 1)?"Active":"Deactive");})->editColumn('manufacturer_id', function($medicine){$manufacturer_name = $medicine->manufacturer->name;返回 $manufacturer_name;})->editColumn('product_name', function($medicine){返回$medicine->product_name.", ".$medicine->doseageForm->name.", ".$medicine->strenght.$medicine->measureUnit->name;})->addColumn('supplier',function($medicine){if($medicine->supplier->count()>0){返回 $medicine->supplier->first()->qualified_person;}别的{返回 ' - -';}})->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 = ''.$状态.'<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>';返回 $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 数据表服务器端处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 和 yajrabox 数据表服务器端处理
- PHP foreach() 与数组中的数组? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01