我有一个表格,一旦单击按钮,我想按 ZA 字母顺序订购其内容。
这工作正常。起初我遇到的问题是我返回的分页链接不正确。修复了这些,所以我不再收到 404 页面了。但是现在当我单击下一页按钮时。该表将转到旧无序数据的下一页。
我的排序功能:
function Sorting($column, $direction)
{
$data = $this->db->GetAll("mid"/* table */, "40" /* rows for pagination*/, true/*paginate*/, true,/*sorting*/ "$column" /* column to be sorted on*/, "$direction" /* ASC or DESc */);
$Fields = $this->db->GetFieldnames("mid", false); // Field names for the table.
$data->setPath("http://localhost:8000/dashboard/beheer/marketing"); // setting the correct path for the pagination otherwise an 404 error would be thrown.
$returnview = view("Beheer.Dashboard.components.Marketing.tableLeads")->with('Leads', $data)->with('MidTableFields', $Fields)->render(); // returning it to the view.
echo json_encode($returnview); // returning it to the Ajax call.
}
我的观点:
<?php
/**
* Created by PhpStorm.
* User: Entric
* Date: 14-6-2018
* Time: 15:43
*/
?>
<div>
<table class="table" id="tablemid">
<thead>
<tr>
@foreach($MidTableFields as $field)
@if($field === 'id')
@php
$field = 'MID';
@endphp
@endif
<th id="{{$field}}"><a onclick="Sort(this.id)" href="#
{{$field}}" id="{{$field}}">{{$field}}</a></th>
@endforeach
</tr>
</thead>
<tbody id="LeadsContentTable" style=>
@foreach($Leads->reverse() as $lead)
@if($lead->Actief == 1)
@php
$active = 'Ja';
@endphp
@else
@php
$active = 'Nee';
@endphp
@endif
<tr class="tableRow" id="row{{$lead->id}}">
<td id="mid">
{{$lead->id}}
</td>
<td id="company">
{{$lead->Bedrijfsnaam}}
</td>
<td id="contact">
{{$lead->Contactpersoon}}
</td>
<td id="email">
{{$lead->Email}}
</td>
<td id="tel">
{{$lead->Telefoon}}
</td>
<td id="ip">
{{$lead->IP}}
</td>
<td id="active">
{{$active}}
</td>
<td id="note">
{{$lead->Notitie}}
</td>
<td>
<a href="#" id="{{$lead->id}}" onclick="EditLead(this.id);" data-toggle="modal"
data-target="#LeadModalEdit">
<button class="btn btn-neutral btn-icon btn-round button">
<i class="material-icons" style="color:rgba(223,176,0,0.79)">mode_edit</i>
</button>
</a>
</td>
<td>
<a href="#" id="{{$lead->id}}" onclick="GetDeleteLead(this.id);" data-toggle="modal"
data-target="#LeadModalDelete">
<button class="btn btn-neutral btn-icon btn-round button">
<i class="material-icons" style="color:rgba(185,14,22,0.81)">clear</i>
</button>
</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="pagination">
{{ $Leads->links("pagination::bootstrap-4")}}
</div>
启动排序的 JS 函数:
function Sort(identifier){
let sorter = 'asc';
if(sorter === 'asc'){
sorter = 'desc';
} else if(sorter === 'desc'){
sorter = 'asc';
}
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: '/SortLeads',
type: 'GET',
dataType: "json",
beforeSend: function (xhr) {
const token = jQuery('meta[name="csrf_token"]').attr('content');
if (token) {
return xhr.setRequestHeader('X-CSRF-TOKEN', token);
}
},
data: {id: identifier, sorter: sorter},
success: function (data) {
$("#tablecont").empty();
$("#tablecont").append(data);
}
});
}
更新
这就是我的控制器功能现在的样子。
function Sorting($column, $direction)
{
$data = $this->db->GetAll("mid"/* table */, "40" /* rows for pagination*/, true/*paginate*/, true,/*sorting*/"$column" /* column to be sorted on*/, "$direction" /* ASC or DESc */);
$Fields = $this->db->GetFieldnames("mid", false); // Field names for the table.
$paginator = $data->appends(['sort' => $direction ])->setPath("http://localhost:8000/dashboard/beheer/marketing")->links();
$returnview = view("Beheer.Dashboard.components.Marketing.tableLeads")->with('Leads', $data)->with('MidTableFields', $Fields)->with('pager', $paginator)->render(); // returning it to the view.
echo json_encode($returnview); // returning it to the Ajax call.
}
在我看来,我的分页器看起来像:
@if(!isset($pager))
{{ $Leads->links("pagination::bootstrap-4")}}
@elseif(isset($pager))
{{$pager}}
@endif
这会加载到新的分页器中,我知道它是另一个分页器,因为它没有使用 Bootstrap 4 进行样式设置。
分页器使用控制器中设置的选项加载。但它仍然没有显示数据..