Skip to content

Commit 6bc466f

Browse files
committed
https://github.com/yajra/laravel-datatables/issues/2493
This will insert lengthMenu parameter according to max_record_per_page in config file. This will not affect manual lengthMenu insertion in "builder parameters". PSR2 CS Fix.
1 parent a6e5e72 commit 6bc466f

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/Html/Builder.php

+26-3
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ public function generateScripts()
119119
public function generateJson()
120120
{
121121
$args = array_merge(
122-
$this->attributes, [
122+
$this->attributes,
123+
[
123124
'ajax' => $this->ajax,
124125
'columns' => $this->collection->map(function (Column $column) {
125126
$column = $column->toArray();
@@ -143,6 +144,26 @@ public function parameterize($attributes = [])
143144
{
144145
$parameters = (new Parameters($attributes))->toArray();
145146

147+
$max_record_per_page = $this->config->get('datatables.max_record_per_page', 0);
148+
if (!array_key_exists('lengthMenu', $parameters)) {
149+
$parameters['lengthMenu'] = [10, 25, 50, 100];
150+
}
151+
152+
if ($max_record_per_page != 0) {
153+
$lengthMenu = array_unique($parameters['lengthMenu']);
154+
foreach ($lengthMenu as $key => $value) {
155+
if ($value > $max_record_per_page) {
156+
unset($lengthMenu[$key]);
157+
}
158+
}
159+
$lengthMenu = array_values($lengthMenu);
160+
if (empty($lengthMenu)) {
161+
$lengthMenu = [$max_record_per_page];
162+
}
163+
sort($lengthMenu);
164+
$parameters['lengthMenu'] = $lengthMenu;
165+
}
166+
146167
$values = [];
147168
$replacements = [];
148169

@@ -212,8 +233,10 @@ public function table(array $attributes = [], $drawFooter = false, $drawSearch =
212233
$htmlAttr = $this->html->attributes($this->tableAttributes);
213234

214235
$tableHtml = '<table ' . $htmlAttr . '>';
215-
$searchHtml = $drawSearch ? '<tr class="search-filter">' . implode('',
216-
$this->compileTableSearchHeaders()) . '</tr>' : '';
236+
$searchHtml = $drawSearch ? '<tr class="search-filter">' . implode(
237+
'',
238+
$this->compileTableSearchHeaders()
239+
) . '</tr>' : '';
217240
$tableHtml .= '<thead><tr>' . implode('', $th) . '</tr>' . $searchHtml . '</thead>';
218241
if ($drawFooter) {
219242
$tf = $this->compileTableFooter();

0 commit comments

Comments
 (0)