【发布时间】:2020-01-30 08:22:09
【问题描述】:
我在 DataTable 中使用服务器端渲染。
我有一个变量将limit 确定为数据库中的行数。
我正在尝试获取例如 100 行,并从页面长度为 25 的 DataTable 中将其分页到 4 页面。
此外,如果限制变量为4 或10,或小于pagelength 值的任何值,则所有记录都将显示在single 页面中。
这是我的代码:
#get datatable params
$draw = $this->request->input('draw');
$start = $this->request->input('start');
$dt_limit = $this->request->input('length'); // datatable pagination limit
$limit_rows = $this->request->input('limit');
# query
$result_product_urls = DB::table('product_match_unmatches')
->select('r_product_url', 'r_image_url_main')
->selectRaw('count(distinct s_product_url, r_product_url) as frequency,
GROUP_CONCAT(CONCAT(s_image_url, "&s_product_url=", s_product_url) SEPARATOR " | ") as source_products')
->groupBy('r_product_url')
->where('request_id', '=', $reqId)
->orderBy('frequency', 'DESC')
->take($limit_rows);
$recordsTotal = $result_product_urls->get()->count();
$urls = $result_product_urls->offset($start)->limit($dt_limit)->get();
这将始终从数据库中获取 25 行,因为 dataTable 页面长度为 25。
但我需要根据$limit_rows 值而不是$dt_limit 来限制行。
如果我只做$urls = $result_product_urls->get();,那么如果$limit_rows的值为100,那么所有100的行都会显示在同一页面中。
如何从数据库中获取limit 行,然后对其进行分页?
我希望我的帖子清楚。
【问题讨论】:
-
澄清一下,在您获取 100 行和页面长度 25 的示例中,您是否希望数据表在您通过 100 分页时不进行 ajax 调用?默认情况下,如果 server-side=true,点击下一页会导致 ajax 请求。这可以解决,但我想确定这就是你要问的。
-
@Aaron 的行数应该由用户作为请求参数传入的限制值定义,而不是由数据表的限制和偏移参数定义。但在我的情况下,限制功能被数据表的参数覆盖。
标签: php mysql laravel datatables server-side-rendering