diff --git a/README.md b/README.md index 4ee5c3b..0dc72ee 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,12 @@ Inertia::render('Page/Index')->table(function ($table) { }); ``` +The `addColumn` method has an optional third parameter to disable the column by default: + +```php +$table->addColumn('name', 'Name', false); +``` + #### Disable global search By default, global search is enabled. This query will be applied to the filters by the `global` attribute. If you don't want to use the global search, you can use the `disableGlobalSearch` method. diff --git a/php/InertiaTable.php b/php/InertiaTable.php index 9ca55fe..00ab094 100644 --- a/php/InertiaTable.php +++ b/php/InertiaTable.php @@ -161,14 +161,15 @@ public function applyTo(Response $response): Response * * @param string $key * @param string $label + * @param bool $enabled * @return self */ - public function addColumn(string $key, string $label): self + public function addColumn(string $key, string $label, bool $enabled = true): self { $this->columns->put($key, [ 'key' => $key, 'label' => $label, - 'enabled' => true, + 'enabled' => $enabled, ]); return $this; @@ -177,7 +178,7 @@ public function addColumn(string $key, string $label): self public function addColumns(array $columns = []): self { foreach ($columns as $key => $value) { - $this->addColumn($key, $value); + $this->addColumn($key, $value, true); } return $this; diff --git a/tests/InertiaTableTest.php b/tests/InertiaTableTest.php index 16615ba..7f97a07 100644 --- a/tests/InertiaTableTest.php +++ b/tests/InertiaTableTest.php @@ -47,6 +47,25 @@ public function it_can_add_a_column_to_toggle() ], $props); } + /** @test */ + public function it_can_add_a_column_that_is_disabled_by_default() + { + $table = new InertiaTable($this->request()); + $table->addColumn('name', 'Name', false); + + $props = $table->getQueryBuilderProps(); + + Assert::assertArraySubset([ + "columns" => [ + "name" => [ + "key" => "name", + "label" => "Name", + "enabled" => false, + ], + ], + ], $props); + } + /** @test */ public function it_gets_the_default_toggled_columns_from_the_query_String() {