Skip to content

Commit c58f3a9

Browse files
committed
feat: add min search length control
fix: #3241
1 parent 14779dd commit c58f3a9

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/CollectionDataTable.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ public function paging(): void
143143
public function make(bool $mDataSupport = true): JsonResponse
144144
{
145145
try {
146+
$this->validateMinLengthSearch();
147+
146148
$this->totalRecords = $this->totalCount();
147149

148150
if ($this->totalRecords) {

src/DataTableAbstract.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ abstract class DataTableAbstract implements DataTable
122122

123123
protected bool $editOnlySelectedColumns = false;
124124

125+
protected int $minSearchLength = 0;
126+
125127
/**
126128
* Can the DataTable engine be created with these parameters.
127129
*
@@ -989,4 +991,27 @@ protected function getPrimaryKeyName(): string
989991
{
990992
return 'id';
991993
}
994+
995+
public function minSearchLength(int $length): static
996+
{
997+
$this->minSearchLength = $length;
998+
999+
return $this;
1000+
}
1001+
1002+
protected function validateMinLengthSearch(): void
1003+
{
1004+
if ($this->request->isSearchable()
1005+
&& $this->minSearchLength > 0
1006+
&& Str::length($this->request->keyword()) < $this->minSearchLength
1007+
) {
1008+
$this->totalRecords = 0;
1009+
$this->filteredRecords = 0;
1010+
throw new \Exception(
1011+
__('Please enter at least :length characters to search.', ['length' => $this->minSearchLength]),
1012+
400
1013+
);
1014+
}
1015+
}
1016+
9921017
}

src/QueryDataTable.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ public static function canCreate($source): bool
124124
public function make(bool $mDataSupport = true): JsonResponse
125125
{
126126
try {
127+
$this->validateMinLengthSearch();
128+
127129
$results = $this->prepareQuery()->results();
128130
$processed = $this->processResults($results, $mDataSupport);
129131
$data = $this->transform($results, $processed);

0 commit comments

Comments
 (0)