Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added multiple search #2261

Open
wants to merge 6 commits into
base: 9.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 41 additions & 9 deletions src/CollectionDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class CollectionDataTable extends DataTableAbstract
*/
public $collection;

/**
* Collection object.
*
* @var \Illuminate\Support\Collection
*/
public $merged;

/**
* Collection object.
*
Expand Down Expand Up @@ -66,6 +73,7 @@ public function __construct(Collection $collection)
$this->request = app('datatables.request');
$this->config = app('datatables.config');
$this->collection = $collection;
$this->merged = collect();
$this->original = $collection;
$this->columns = array_keys($this->serialize($collection->first()));
}
Expand Down Expand Up @@ -216,16 +224,9 @@ private function revertIndexColumn($mDataSupport)
}
}

/**
* Perform global search for the given keyword.
*
* @param string $keyword
*/
protected function globalSearch($keyword)
protected function search($keyword)
{
$keyword = $this->config->isCaseInsensitive() ? Str::lower($keyword) : $keyword;

$this->collection = $this->collection->filter(function ($row) use ($keyword) {
return $this->collection->filter(function ($row) use ($keyword) {
$this->isFilterApplied = true;

$data = $this->serialize($row);
Expand All @@ -241,6 +242,7 @@ protected function globalSearch($keyword)
}

$value = $this->config->isCaseInsensitive() ? Str::lower($value) : $value;

if (Str::contains($value, $keyword)) {
return true;
}
Expand All @@ -250,6 +252,36 @@ protected function globalSearch($keyword)
});
}

/**
* Perform global search for the given keyword.
*
* @param string $keyword
*/
protected function globalSearch($keyword)
{
$keyword = $this->config->isCaseInsensitive() ? Str::lower($keyword) : $keyword;

$this->collection = $this->search($keyword);
}

/**
* Perform multiple search for the given keyword.
*
* @param string $keyword
*/
protected function multiSearch($keywords)
{
$keywords->each(function ($keyword) {
$keyword = $this->config->isCaseInsensitive() ? Str::lower($keyword) : $keyword;

$mergedCollection = $this->search($keyword);

$this->merged = $this->merged->merge($mergedCollection);
});

$this->collection = $this->merged->unique();
}

/**
* Perform default query orderBy clause.
*/
Expand Down
48 changes: 48 additions & 0 deletions src/DataTableAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,32 @@ public function startsWithSearch($state = true)
return $this;
}

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

return $this;
}

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

return $this;
}

/**
* Set total records manually.
*
Expand Down Expand Up @@ -655,6 +681,12 @@ public function filtering()
{
$keyword = $this->request->keyword();

if ($this->config->isMultiple()) {
$this->smartMultiSearch($keyword);

return;
}

if ($this->config->isMultiTerm()) {
$this->smartGlobalSearch($keyword);

Expand All @@ -681,6 +713,22 @@ protected function smartGlobalSearch($keyword)
});
}

/**
* Perform multiple search by splitting keyword into
* individual words and searches for each of them.
*
* @param string $keyword
*/
protected function smartMultiSearch($keyword)
{
$this->multiSearch(
collect(explode(' ', $keyword))
->reject(function ($keyword) {
return trim($keyword) === '';
})
);
}

/**
* Perform global search for the given keyword.
*
Expand Down
53 changes: 40 additions & 13 deletions src/QueryDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,33 +703,60 @@ protected function getNullsLastSql($column, $direction)
);
}

/**
* Perform global search for the given keyword.
*
* @param string $keyword
*/
protected function globalSearch($keyword)
protected function search($keyword, $multiple = false)
{
$this->query->where(function ($query) use ($keyword) {
$this->query->where(function ($query) use ($keyword, $multiple) {
collect($this->request->searchableColumnIndex())
->map(function ($index) {
return $this->getColumnName($index);
})
->reject(function ($column) {
return $this->isBlacklisted($column) && ! $this->hasFilterColumn($column);
})
->each(function ($column) use ($keyword, $query) {
if ($this->hasFilterColumn($column)) {
$this->applyFilterColumn($query, $column, $keyword, 'or');
->each(function ($column) use ($keyword, $query, $multiple) {
if ($multiple) {
$keyword->each(function ($keyword) use ($query, $column) {
if ($this->hasFilterColumn($column)) {
$this->applyFilterColumn($query, $column, $keyword, 'or');
} else {
$this->compileQuerySearch($query, $column, $keyword);
}

$this->isFilterApplied = true;
});
} else {
$this->compileQuerySearch($query, $column, $keyword);
}
if ($this->hasFilterColumn($column)) {
$this->applyFilterColumn($query, $column, $keyword, 'or');
} else {
$this->compileQuerySearch($query, $column, $keyword);
}

$this->isFilterApplied = true;
$this->isFilterApplied = true;
}
});
});
}

/**
* Perform global search for the given keyword.
*
* @param string $keyword
*/
protected function globalSearch($keyword)
{
$this->search($keyword);
}

/**
* Perform multiple search for the given keyword.
*
* @param string $keyword
*/
protected function multiSearch($keywords)
{
$this->search($keywords, true);
}

/**
* Append debug parameters on output.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Utilities/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ public function isMultiTerm()
return $this->repository->get('datatables.search.multi_term', true);
}

/**
* Check if dataTable config uses multiple searching.
*
* @return bool
*/
public function isMultiple()
{
return $this->repository->get('datatables.search.multiple', false);
}

/**
* Check if dataTable config uses starts_with searching.
*
Expand Down
6 changes: 6 additions & 0 deletions src/config/datatables.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
*/
'multi_term' => true,

/*
* Multiple search will explode search keyword using spaces resulting into multiple search.
*/
'multiple' => false,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multi_term already explodes the keyword using spaces.

    /**
     * Perform multi-term search by splitting keyword into
     * individual words and searches for each of them.
     *
     * @param string $keyword
     */
    protected function smartGlobalSearch($keyword)
    {
        collect(explode(' ', $keyword))
            ->reject(function ($keyword) {
                return trim($keyword) === '';
            })
            ->each(function ($keyword) {
                $this->globalSearch($keyword);
            });
    }


/*
* Case insensitive will search the keyword in lower case format.
* SQL: LOWER(column) LIKE LOWER(keyword)
Expand Down Expand Up @@ -66,6 +71,7 @@
/*
* Nulls last sql pattern for PostgreSQL & Oracle.
* For MySQL, use 'CASE WHEN :column IS NULL THEN 1 ELSE 0 END, :column :direction'
* For PostgreSQL & Oracle use ':column :direction NULLS LAST'
*/
'nulls_last_sql' => ':column :direction NULLS LAST',

Expand Down