1

我想使用 laravel 集合在 yajra 数据表的另一列上输出一个可点击的删除按钮。问题是它{!! Form::open(["action" => ["UsersController@destroy",28], "method" => "POST", "class" => "pull-right"]) !!} {{ Form::hidden("_method", "DELETE") }} {{ Form::submit("Delete", ["class" => "btn btn-danger"]) }} {!! Form::close()!!}")在视图中输出原始 html 文本,而不是可点击的删除按钮。我可以输出常规的 html 标签,但我想使用 laravel 集合。我很困惑为什么即使我已经添加了rawColumns()功能它也不起作用。

这是我的控制器:

public function yajraDT()
{
    $users = User::all();
    return Datatables::of($users)
    ->addColumn('delete', function ($users) {
            return '{!! Form::open(["action" => ["UsersController@destroy",'.$users->id.'], "method" => "POST", "class" => "pull-right"]) !!}
            {{ Form::hidden("_method", "DELETE") }}
            {{ Form::submit("Delete", ["class" => "btn btn-danger"]) }}
            {!! Form::close()!!}")';
    })
    ->rawColumns(['delete'])
    ->make(true);
}

这是我的看法:

<script>
     $(function() {
           $('#tableDT').DataTable({
           processing: true,
           serverSide: true,
           ajax: '{{ url('users/yajraDT') }}',
           columns: [
                    { data: 'id', name: 'id', 
                        @if(Auth::check() && Auth::user()->type == "Admin")
                            render:function(data, type, row)
                            {
                                return "<a href='/users/"+ row.id +"'>" + row.id + "</a>"
                            }
                        @endif},
                    { data: 'first_name', name: 'first_name' },
                    { data: 'last_name', name: 'last_name' },
                    { data: 'email', name: 'email' },
                    { data: 'gender', name: 'gender' },
                    {data: 'delete', name: 'delete'}
                 ]
        });
     });
</script>
4

2 回答 2

1

这就是我这样做的方式:

// in the controller

return Datatables::of($users)
    ->addColumn('delete', function ( $user ) {
        return view('user.delete', compact('user'))->render();
    })
    ->escapeColumns([])
    ->make();

然后你的delete.blade.php

{!! Form::open(["action" => ["UsersController@destroy", $user->id], "method" => "POST", "class" => "pull-right"]) !!}
    {{ Form::hidden("_method", "DELETE") }}
    {{ Form::submit("Delete", ["class" => "btn btn-danger"]) }}
{!! Form::close()!!}

这样,您的控制器中就没有硬编码的 HTML,并且您可以在显示之前渲染 html 输出。

另一个建议是将您的管理检查从 JS 代码移动到您的控制器中,您可以使用editColumnDatatable 上的方法。

于 2019-02-23T08:39:29.790 回答
0

我认为 laravel 集体适用于刀片,因此无需将其放在控制器上。简单地 :-

public function yajraDT()
{
    $users = User::all();
    return Datatables::of($users)
    ->addColumn('delete', function (User $users) {
            return '<a href="admin/user/'.$users->id.'></a>';
    })
    ->rawColumns(['delete'])
    ->make(true);
}

//ROUTE Start
Route::get('admin/user/delete/{id},'Users@destroy');
//ROUTE End

//Remove User
public function destroy($id){
 try{
      User::where('id',$id)->delete();
      return back()->with([
      'message'  => 'User removed successfully."
     ]);
    }catch(Exception $e){
       return back()->with([
          'message' => $e->getMesssage() 
       ]);
    }
  }
于 2019-02-25T10:51:25.487 回答