如何在 yajra 数据表 laravel 5.7 中使用 link 使列数据可点击

How to make column data clickable with link in yajra datatable laravel 5.7

我正在尝试使链接到另一个视图的 ID 的行值可点击。这可以在前端使用来自 jQuery 的常规数据表来实现,例如:<td><h6><a href="/users/{{ $user->id }}">{{ $user->id }}</a></h6></td>。但是我如何使用 yajra 来做到这一点?出于某种原因,yajrabox.com 不会加载到我这边,所以我无法阅读他们的文档。我也找不到相关的教程。这是我目前所拥有的。

用户控制器:

public function index()
{        
    return view('users.index');
}

public function yajraDT()
{
    return Datatables::of(User::query())->make(true);
}

index.blade.php:

<div class="container">
<h2>Laravel DataTables Tutorial Example</h2>
    <table class="table table-bordered" id="tableDT">
        <thead>
            <tr>
                <th class="text-left">Id</th>
                <th class="text-left">First Name</th>
                <th class="text-left">Last Name</th>
                <th class="text-left">Email</th>
                <th class="text-left">Gender</th>
                @if(Auth::check() && Auth::user()->type == "Admin")
                <th class="text-left">Actions</th>
                @endif
            </tr>
        </thead>
    </table>

<script>
     $(function() {
           $('#tableDT').DataTable({
           processing: true,
           serverSide: true,
           ajax: '{{ url('users/yajraDT') }}',
           columns: [
                    { data: 'id', name: 'id' },
                    { data: 'first_name', name: 'first_name' },
                    { data: 'last_name', name: 'last_name' },
                    { data: 'email', name: 'email' },
                    { data: 'gender', name: 'gender' }
                 ]
        });
     });
</script>

routes.web:

Route::get('users/yajraDT', 'UsersController@yajraDT');
Route::resource('users', 'UsersController');

您可以像这样将渲染函数传递给您的数据:

{ data: 'id', name: 'id', render:function(data, type, row){
    return "<a href='/users/"+ row.id +"'>" + row.id + "</a>"
}},

这会将所有 <td> 包装在该锚标记中。不过,我想知道您是否 运行 会遇到 DataTable 自己的 ID 属性的问题。所以我不确定 row.id 是否会 return 你的 id 或 DataTables。如果你在 return 语句之前添加一个 console.log(row) 你应该找到你可以在渲染函数中操作的对象,所以如果 row.id 不是,你应该能够在那里找到你需要的东西return输入正确的 ID。

您可以使用来自控制器的 rawColumns api。检查这个:http://yajrabox.com/docs/laravel-datatables/master/raw-columns

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