gpt4 book ai didi

php - 使用 yajra 数据表作为服务器端数据表

转载 作者:行者123 更新时间:2023-12-01 03:34:53 26 4
gpt4 key购买 nike

我正在使用 yajra 数据表作为服务器端数据表我的 Controller 是这个

public static function alleventsData(Request $request)
{
$limit = intVal($request->input('length'));
$start = $request->input('start');
$meta = EventsRepository::showMeta();
$totalRecords = $meta[1][1]['Value'];
$offset = intVal($start);
$allEvents = EventsRepository::allEvents($offset, $limit);
return Datatables::collection($allEvents)
->addColumn(
'parent',
function ($allEvents) {
return $allEvents['category_name'];
}
)
->addColumn(
'venueName',
function ($allEvents) {
return $allEvents['venue_name'];
}
)
->addColumn(
'venueLocation',
function ($allEvents) {
return $allEvents['location'];
}
)
->addColumn(
'occurs_at',
function ($allEvents) {
return $allEvents['occurs_at'];
}
)
->addColumn(
'hot_popular_main',
function ($allEvents) {
return '<input type="checkbox" name="hot_popular_main" class= "updatePopertyEvent" attr="hot_popular_main" id="'.$allEvents['id'].'" value="'.$allEvents['hot_popular_main'].'" '.($allEvents['hot_popular_main']==1?'checked="checked"':'').'/>';
}
)
->addColumn(
'synchronize',
function ($allEvents) {
return '<button value="'.$allEvents['id'].'" class="btn btn-info synchronize" >Synchronize</button>';
}
)
->addColumn(
'status',
function ($allEvents) {
$status = $allEvents['status']==1?"Active":"Deactive";
return '<button value="'.$allEvents['id'].'" class="btn btn-info status" data-attr="'.$allEvents['status'].'">'.$status.'</button>';
}
)
->with(['recordsTotal'=>$totalRecords, 'recordsFiltered'=>$totalRecords])
->make(true);
}

我的js是这个

    $(function() {
$('.eventTableAll').DataTable({
processing: true,
serverSide: true,
ajax: '{!! route('datatables.alleventsData') !!}',
columns: [
{ data: 'event_name', name: 'event_name' },
{ data: 'parent', name: 'parent', searchable: true },
{ data: 'venueName', name: 'venueName', searchable: true },
{ data: 'venueLocation', name: 'venueLocation', searchable: true },
{ data: 'occurs_at', name: 'occurs_at', searchable: true },
{ data: 'hot_popular_main', name: 'hot_popular_main' },
{ data: 'synchronize', name: 'synchronize' },
{ data: 'status', name: 'status' }

]
});
});

但问题是,当我像第二个一样移动到下一页时,它没有获取任何数据,我看到控制台正在获取数据,但没有嵌入数据表数据索引中。

最佳答案

两天前我遇到了完全相同的问题。在这里,您将手动获取带有限制和偏移量的记录并使用 Datatables::collection()。

首先,如果您在 Laravel 设置中为表单启用了 csrf:

  1. 禁用它。在 Kernel.php 中注释 \App\Http\Middleware\VerifyCsrfToken::class
  2. 或者在datatable js函数之前在ajax header 中设置csrf

    $.ajaxSetup({
    headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
    });

如果这样做了。仍然无法正常工作,然后继续。

<小时/>

解决方案1:在将获取的数据传递给 Datatables::collection() 之前,使用以下命令:

$allEvents = collect(array_pad($allEvents->toArray(), -($iDisplayStart + count($allEvents->toArray())), 0));

这一行为您提供响应 Datatables::collection() 的数据,如下所示:对于第 1 页:数据 = [ 0=>[event_0], 1=>[event_1], ... limit=>[event_limit] ];

对于第 n 页:数据 = [ 0=>[], 1=>[], ... offset-1=?[], offset=>[event_0], offset+1=>[event_1], ... offset+limit-1 =>[event_limit]];

除了在集合中的偏移量之前创建空索引之外什么都没有。这是因为 collection() 或 of() [或任何其他预构建函数] 考虑 $iDisplayStart 并从 index=$iDisplayStart 开始在集合中查找数据。

最后

return Datatable::of($allEvents)
->with(['recordsTotal' => $allEventsCount, 'recordsFiltered' => $allEventsCount, 'start' => $iDisplayStart])
->make();
<小时/>

解决方案 2:使用 yajra 的预构建函数代替手动实现分页:

return Datatables::of($allEvents)
->filter(function ($allEvents) use ($request, $iDisplayLength, $iDisplayOffset) {
$allEvents->skip(($iDisplayOffset) * $iDisplayLength)
->take($iDisplayLength);
return $allEvents;
})
->with(['recordsTotal' => $allEventsCount, 'recordsFiltered' => $allEventsCount, 'start' => $iDisplayStart])
->make();

引用:https://datatables.yajrabox.com/eloquent/post-column-search

<小时/>

注意:如果上述解决方案不起作用,请注意以下所有事项:

  • 检查您是否已包含适用于 yajra 的最新受支持版本的 datatables js 库
  • 检查是否从数据库获取指定限制的数据
  • 检查您正在进行的 ajax 数据表调用是否属于 POST 类型
  • 尝试将这些 ajax 参数切换为 true/false:处理、服务器端。如果在某些旧版本的数据表库中发送 ajax 参数,这些参数必须设置为 true
  • 检查在这两种情况下返回数据表中是否有这些行 ->with(['recordsTotal' => $allEventsCount, 'recordsFiltered' => $allEventsCount, 'start' => $iDisplayStart])
  • 请注意,$allEventsCount 是数据库中所有记录的计数。您需要使用 ->count() 手动获取它
<小时/>

解决方案3:

如果上述解决方案不起作用,最后一个解决方案是放弃 Yajra 并使用客户端数据表和使用 datatables.js 进行分页的代码。这不会导致性能下降,但您将需要进行更多的客户端编码。缓存也可用于客户端数据表。

引用:https://datatables.net/examples/server_side/pipeline.html
datatables.net/examples/data_sources/server_side.html

关于php - 使用 yajra 数据表作为服务器端数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37341402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com