Skip to content

Commit

Permalink
improve gridview table attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Nov 22, 2023
1 parent dae6583 commit ff74cc7
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 101 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
},
"config-plugin": {
"params": "params.php",
"di": "di.php"
"di": "di.php",
"widgets-themes": "widgets-themes.php"
}
},
"autoload": {
Expand Down
13 changes: 13 additions & 0 deletions config/widgets-themes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Yiisoft\Yii\DataView\GridView;

return [
'bootstrap5' => [
GridView::class => [
'tableClass' => ['table'],
],
],
];
33 changes: 28 additions & 5 deletions src/GridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class GridView extends BaseListView
private bool $headerTableEnabled = true;
private array $headerRowAttributes = [];
private array $rowAttributes = [];
private array $tableAttributes = ['class' => 'table'];
private array $tableAttributes = [];

public function __construct(
private CurrentRoute $currentRoute,
Expand Down Expand Up @@ -257,15 +257,38 @@ public function rowAttributes(array $values): self
}

/**
* Return new instance with the HTML attributes for the table.
* Return new instance with the HTML attributes for the table tag.
*
* @param array $values Attribute values indexed by attribute names.
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public function tableAttributes(array $attributes): self
{
$new = clone $this;
$new->tableAttributes = $attributes;
return $new;
}

/**
* Add one or more CSS classes to the table tag.
*
* @param string|null ...$class One or many CSS classes.
*/
public function tableAttributes(array $values): self
public function addTableClass(?string ...$class): self
{
$new = clone $this;
$new->tableAttributes = $values;
Html::addCssClass($new->tableAttributes, $class);
return $new;
}

/**
* Replace current table tag CSS classes with a new set of classes.
*
* @param string|null ...$class One or many CSS classes.
*/
public function tableClass(?string ...$class): static
{
$new = clone $this;
$new->tableAttributes['class'] = array_filter($class, static fn ($c) => $c !== null);
return $new;
}

Expand Down
34 changes: 17 additions & 17 deletions tests/Column/ActionColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testContent(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -84,7 +84,7 @@ public function testContentAttributes(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -133,7 +133,7 @@ public function testCustomButton(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -188,7 +188,7 @@ public function testDataLabel(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -233,7 +233,7 @@ public function testFooterAttributes(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -286,7 +286,7 @@ public function testLabel(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>test.label</th>
Expand Down Expand Up @@ -331,7 +331,7 @@ public function testLabelWithMbString(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Ενέργειες</th>
Expand Down Expand Up @@ -376,7 +376,7 @@ public function testLabelAttributes(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th class="test.class">test.label</th>
Expand Down Expand Up @@ -421,7 +421,7 @@ public function testName(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -466,7 +466,7 @@ public function testNotVisible(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr></tr>
</thead>
Expand Down Expand Up @@ -497,7 +497,7 @@ public function testPrimaryKey(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -550,7 +550,7 @@ public function testRender(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Id</th>
Expand Down Expand Up @@ -605,7 +605,7 @@ public function testUrlArguments(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -650,7 +650,7 @@ public function testUrlCreator(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -701,7 +701,7 @@ public function testUrlQueryParameters(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -746,7 +746,7 @@ public function testUrlParamsConfig(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down Expand Up @@ -791,7 +791,7 @@ public function testVisibleButtonsClosure(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Actions</th>
Expand Down
20 changes: 10 additions & 10 deletions tests/Column/CheckboxColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testContent(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Id</th>
Expand Down Expand Up @@ -84,7 +84,7 @@ public function testContentAttributes(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Id</th>
Expand Down Expand Up @@ -135,7 +135,7 @@ public function testDataLabel(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Id</th>
Expand Down Expand Up @@ -182,7 +182,7 @@ public function testLabel(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Id</th>
Expand Down Expand Up @@ -229,7 +229,7 @@ public function testLabelMbString(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Id</th>
Expand Down Expand Up @@ -276,7 +276,7 @@ public function testLabelAttributes(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Id</th>
Expand Down Expand Up @@ -323,7 +323,7 @@ public function testName(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Id</th>
Expand Down Expand Up @@ -370,7 +370,7 @@ public function testNotMultiple(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Id</th>
Expand Down Expand Up @@ -417,7 +417,7 @@ public function testNotVisible(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Id</th>
Expand Down Expand Up @@ -461,7 +461,7 @@ public function testRender(): void
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table class="table">
<table>
<thead>
<tr>
<th>Id</th>
Expand Down
Loading

0 comments on commit ff74cc7

Please sign in to comment.