如何在laravel 4.2的数据表中添加编辑/删除按钮?

时间:2015-03-03 10:04:57

标签: php jquery laravel-4 datatable laravel-routing

我使用laravel 4.2 billam服务器端Datatable,现在它可以工作。但我无法在此数据表中添加编辑和删除按钮。

查看页面:

 <script type="text/javascript">
var oTable;
$(document).ready(function() {
    oTable = $('#tasks').dataTable( {
        "sPaginationType": "bootstrap",
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "{{ URL::to('/task') }}"
    });
});

路线:

Route::get('/task',function(){
$posts = Campaigns::select(array('title','created_at','status'));
 return Datatables::of($posts)
 ->make();
});

现在我该如何添加删除和编辑按钮?我跟着来自here的文件不适合我。

记录示例2:添加和编辑列

->add_column('operations', '<a href="{{ URL::route( \'admin.post\', array( \'edit\',$id )) }}">edit</a>
                <a href="{{ URL::route( \'admin.post\', array( \'delete\',$id )) }}">delete</a>
            ')
->edit_column('status', '{{ $status ? 'Active' : 'Passive' }}')
->edit_column('ownername', function($row) {
    return "The author of this post is {$row->ownername}";
})

但是如何在路由或控制器中使用刀片结构化代码?这有点混乱..

1 个答案:

答案 0 :(得分:0)

要从控制器添加指向datatables列的链接,您可以执行以下操作:

->addColumn('actions', function ($data) {
                return "<a class='btn btn-xs btn-success' href='/management/members/$data->id/view'>View</a>";
            })

$ data是数据表包装器之后的查询。所以在控制器方法的conecpt中,这将使用类似于:

/**
 * Members list data.
 *
 * @param Datatables $datatables
 * @param MemberRepository $repository
 *
 * @return \Illuminate\Http\JsonResponse
 */
public function getIndexData(Datatables $datatables, MemberRepository $repository)
{
    $members = $repository->getMemberListForManagement();


    return $datatables->usingQueryBuilder($members)
        ->editColumn('created_at', function ($data) {
            return date('d F Y', strtotime($data->created_at));
        })
        ->editColumn('is_banned',
            '@if($is_banned)
                Yes
            @else
                No
            @endif')
        ->addColumn('actions', function ($data) {
            return "<a class='btn btn-xs btn-success' href='/management/members/$data->id/view'>View</a>";
        })
        ->removeColumn('id')
        ->make(true);
}