Skip to content

Commit

Permalink
fix: aliases column not searchable
Browse files Browse the repository at this point in the history
  • Loading branch information
irsyadulibad committed May 10, 2022
1 parent 996544e commit 9af21d9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/QueryDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,30 @@ private function filterRecords(): void
{
$fields = Request::fields();
$keyword = Request::keyword();
$firstLike = false;

if(!empty($fields)) {
$this->builder->groupStart();
$firstLike = false;

foreach($fields as $field) {
$fieldName = strlen($field->name) > 0 ? $field->name : $field->data;

if(!$field->searchable) continue;
if(!in_array($fieldName, $this->fields)) continue;


if(array_key_exists($fieldName, $this->aliases)) {
$field = $this->aliases[$field];
$fieldName = $this->aliases[$fieldName];
}


if(!in_array($fieldName, $this->fields)) continue;

if(!$firstLike) {
$this->builder->like($fieldName, $keyword->value);
$firstLike = true;
} else {
$this->builder->orLike($fieldName, $keyword->value);
}
}

$this->builder->groupEnd();
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Integration/BuilderDataTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ public function it_can_order_column_alias()
$this->assertEquals('name999', json_decode($dt)->data[0]->user_name);
}

/** @test */
public function it_can_search_column_alias()
{
$_GET['search'] = [
'value' => 'email-1000',
'regex' => 'false'
];

$_GET['columns'][2]['name'] = 'user_email';
$_GET['columns'][2]['data'] = 'user_email';

$dt = datatables('users')->select('email as user_email')->make();
$this->assertEquals(1, json_decode($dt)->recordsFiltered);
}

/** @test */
public function it_can_join_another_table()
{
Expand Down
15 changes: 15 additions & 0 deletions tests/Integration/QueryDataTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,21 @@ public function it_can_order_column_alias()
$this->assertEquals('name999', json_decode($dt)->data[0]->user_name);
}

/** @test */
public function it_can_search_column_alias()
{
$_GET['search'] = [
'value' => 'email-1000',
'regex' => 'false'
];

$_GET['columns'][2]['name'] = 'user_email';
$_GET['columns'][2]['data'] = 'user_email';

$dt = datatables('users')->select('email as user_email')->make();
$this->assertEquals(1, json_decode($dt)->recordsFiltered);
}

/** @test */
public function it_can_join_another_table()
{
Expand Down

0 comments on commit 9af21d9

Please sign in to comment.