doudouchan5830
2015-03-03 10:04
浏览 74

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

I used laravel 4.2 billam Server side Datatable, and now it' works. But I can't add Edit and Remove Button in this datatable.

View page :

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

Route :

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

Now how can I add Delete and edit button ? I am follow there documentation from here that not works for me.

Documented Example 2: Adding and editing columns

->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}";
})

But how can I use blade structured code in route or controller? it's make little confusion..

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • ds20021205 2015-09-26 09:10
    已采纳

    To add a link to a datatables column from the controller you can do something like this:

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

    $data is the query after the datatables wrapper. So in conecpt of the controller method, this will be used similar to this:

    /**
     * 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);
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题