[英]Laravel DataTables does not render HTML
我正在使用 Laravel Datatables 7,但我的表沒有呈現 HTML 代碼。 它之前正在渲染 HTML,但是當我將新的 Laravel DataTables 從 6 更新為 7 時,它停止在列中渲染 HTML。 http://prntscr.com/e11n84
這是 Laravel DataTables 6 - http://prntscr.com/e11ph0
$(function() {
$('#users-table').DataTable({
processing: true,
serverSide: true,
ajax: {
url: '{{ route("admin.access.user.get") }}',
type: 'post',
data: {status: 1, trashed: false}
},
columns: [
{data: 'id', name: '{{config('access.users_table')}}.id'},
{data: 'name', name: '{{config('access.users_table')}}.name', render: $.fn.dataTable.render.text()},
{data: 'email', name: '{{config('access.users_table')}}.email', render: $.fn.dataTable.render.text()},
{data: 'confirmed', name: '{{config('access.users_table')}}.confirmed'},
{data: 'roles', name: '{{config('access.roles_table')}}.name', sortable: false},
{data: 'created_at', name: '{{config('access.users_table')}}.created_at'},
{data: 'updated_at', name: '{{config('access.users_table')}}.updated_at'},
{data: 'actions', name: 'actions', searchable: false, sortable: false}
],
order: [[0, "asc"]],
searchDelay: 500
});
});
嘗試使用toJson()
方法從控制器以 JSON 形式提供數據。
$data= User::all();
return datatables()->of($data)
->addColumn('action', function ($row) {
$html = '<a href="/users/'.$row->id.'">Edit</a> ';
$html .= '<button data-rowid="'.$row->id.'">Del</button>';
return $html;
})->toJson();
//im also using yajra data tables..
//it's easy to render html from controller...
example:
$query = Appointment::all();
$table = Datatables::of($query);
$table->editColumn('user_id', function ($row) {
return $row->user_id ? '<span style="color: green;">ACCEPTED</span>' :
'<span style="color: red;">PENDING</span>';
});
//then you need to add the column you want to render html
$table->rawColumns(['user_id']);
添加帶有 html 的列,如其他答案所示,然后在->rawColumns(['html_column', 'another_html_column'])
之前添加此->rawColumns(['html_column', 'another_html_column'])
->toJason()
以呈現服務器發送的所有 html 列
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.