Skip to content

Commit

Permalink
Add support for startsWithSearch filter.
Browse files Browse the repository at this point in the history
Fix #2161
  • Loading branch information
yajra committed Aug 30, 2019
1 parent d1bc415 commit 8ebc9f6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/DataTableAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,25 @@ public function whitelist($whitelist = '*')
/**
* Set smart search config at runtime.
*
* @param bool $bool
* @param bool $state
* @return $this
*/
public function smart($bool = true)
public function smart($state = true)
{
$this->config->set(['datatables.search.smart' => $bool]);
$this->config->set('datatables.search.smart', $state);

return $this;
}

/**
* Set starts_with search config at runtime.
*
* @param bool $state
* @return $this
*/
public function startsWithSearch($state = true)
{
$this->config->set('datatables.search.starts_with', $state);

return $this;
}
Expand Down
4 changes: 4 additions & 0 deletions src/QueryDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ protected function addTablePrefix($query, $column)
*/
protected function prepareKeyword($keyword)
{
if ($this->config->isStartsWithSearch()) {
return "$keyword%";
}

if ($this->config->isCaseInsensitive()) {
$keyword = Str::lower($keyword);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Utilities/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,14 @@ public function isMultiTerm()
{
return $this->repository->get('datatables.search.multi_term', true);
}

/**
* Check if dataTable config uses starts_with searching.
*
* @return bool
*/
public function isStartsWithSearch()
{
return $this->repository->get('datatables.search.starts_with', false);
}
}
6 changes: 6 additions & 0 deletions src/config/datatables.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
* SQL: column LIKE "%k%e%y%w%o%r%d%"
*/
'use_wildcards' => false,

/*
* Perform a search which starts with the given keyword.
* SQL: column LIKE "keyword%"
*/
'starts_with' => false,
],

/*
Expand Down

0 comments on commit 8ebc9f6

Please sign in to comment.