Skip to content

Commit

Permalink
Merge pull request #28 from irsyadulibad/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
irsyadulibad authored May 10, 2022
2 parents 786d2af + 9af21d9 commit ec03792
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ jobs:

- name: Testing with PHPUnit
run: vendor/bin/phpunit --verbose

- name: Testing with PHPStan
run: vendor/bin/phpstan -v
18 changes: 17 additions & 1 deletion src/DataTableAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ abstract class DataTableAbstract
* Column to be processed
*/
protected array $columnDef = [
'index' => false,
'index' => [
'indexed' => false,
'rowID' => 'DT_RowIndex'
],
'append' => [],
'edit' => [],
'hide' => [],
Expand Down Expand Up @@ -93,6 +96,19 @@ public function editColumn(string $name, callable $callback)
return $this;
}

/**
* Add indexed column to JSON response
*
* @param string $rowid | row index name
*/
public function addIndexColumn(string $rowid = null)
{
$this->columnDef['index']['indexed'] = true;

if(!is_null($rowid)) $this->columnDef['index']['rowID'] = $rowid;
return $this;
}

/**
* Render response data
*
Expand Down
11 changes: 11 additions & 0 deletions src/Processors/DataProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function result(): array

if(!empty($this->columnDef['hide'])) $this->hideFields();

if($this->columnDef['index']['indexed']) $this->addIndexField();

$this->escapeFields();

return $this->results;
Expand Down Expand Up @@ -77,4 +79,13 @@ private function escapeFields(): void
}
}
}

private function addIndexField(): void
{
$rowid = $this->columnDef['index']['rowID'];

for($i = 0; $i < count($this->results); $i++) {
$this->results[$i][$rowid] = $i + 1;
}
}
}
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
36 changes: 36 additions & 0 deletions tests/Integration/BuilderDataTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,27 @@ public function it_can_hide_column()
$this->assertObjectNotHasAttribute('username', json_decode($dt)->data[0]);
}

/** @test */
public function it_can_add_indexed_column()
{
$dt = datatables($this->builder)->addIndexColumn()->make();
$res = json_decode($dt);

$this->assertObjectHasAttribute('DT_RowIndex', $res->data[0]);
$this->assertEquals(2, $res->data[1]->DT_RowIndex);
$this->assertEquals(10, $res->data[9]->DT_RowIndex);
}

/** @test */
public function it_can_use_custom_index_column()
{
$dt = datatables('users')->addIndexColumn('RowIndex')->make();
$data = json_decode($dt)->data[0];

$this->assertObjectHasAttribute('RowIndex', $data);
$this->assertEquals(1, $data->RowIndex);
}

/** @test */
public function it_can_use_column_alias()
{
Expand All @@ -145,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
36 changes: 36 additions & 0 deletions tests/Integration/QueryDataTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,27 @@ public function it_can_hide_column()
$this->assertObjectNotHasAttribute('username', json_decode($dt)->data[0]);
}

/** @test */
public function it_can_add_indexed_column()
{
$dt = datatables('users')->addIndexColumn()->make();
$res = json_decode($dt);

$this->assertObjectHasAttribute('DT_RowIndex', $res->data[0]);
$this->assertEquals(2, $res->data[1]->DT_RowIndex);
$this->assertEquals(10, $res->data[9]->DT_RowIndex);
}

/** @test */
public function it_can_use_custom_index_column()
{
$dt = datatables('users')->addIndexColumn('RowIndex')->make();
$data = json_decode($dt)->data[0];

$this->assertObjectHasAttribute('RowIndex', $data);
$this->assertEquals(1, $data->RowIndex);
}

/** @test */
public function it_can_use_column_alias()
{
Expand All @@ -165,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 ec03792

Please sign in to comment.