Skip to content

Commit 159a4ee

Browse files
committed
Prevent editColumn when column is not shown
1 parent 1c42c78 commit 159a4ee

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/DataTableAbstract.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ public function addIndexColumn(): static
231231
*/
232232
public function editColumn($name, $content): static
233233
{
234-
$this->columnDef['edit'][] = ['name' => $name, 'content' => $content];
234+
if (!count($this->request->columns()) || in_array($name, Arr::pluck($this->request->columns(), 'name'))) {
235+
$this->columnDef['edit'][] = ['name' => $name, 'content' => $content];
236+
}
235237

236238
return $this;
237239
}

tests/Integration/QueryDataTableTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,19 @@ public function it_returns_only_the_selected_columns()
188188
$this->assertArrayHasKey('name', $json['data'][0]);
189189
}
190190

191+
/** @test */
192+
public function it_edit_only_the_selected_columns()
193+
{
194+
$json = $this->call('GET', '/query/edit-columns', [
195+
'columns' => [
196+
['data' => 'name', 'name' => 'name', 'searchable' => 'true', 'orderable' => 'true'],
197+
],
198+
])->json();
199+
200+
$this->assertEquals('edited', $json['data'][0]['name']);
201+
$this->assertNotEquals('edited', $json['data'][0]['email']);
202+
}
203+
191204
/** @test */
192205
public function it_does_not_allow_raw_html_on_added_columns()
193206
{
@@ -417,6 +430,17 @@ protected function setUp(): void
417430
->toJson();
418431
});
419432

433+
$router->get('/query/edit-columns', function (DataTables $dataTable) {
434+
return $dataTable->query(DB::table('users'))
435+
->editColumn('name', function () {
436+
return 'edited';
437+
})
438+
->editColumn('email', function () {
439+
return 'edited';
440+
})
441+
->toJson();
442+
});
443+
420444
$router->get('/query/xss-add', function (DataTables $dataTable) {
421445
return $dataTable->query(DB::table('users'))
422446
->addColumn('foo', '<a href="#">Allowed</a>')

0 commit comments

Comments
 (0)