我正在使用laravel datatable包,一切正常。我有关系列搜索的问题。让我详细说明一下:
我有两张桌子:
ed_class(主要表)
ed_section(二级表)
主要数据来自ed_section表,并通过建立关系我从ed_class表获取类名。由于数据来自ed_section表,因此数据表列搜索在节名列上工作正常,但它不能在类名列上工作,那么如何在类名列上实现搜索。?
这里有数据表js代码:
$(function() {
$('#table').DataTable({
"pageLength": 25,
"ordering": false,
//"columnDefs": [{
//"targets": 7,
//"orderable": false
//}],
processing: true,
serverSide: true,
ajax: '{!! url('sections/data') !!}',
columns: [
{ data: 'sr_no', name: 'sr_no' },
{ data: 'classid', name: 'classid' },
{ data: 'sectiontitle', name: 'sectiontitle' },
{ data: 'Option', name: 'Option' }
]
});
});
这里是服务器端代码(使用laravel datatable包):
public function data(){
$model = Section::all();
$data = Datatables::of($model);
$data = $data->addColumn('sr_no', function(Section $section){
})->editColumn('classid', function(Section $section){
return $section->studentClass->classtitle;
})->addColumn('Option', function(Section $section) {
return '<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Action <span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-left">
<li><a class="edit_section" data-id='.$section->sectionid.'> <i class="fa fa-edit" data-toggle="tooltip" title="" data-original-title="Edit"></i> Edit</a></li>
<li role="separator" class="divider"></li>
<li><a href="'.url('/delete_sections/' . $section->sectionid).'" onclick="return confirm(\'Are you sure you want to remove this Section? \')" data-action="delete"> <i class="fa fa-trash-o" data-toggle="tooltip" title="" data-original-title="Delete"></i> Delete</a></li>
</ul>
</div>';
})->rawColumns(['Option']);
$sections = $data->make(true);
return $sections;
}
这是模特关系:
class Section extends Model {
public function studentClass(){
return $this->belongsTo('App\Classes','classid','classId');
}
}
答案 0 :(得分:0)
I have find solution and want to share so that it can help others too:
Here's server side code in controller (using laravel datatable package):
public function data(){
$model = Section::with('studentClass');
$data = Datatables::of($model);
$data = $data->addIndexColumn()
->editColumn('classid', function(Section $section){
return $section->studentClass->classtitle;
})->addColumn('Option', function(Section $section) {
return '<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Action <span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-left">
<li><a class="edit_section" data-id='.$section->sectionid.'> <i class="fa fa-edit" data-toggle="tooltip" title="" data-original-title="Edit"></i> Edit</a></li>
<li role="separator" class="divider"></li>
<li><a href="'.url('/delete_sections/' . $section->sectionid).'" onclick="return confirm(\'Are you sure you want to remove this Section? \')" data-action="delete"> <i class="fa fa-trash-o" data-toggle="tooltip" title="" data-original-title="Delete"></i> Delete</a></li>
</ul>
</div>';
})->rawColumns(['Option']);
$sections = $data->make(true);
return $sections;
}
Here's datatable js code:
$(function() {
$('#table').DataTable({
"pageLength": 25,
"ordering": false,
"columnDefs": [
{ "searchable": false, "targets": [0,3] }
],
processing: true,
serverSide: true,
ajax: '{!! url('sections/data') !!}',
columns: [
{ data: 'DT_Row_Index', name: 'DT_Row_Index' },
{ data: 'classid', name: 'studentClass.classtitle' },
{ data: 'sectiontitle', name: 'sectiontitle' },
{ data: 'Option', name: 'Option' }
],
"oLanguage": {
"sSearch": "Search Class: "
}
});
});
答案 1 :(得分:0)
尝试这样
$orders = Order::with('user');
return Datatables::of($orders)
->addColumn('user', function ($order) {
return $order->user->->name;
})
视图中:
{ "data": "user" , name : "user.name"},