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 1 commit
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
51 changes: 51 additions & 0 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 @@ -250,6 +258,49 @@ protected function globalSearch($keyword)
});
}

/**
* Perform multiple search for the given keyword.
*
* @param string $keyword
*/
protected function multiSearch($keywords)
Copy link
Owner

Choose a reason for hiding this comment

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

This seems like a duplicate of globalSearch method.

{
if (is_array($keywords)) {
foreach ($keywords as $keyword) {
$keyword = $this->config->isCaseInsensitive() ? Str::lower($keyword) : $keyword;

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

$data = $this->serialize($row);
foreach ($this->request->searchableColumnIndex() as $index) {
$column = $this->getColumnName($index);
$value = Arr::get($data, $column);
if (! $value || is_array($value)) {
if (! is_numeric($value)) {
continue;
}

$value = (string) $value;
}

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

if (Str::contains($value, $keyword)) {
return true;
}
}

return false;
});

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

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

/**
* Perform default query orderBy clause.
*/
Expand Down
19 changes: 18 additions & 1 deletion src/DataTableAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,9 +655,15 @@ public function filtering()
{
$keyword = $this->request->keyword();

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

return;
}

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

return;
}

Expand All @@ -681,6 +687,17 @@ 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(explode(' ', $keyword));
}

/**
* Perform global search for the given 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 @@ -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
5 changes: 5 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