【发布时间】:2020-03-18 09:07:50
【问题描述】:
我正在使用数据表 v1.10.13。我在根据 laravel 的 created_at 显示数据时遇到问题。在获取数据时,我根据 created_at desc 获取帖子,但在显示数据时,它按字母顺序显示数据。如何使用数据表首先获取最新帖子?我没有使用 ajax 填充数据。
我有这个查询来获取 PostController 中的帖子:
$allPost = $this->post->orderBy('created_at', 'desc')->get();
我在 html 中有以下代码:
<div class="dt-responsive table-responsive">
<table id="posts-data" class="table table-striped table-bordered nowrap dataTable no-footer" role="grid" aria-describedby="basic-col-reorder_info">
<thead>
<tr>
<th>Title</th>
<th>Category</th>
<th>Status</th>
<th>Image</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@if(!empty($allPost)) @foreach($allPost as $postsLists)
<tr>
<td>{{ $postsLists->title }}</td>
<td>
@if(!empty($postsLists->categories)) @foreach($postsLists->categories as $cat_lists)
<i class="icofont icofont-arrow-right"></i> {{$cat_lists->name}}
<br> @endforeach @endif
</td>
<td>{{ $postsLists->status }}</td>
<td><img src="{{ $postsLists->image }}" alt="" width="100"></td>
<td><a href="{{ $postsLists->image }}" target="_blank">View image</a></td>
<td>
<a href="{{ route('posts.edit', $postsLists->id) }}" class="btn btn-primary btn-sm pull-left" style="margin-right: 5px">
<span class="icofont icofont-ui-edit"></span>
</a>
<a class="pull-left" onclick="return confirm('Are you sure you want to delete this post?')">
<form method="POST" action="{{ route('posts.destroy', $postsLists->id) }}" accept-charset="UTF-8">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="{{ csrf_token() }}">
<button class="btn btn-danger btn-sm" type="submit"><span class="icofont icofont-ui-delete"></span></button>
</form>
</a>
</td>
</tr>
@endforeach @endif
</tbody>
</table>
我有以下代码来调用初始化数据表:
$('#posts-data').DataTable({
colReorder: true,
pageLength: 0,
lengthMenu: [20, 40, 60, 80, 90, 100],
});
【问题讨论】:
-
剂量
$allPost有正确的顺序吗? -
是的,它们的顺序正确。
标签: laravel datatables datatables-1.10