0

我试图使链接到另一个视图的 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>

路线网:

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

2 回答 2

5

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

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

这将包装<td>该锚标记中的所有内容。不过,我想知道您是否会遇到 DataTable 自己的 ID 属性的问题。所以我不确定 row.id 是否会返回您的id 或 DataTables。如果您在 return 语句之前添加 a ,您应该找到可以在渲染函数中操作的对象,因此如果没有返回正确的 id console.log(row),您应该能够在其中找到所需的内容。row.id

于 2019-02-22T14:55:18.693 回答
5

您可以使用控制器中的 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);
于 2019-06-21T12:09:14.160 回答