Skip to content

Commit 098a8d7

Browse files
authored
Merge branch 'yajra:master' into master
2 parents 19c8ebb + 66299d9 commit 098a8d7

File tree

7 files changed

+261
-69
lines changed

7 files changed

+261
-69
lines changed

CHANGELOG.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88

99
### [Unreleased]
1010

11+
### [v10.6.0] - 2023-06-29
12+
13+
- feat: Expose autoFilter setter to disable post filtering #2981
14+
15+
### [v10.5.0] - 2023-06-29
16+
17+
- feat: Prevent editColumn when column is not shown #3018
18+
19+
### [v10.4.4] - 2023-06-27
20+
21+
- feat: Optimize countQuery with complex select #3008
22+
- fix: phpstan #3022
23+
1124
### [v10.4.3] - 2023-06-07
1225

1326
- Fix: Prevent the filteredCount() query if no filter is applied to the initial query #3007
@@ -130,7 +143,10 @@
130143
- Drop support for `ApiResourceDataTable`
131144
- PHP8 syntax / method signature changed
132145

133-
[Unreleased]: https://github.com/yajra/laravel-datatables/compare/v10.3.1...10.x
146+
[Unreleased]: https://github.com/yajra/laravel-datatables/compare/v10.6.0...10.x
147+
[v10.6.0]: https://github.com/yajra/laravel-datatables/compare/v10.6.0...v10.5.0
148+
[v10.5.0]: https://github.com/yajra/laravel-datatables/compare/v10.5.0...v10.4.4
149+
[v10.4.4]: https://github.com/yajra/laravel-datatables/compare/v10.4.4...v10.4.3
134150
[v10.3.1]: https://github.com/yajra/laravel-datatables/compare/v10.3.1...v10.3.0
135151
[v10.3.1]: https://github.com/yajra/laravel-datatables/compare/v10.3.1...v10.3.0
136152
[v10.3.0]: https://github.com/yajra/laravel-datatables/compare/v10.3.0...v10.2.3

phpstan.neon.dist

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ parameters:
1010

1111
ignoreErrors:
1212
- '#Unsafe usage of new static\(\).#'
13+
- '#Negated boolean expression is always false.#'
1314

1415
excludePaths:
1516
- src/helper.php

src/DataTableAbstract.php

+45-18
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ abstract class DataTableAbstract implements DataTable
5252
* @var array
5353
*/
5454
protected array $columnDef = [
55-
'index' => false,
56-
'append' => [],
57-
'edit' => [],
58-
'filter' => [],
59-
'order' => [],
60-
'only' => null,
61-
'hidden' => [],
55+
'index' => false,
56+
'append' => [],
57+
'edit' => [],
58+
'filter' => [],
59+
'order' => [],
60+
'only' => null,
61+
'hidden' => [],
6262
'visible' => [],
6363
];
6464

@@ -103,10 +103,10 @@ abstract class DataTableAbstract implements DataTable
103103
* @var array
104104
*/
105105
protected array $templates = [
106-
'DT_RowId' => '',
106+
'DT_RowId' => '',
107107
'DT_RowClass' => '',
108-
'DT_RowData' => [],
109-
'DT_RowAttr' => [],
108+
'DT_RowData' => [],
109+
'DT_RowAttr' => [],
110110
];
111111

112112
/**
@@ -147,6 +147,8 @@ abstract class DataTableAbstract implements DataTable
147147

148148
protected mixed $transformer;
149149

150+
protected bool $editOnlySelectedColumns = false;
151+
150152
/**
151153
* Can the DataTable engine be created with these parameters.
152154
*
@@ -231,7 +233,13 @@ public function addIndexColumn(): static
231233
*/
232234
public function editColumn($name, $content): static
233235
{
234-
$this->columnDef['edit'][] = ['name' => $name, 'content' => $content];
236+
if ($this->editOnlySelectedColumns) {
237+
if (! count($this->request->columns()) || in_array($name, Arr::pluck($this->request->columns(), 'name'))) {
238+
$this->columnDef['edit'][] = ['name' => $name, 'content' => $content];
239+
}
240+
} else {
241+
$this->columnDef['edit'][] = ['name' => $name, 'content' => $content];
242+
}
235243

236244
return $this;
237245
}
@@ -584,6 +592,18 @@ public function skipPaging(): static
584592
return $this;
585593
}
586594

595+
/**
596+
* Skip auto filtering as needed.
597+
*
598+
* @return $this
599+
*/
600+
public function skipAutoFilter(): static
601+
{
602+
$this->autoFilter = false;
603+
604+
return $this;
605+
}
606+
587607
/**
588608
* Push a new column name to blacklist.
589609
*
@@ -867,10 +887,10 @@ protected function processResults($results, $object = false): array
867887
protected function render(array $data): JsonResponse
868888
{
869889
$output = $this->attachAppends([
870-
'draw' => $this->request->draw(),
871-
'recordsTotal' => $this->totalRecords,
890+
'draw' => $this->request->draw(),
891+
'recordsTotal' => $this->totalRecords,
872892
'recordsFiltered' => $this->filteredRecords ?? 0,
873-
'data' => $data,
893+
'data' => $data,
874894
]);
875895

876896
if ($this->config->isDebugging()) {
@@ -934,11 +954,11 @@ protected function errorResponse(\Exception $exception)
934954
$this->getLogger()->error($exception);
935955

936956
return new JsonResponse([
937-
'draw' => $this->request->draw(),
938-
'recordsTotal' => $this->totalRecords,
957+
'draw' => $this->request->draw(),
958+
'recordsTotal' => $this->totalRecords,
939959
'recordsFiltered' => 0,
940-
'data' => [],
941-
'error' => $error ? __($error) : "Exception Message:\n\n".$exception->getMessage(),
960+
'data' => [],
961+
'error' => $error ? __($error) : "Exception Message:\n\n".$exception->getMessage(),
942962
]);
943963
}
944964

@@ -1039,4 +1059,11 @@ protected function getPrimaryKeyName(): string
10391059
{
10401060
return 'id';
10411061
}
1062+
1063+
public function editOnlySelectedColumns(): static
1064+
{
1065+
$this->editOnlySelectedColumns = true;
1066+
1067+
return $this;
1068+
}
10421069
}

src/QueryDataTable.php

+33-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Query\Expression;
99
use Illuminate\Http\JsonResponse;
1010
use Illuminate\Support\Collection;
11+
use Illuminate\Support\Facades\DB;
1112
use Illuminate\Support\Str;
1213
use Yajra\DataTables\Utilities\Helper;
1314

@@ -48,6 +49,13 @@ class QueryDataTable extends DataTableAbstract
4849
*/
4950
protected bool $keepSelectBindings = false;
5051

52+
/**
53+
* Flag to ignore the selects in count query.
54+
*
55+
* @var bool
56+
*/
57+
protected bool $ignoreSelectInCountQuery = false;
58+
5159
/**
5260
* @param QueryBuilder $builder
5361
*/
@@ -156,10 +164,20 @@ public function prepareCountQuery(): QueryBuilder
156164
$builder = clone $this->query;
157165

158166
if ($this->isComplexQuery($builder)) {
167+
$builder->select(DB::raw('1'));
168+
if ($this->ignoreSelectInCountQuery || ! $this->isComplexQuery($builder)) {
169+
return $this->getConnection()
170+
->query()
171+
->fromRaw('('.$builder->toSql().') count_row_table')
172+
->setBindings($builder->getBindings());
173+
}
174+
175+
$builder = clone $this->query;
176+
159177
return $this->getConnection()
160-
->query()
161-
->fromRaw('('.$builder->toSql().') count_row_table')
162-
->setBindings($builder->getBindings());
178+
->query()
179+
->fromRaw('('.$builder->toSql().') count_row_table')
180+
->setBindings($builder->getBindings());
163181
}
164182

165183
$row_count = $this->wrap('row_count');
@@ -819,4 +837,16 @@ public function getFilteredQuery(): QueryBuilder
819837

820838
return $this->getQuery();
821839
}
840+
841+
/**
842+
* Ignore the selects in count query.
843+
*
844+
* @return $this
845+
*/
846+
public function ignoreSelectsInCountQuery(): static
847+
{
848+
$this->ignoreSelectInCountQuery = true;
849+
850+
return $this;
851+
}
822852
}

src/Utilities/Request.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ public function getBaseRequest(): BaseRequest
254254
*/
255255
public function start(): int
256256
{
257-
return intval($this->request->input('start', 0));
257+
$start = $this->request->input('start', 0);
258+
259+
return is_numeric($start) ? intval($start) : 0;
258260
}
259261

260262
/**
@@ -264,7 +266,9 @@ public function start(): int
264266
*/
265267
public function length(): int
266268
{
267-
return intval($this->request->input('length', 10));
269+
$length = $this->request->input('length', 10);
270+
271+
return is_numeric($length) ? intval($length) : 10;
268272
}
269273

270274
/**
@@ -274,6 +278,8 @@ public function length(): int
274278
*/
275279
public function draw(): int
276280
{
277-
return intval($this->request->input('draw', 0));
281+
$draw = $this->request->input('draw', 0);
282+
283+
return is_numeric($draw) ? intval($draw) : 0;
278284
}
279285
}

0 commit comments

Comments
 (0)