doudouchan5830 2015-03-03 10:04
浏览 85
已采纳

如何在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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化