最佳答案
注意:这没有使用 Chumper/Datatable 包,但它确实使用了 jQuery 数据表,因此可能会有一些用处。
这是我的做法。这是一个场景,其中我有一张 table ,里面有美国橄榄球队。每个团队都是一个 session 的成员,也是一个部门的一部分。团队可以按团队名称、 session 或部门进行排序。下面是用于实现此目的的服务器端代码。此外,它们可以按 session 或部门进行过滤。
/*
* Route::get( 'api/v1/teams-table', 'APIController@teamsTable' );
*/
public function dataTable() {
// get the input parameters
$i = Input::all();
// parse the parameters and set default values
$draw = isset( $i[ 'draw' ] ) ? $i[ 'draw' ] : 1;
$start = isset( $i[ 'start' ] ) ? $i[ 'start' ] : 0;
$length = isset( $i[ 'length' ] ) ? $i[ 'length' ] : 10;
$search = isset( $i[ 'search' ][ 'value' ] ) && '' != $i[ 'search' ][ 'value' ] ? $i[ 'search' ][ 'value' ] : false;
$ordrby = isset( $i[ 'order' ] ) ? $i[ 'columns' ][ $i[ 'order' ][ 0 ][ 'column' ] ][ 'name' ] : '';
$ordrdr = isset( $i[ 'order' ] ) ? $i[ 'order' ][ 0 ][ 'dir' ] : 'asc';
$total = Team::count();
$filter = $total;
// get the data
if ( '' == $search ) {
switch( $ordrby ) {
case 'name':
$teams = Team::with( 'conferences', 'logo', 'conferences.division' )
->skip( $start )
->take( $length )
->orderBy( 'name', $ordrdr )
->get();
break;
case 'conference':
$teams = Team::with( 'conferences', 'logo', 'conferences.division' )
->join( 'conference_team', 'conference_team.team_id', '=', 'teams.id' )->join( 'conferences', 'conferences.id', '=', 'conference_team.conference_id' )
->orderBy( 'conferences.abbr', $ordrdr )
->skip( $start )
->take( $length )
->get();
break;
case 'division':
$teams = Team::with( 'conferences', 'logo', 'conferences.division' )
->skip( $start )
->take( $length )
->conference()
->division()
->orderBy( 'abbr', $ordrdr )
->get();
break;
default:
$teams = Team::with([ 'conferences', 'logo', 'conferences.division' ])
->skip( $start )
->take( $length )
->get();
}
} else {
$teams = Team::with( 'conferences', 'logo', 'conferences.division' )
->skip( $start )
->take( $length )
->where( 'name', 'LIKE', '%' . $search . '%' )
->orWhereHas( 'conferences', function( $q ) use ( $search ) {
$q->where( 'abbr', 'LIKE', '%' . $search . '%' )
->orWhereHas( 'division', function( $qu ) use ( $search ) {
$qu->where( 'abbr', 'LIKE', '%' . $search . '%' );
});
})
->get();
$filter = Team::with( 'conferences', 'logo', 'conferences.division' )
->where( 'name', 'LIKE', '%' . $search . '%' )
->orWhereHas( 'conferences', function( $q ) use ( $search ) {
$q->where( 'abbr', 'LIKE', '%' . $search . '%' )
->orWhereHas( 'division', function( $qu ) use ( $search ) {
$qu->where( 'abbr', 'LIKE', '%' . $search . '%' );
});
})
->count();
}
// loop through the retrieved data and format it to be returned as JSON
$data = [];
foreach ( $teams as $t ) {
$show = URL::route( 'admin.team.show', $t->slug );
$edit = URL::route( 'admin.team.depth_chart', $t->slug );
$data[] = [
'checkbox' => '<label><input type="checkbox" class="ace" value="' . $t->id . '" /><span class="lbl"></span></label>',
'logo' => '<img src="' . $t->logo->filename . '" alt="' . $t->name . ' logo" height="40">',
'name' => [
'display' => link_to_route( 'admin.team.show', $t->name, [ $t->slug ] ),
'filter' => $t->name,
'sort' => $t->name,
],
'conference' => [
'display' => link_to_route( 'admin.conference.show', $t->conferences[ 0 ]->abbr, [ $t->conferences[ 0 ]->slug ] ),
'filter' => $t->conferences[ 0 ]->name . ' ' . $t->conferences[ 0 ]->abbr,
'sort' => $t->conferences[ 0 ]->abbr,
],
'division' => [
'display' => link_to_route( 'admin.division.show', $t->conferences[ 0 ]->division->abbr, [ $t->conferences[ 0 ]->division->slug ] ),
'filter' => $t->conferences[ 0 ]->division->name . ' ' . $t->conferences[ 0 ]->division->abbr,
'sort' => $t->conferences[ 0 ]->division->abbr,
],
'site' => '<a target="_blank" href="' . $t->url . '">website <i class="fa fa-external-link"></i></a>',
'actions' => sprintf( $this->actions, $show, $edit, $show, $edit ),
];
}
$tdata = [
'draw' => $draw,
'recordsTotal' => $total, //consider caching or setting fixed value for this
'recordsFiltered' => $filter,
'data' => $data,
];
return Response::json( $tdata );
}
如果幸运的话,您可以调整此示例以适应您的情况。希望这有帮助!
关于jquery数据表,服务器端自定义排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33036554/