Laravel AJAX分页从旧数据中调出行
我有一个表格,我想在点击按钮后按字母顺序从Z排序 这个很好用。一开始我遇到了问题,我返回的分页链接不正确。修正了这些,所以我再也没有404页被扔给我了。但现在当我点击下一页按钮。该表将转到旧无序数据的下一页 我的排序功能:Laravel AJAX分页从旧数据中调出行,ajax,laravel,laravel-pagination,Ajax,Laravel,Laravel Pagination,我有一个表格,我想在点击按钮后按字母顺序从Z排序 这个很好用。一开始我遇到了问题,我返回的分页链接不正确。修正了这些,所以我再也没有404页被扔给我了。但现在当我点击下一页按钮。该表将转到旧无序数据的下一页 我的排序功能: function Sorting($column, $direction) { $data = $this->db->GetAll("mid"/* table */, "40" /* rows for pagination*/, true/*pagina
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>
更新
这就是我的控制器功能现在的样子
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.
}
在我看来,我的paginator如下所示:
@if(!isset($pager))
{{ $Leads->links("pagination::bootstrap-4")}}
@elseif(isset($pager))
{{$pager}}
@endif
这将加载到新的paginator中,我知道这是另一个,因为它没有使用Bootstrap4设置样式
分页器将使用控制器中设置的选项加载。但是它仍然没有显示数据。这是因为
{{$Leads->links(“pagination::bootstrap-4”)}
根据旧参数创建链接。首先,
$direction
参数是'asc',因此Laravel根据这些参数创建链接。您应该添加到可变方向参数中。像这样的<代码>{{$Leads->appends(['sort'=>'desc'])->links()}
我不知道在ajax请求之后怎么做。可能您只是在单击排序按钮时用新的
参数刷新页面
你应该这样做
success: function (data) {
var rows = data.rows
var paginator_links = data.links // you will produce this in backend
$("#tablecont").empty();
// $("#tablecont").append(data);
$("#tablecont").append(rows);
// here you will replace links
$('.pagination').empty();
$('.pagination').append(links);
}
我更改了排序请求的请求类型以获取。我将paginator的url更改为:domain.nl/dashboard/beheer/marketing/sortleads
let sorter = 'desc';
function Sort(identifier){
if(sorter === 'desc'){
sorter = 'asc';
} else if(sorter === 'desc'){
sorter = 'asc';
}
if(identifier == "MID"){
identifier = "id";
}
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: '/dashboard/beheer/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);
}
});
}
我检查了ajax请求在AjaxController中激发的函数中发送了哪些参数
我的AjaxController中的排序功能
public function LeadSort(){
if(isset($_GET['id'])){
$this->marketingController->Sorting($_GET['id'], $_GET['sorter']);
} else {
$paginator = \Session::get('pager');
$arrayoptions = \Session::get('data');
$data = $this->db->GetAll("mid", "50" , true, true, $arrayoptions['col'], $arrayoptions['dir'] );
$Fields = \Session::get('fields');
return view("Beheer.marketing")->with('Leads', $data)->with('MidTableFields', $Fields)->with('pager', $paginator)->render();
}
}
在这里,我检查是否正在发送ID。如果是的话。这意味着这是第一次排序。ID是指列标题名称。所以,如果单击公司名称,它将按公司名称排序
我检查$\u GET['id']
设置了哪些列,并检查$\u GET['sorter']
需要排序的方向。我将这2个值放入2个会话变量中,以便稍后在MarketingController的分类器功能中使用。我生成已排序的数据并将其放入html表中,在这个函数中,我还加载正在排序的表的表字段名,加载/呈现分页器并将其放入会话变量中
最后,我使用数据并在视图中渲染它。它将由Ajax Success函数返回并追加
下面是MarketingController.php的Sorter函数
function Sorting($column, $direction)
{
$data = $this->db->GetAll("mid", "50", true, true,"$column", "$direction");
$Fields = $this->db->GetFieldnames("mid", false);
$paginator = $data->appends(['sort' => $direction ])->render("pagination::bootstrap-4");
\Session::put('pager', $paginator);
\Session::put('data', ["col" => $column, "dir" => $direction]);
\Session::put('fields', $Fields);
$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.
}
但是如果我们想在桌子上翻页呢?单击分页按钮时,没有发送ID。因此,它将尝试获取我在会话变量中输入的所有值。我重复使用它们,获取数据并呈现新视图。这将生成第二页或分页器单击的任何页面。它还将附加Ajax成功函数
此解决方案的缺点:
我还没有弄明白为什么以及如何解决这个解决方案产生的问题。但现在的问题是:如果按下分页器的第三个按钮。将加载正确的数据。但不会更新分页器以激活第三个按钮
生下穆罕默德·卡卡巴耶夫的巨大支柱。感谢您帮助我走出困境,让我走上通往完整解决方案的正确道路 我会调查的。刚刚看到你的答案出现在我的收件箱里。我会在以后评论我的结果:)谢谢!只是为了测试和以后正确实现它。我在tableLeads视图中将分页器设置为:{$Leads->appends([“sort”=>'desc'])->links(“分页::引导-4”)}
结果仍然相同。它仍然会获取旧数据并返回第二页。单击我的排序按钮,然后单击分页器。我知道。这就是为什么我说也许你们应该刷新页面。这是因为您在页面加载后更新了表。当页面加载时,php代码将被编译。和{{$Leads->appends(['sort'=>'desc'])->links()}这将生成带有链接的结果,其中'sort'是'desc',并且无论您使用javascript做什么,它都保持不变。这是我的建议。当您使用ajax加载数据时,在后端生成{{$Leads->appends(['sort'=>'desc'])->links()}。并在前端更换它。不知道我能不能解释一下。对不起,英语不好,请看我的最新问题。我认为这是动态实现它的方法。但是它仍然不起作用。。分页器确实被更换了。但当加载另一个页面时,它仍然没有显示我想要的数据。。
function Sorting($column, $direction)
{
$data = $this->db->GetAll("mid", "50", true, true,"$column", "$direction");
$Fields = $this->db->GetFieldnames("mid", false);
$paginator = $data->appends(['sort' => $direction ])->render("pagination::bootstrap-4");
\Session::put('pager', $paginator);
\Session::put('data', ["col" => $column, "dir" => $direction]);
\Session::put('fields', $Fields);
$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.
}