Skip to content

Commit

Permalink
Merge pull request #3018 from Arkhas/prevent_edit_columns_when_column…
Browse files Browse the repository at this point in the history
…s_not_shown

feat: Prevent editColumn when column is not shown
  • Loading branch information
yajra committed Jun 29, 2023
2 parents cb61786 + e9a2697 commit b1c5fa8
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 60 deletions.
51 changes: 33 additions & 18 deletions src/DataTableAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ abstract class DataTableAbstract implements DataTable
* @var array
*/
protected array $columnDef = [
'index' => false,
'append' => [],
'edit' => [],
'filter' => [],
'order' => [],
'only' => null,
'hidden' => [],
'index' => false,
'append' => [],
'edit' => [],
'filter' => [],
'order' => [],
'only' => null,
'hidden' => [],
'visible' => [],
];

Expand Down Expand Up @@ -103,10 +103,10 @@ abstract class DataTableAbstract implements DataTable
* @var array
*/
protected array $templates = [
'DT_RowId' => '',
'DT_RowId' => '',
'DT_RowClass' => '',
'DT_RowData' => [],
'DT_RowAttr' => [],
'DT_RowData' => [],
'DT_RowAttr' => [],
];

/**
Expand Down Expand Up @@ -147,6 +147,8 @@ abstract class DataTableAbstract implements DataTable

protected mixed $transformer;

protected bool $editOnlySelectedColumns = false;

/**
* Can the DataTable engine be created with these parameters.
*
Expand Down Expand Up @@ -231,7 +233,13 @@ public function addIndexColumn(): static
*/
public function editColumn($name, $content): static
{
$this->columnDef['edit'][] = ['name' => $name, 'content' => $content];
if ($this->editOnlySelectedColumns) {
if (! count($this->request->columns()) || in_array($name, Arr::pluck($this->request->columns(), 'name'))) {
$this->columnDef['edit'][] = ['name' => $name, 'content' => $content];
}
} else {
$this->columnDef['edit'][] = ['name' => $name, 'content' => $content];
}

return $this;
}
Expand Down Expand Up @@ -867,10 +875,10 @@ protected function processResults($results, $object = false): array
protected function render(array $data): JsonResponse
{
$output = $this->attachAppends([
'draw' => $this->request->draw(),
'recordsTotal' => $this->totalRecords,
'draw' => $this->request->draw(),
'recordsTotal' => $this->totalRecords,
'recordsFiltered' => $this->filteredRecords ?? 0,
'data' => $data,
'data' => $data,
]);

if ($this->config->isDebugging()) {
Expand Down Expand Up @@ -934,11 +942,11 @@ protected function errorResponse(\Exception $exception)
$this->getLogger()->error($exception);

return new JsonResponse([
'draw' => $this->request->draw(),
'recordsTotal' => $this->totalRecords,
'draw' => $this->request->draw(),
'recordsTotal' => $this->totalRecords,
'recordsFiltered' => 0,
'data' => [],
'error' => $error ? __($error) : "Exception Message:\n\n".$exception->getMessage(),
'data' => [],
'error' => $error ? __($error) : "Exception Message:\n\n".$exception->getMessage(),
]);
}

Expand Down Expand Up @@ -1039,4 +1047,11 @@ protected function getPrimaryKeyName(): string
{
return 'id';
}

public function editOnlySelectedColumns(): static
{
$this->editOnlySelectedColumns = true;

return $this;
}
}
113 changes: 71 additions & 42 deletions tests/Integration/QueryDataTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function it_can_set_total_records()
{
$crawler = $this->call('GET', '/set-total-records');
$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 10,
'draw' => 0,
'recordsTotal' => 10,
'recordsFiltered' => 10,
]);
}
Expand All @@ -34,8 +34,8 @@ public function it_can_set_zero_total_records()
{
$crawler = $this->call('GET', '/zero-total-records');
$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 0,
'draw' => 0,
'recordsTotal' => 0,
'recordsFiltered' => 0,
]);
}
Expand All @@ -45,8 +45,8 @@ public function it_can_set_total_filtered_records()
{
$crawler = $this->call('GET', '/set-filtered-records');
$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 10,
]);
}
Expand All @@ -56,8 +56,8 @@ public function it_returns_all_records_when_no_parameters_is_passed()
{
$crawler = $this->call('GET', '/query/users');
$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 20,
]);
}
Expand All @@ -70,12 +70,12 @@ public function it_can_perform_global_search()
['data' => 'name', 'name' => 'name', 'searchable' => 'true', 'orderable' => 'true'],
['data' => 'email', 'name' => 'email', 'searchable' => 'true', 'orderable' => 'true'],
],
'search' => ['value' => 'Record-19'],
'search' => ['value' => 'Record-19'],
]);

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 1,
]);
}
Expand All @@ -88,12 +88,12 @@ public function it_can_skip_total_records_count_query()
['data' => 'name', 'name' => 'name', 'searchable' => 'true', 'orderable' => 'true'],
['data' => 'email', 'name' => 'email', 'searchable' => 'true', 'orderable' => 'true'],
],
'search' => ['value' => 'Record-19'],
'search' => ['value' => 'Record-19'],
]);

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 0,
'draw' => 0,
'recordsTotal' => 0,
'recordsFiltered' => 1,
]);
}
Expand All @@ -106,12 +106,12 @@ public function it_can_perform_multiple_term_global_search()
['data' => 'name', 'name' => 'name', 'searchable' => 'true', 'orderable' => 'true'],
['data' => 'email', 'name' => 'email', 'searchable' => 'true', 'orderable' => 'true'],
],
'search' => ['value' => 'Record-19 Email-19'],
'search' => ['value' => 'Record-19 Email-19'],
]);

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 1,
]);
}
Expand Down Expand Up @@ -170,12 +170,12 @@ public function it_does_not_allow_search_on_added_columns()
['data' => 'name', 'name' => 'name', 'searchable' => 'true', 'orderable' => 'true'],
['data' => 'email', 'name' => 'email', 'searchable' => 'true', 'orderable' => 'true'],
],
'search' => ['value' => 'Record-19'],
'search' => ['value' => 'Record-19'],
]);

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 1,
]);
}
Expand All @@ -188,6 +188,20 @@ public function it_returns_only_the_selected_columns()
$this->assertArrayHasKey('name', $json['data'][0]);
}

/** @test */
public function it_edit_only_the_selected_columns_after_using_editOnlySelectedColumns()
{
$json = $this->call('GET', '/query/edit-columns', [
'columns' => [
['data' => 'name', 'name' => 'name', 'searchable' => 'true', 'orderable' => 'true'],
],
])->json();

$this->assertEquals('edited', $json['data'][0]['name']);
$this->assertEquals('edited', $json['data'][0]['id']);
$this->assertNotEquals('edited', $json['data'][0]['email']);
}

/** @test */
public function it_does_not_allow_raw_html_on_added_columns()
{
Expand Down Expand Up @@ -222,12 +236,12 @@ public function it_can_return_auto_index_column()
['data' => 'name', 'name' => 'name', 'searchable' => 'true', 'orderable' => 'true'],
['data' => 'email', 'name' => 'email', 'searchable' => 'true', 'orderable' => 'true'],
],
'search' => ['value' => 'Record-19'],
'search' => ['value' => 'Record-19'],
]);

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 1,
]);

Expand All @@ -243,12 +257,12 @@ public function it_allows_search_on_added_column_with_custom_filter_handler()
['data' => 'name', 'name' => 'name', 'searchable' => 'true', 'orderable' => 'true'],
['data' => 'email', 'name' => 'email', 'searchable' => 'true', 'orderable' => 'true'],
],
'search' => ['value' => 'Record-19'],
'search' => ['value' => 'Record-19'],
]);

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 1,
]);

Expand All @@ -262,10 +276,10 @@ public function it_returns_search_panes_options()
$crawler = $this->call('GET', '/query/search-panes');

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 20,
'searchPanes' => [
'searchPanes' => [
'options' => [
'id' => [],
],
Expand All @@ -287,8 +301,8 @@ public function it_performs_search_using_search_panes()
]);

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 2,
]);
}
Expand All @@ -299,21 +313,21 @@ public function it_allows_column_search_added_column_with_custom_filter_handler(
$crawler = $this->call('GET', '/query/blacklisted-filter', [
'columns' => [
[
'data' => 'foo',
'name' => 'foo',
'data' => 'foo',
'name' => 'foo',
'searchable' => 'true',
'orderable' => 'true',
'search' => ['value' => 'Record-1'],
'orderable' => 'true',
'search' => ['value' => 'Record-1'],
],
['data' => 'name', 'name' => 'name', 'searchable' => 'true', 'orderable' => 'true'],
['data' => 'email', 'name' => 'email', 'searchable' => 'true', 'orderable' => 'true'],
],
'search' => ['value' => ''],
'search' => ['value' => ''],
]);

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 1,
]);
}
Expand All @@ -324,8 +338,8 @@ public function it_can_return_formatted_columns()
$crawler = $this->call('GET', '/query/formatColumn');

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 20,
]);

Expand All @@ -345,8 +359,8 @@ public function it_can_return_added_column_with_dependency_injection()
$crawler = $this->call('GET', '/closure-di');

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 20,
]);

Expand Down Expand Up @@ -417,6 +431,21 @@ protected function setUp(): void
->toJson();
});

$router->get('/query/edit-columns', function (DataTables $dataTable) {
return $dataTable->query(DB::table('users'))
->editColumn('id', function () {
return 'edited';
})
->editOnlySelectedColumns()
->editColumn('name', function () {
return 'edited';
})
->editColumn('email', function () {
return 'edited';
})
->toJson();
});

$router->get('/query/xss-add', function (DataTables $dataTable) {
return $dataTable->query(DB::table('users'))
->addColumn('foo', '<a href="#">Allowed</a>')
Expand Down

0 comments on commit b1c5fa8

Please sign in to comment.