Skip to content

Commit 6d4828e

Browse files
authored
Merge pull request #191 from yajra/thead-class
feat: thead class builder
2 parents d4f9aa3 + 9416824 commit 6d4828e

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

src/Html/Builder.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,18 @@ public function table(array $attributes = [], bool $drawFooter = false, bool $dr
198198
$htmlAttr = $this->html->attributes($this->tableAttributes);
199199

200200
$tableHtml = '<table'.$htmlAttr.'>';
201-
$searchHtml = $drawSearch ? '<tr class="search-filter">'.implode('',
202-
$this->compileTableSearchHeaders()).'</tr>' : '';
203-
$tableHtml .= '<thead><tr>'.implode('', $th).'</tr>'.$searchHtml.'</thead>';
201+
$searchHtml = $drawSearch
202+
? '<tr class="search-filter">'.implode('', $this->compileTableSearchHeaders()).'</tr>'
203+
: '';
204+
205+
$tableHtml .= '<thead'.($this->theadClass ?? '').'>';
206+
$tableHtml .= '<tr>'.implode('', $th).'</tr>'.$searchHtml.'</thead>';
207+
204208
if ($drawFooter) {
205209
$tf = $this->compileTableFooter();
206210
$tableHtml .= '<tfoot><tr>'.implode('', $tf).'</tr></tfoot>';
207211
}
212+
208213
$tableHtml .= '</table>';
209214

210215
return new HtmlString($tableHtml);

src/Html/HasTable.php

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,7 @@
66

77
trait HasTable
88
{
9-
/**
10-
* Retrieves HTML table attribute value.
11-
*
12-
* @param string $attribute
13-
* @return string
14-
*/
15-
public function getTableAttribute(string $attribute): string
16-
{
17-
return $this->tableAttributes[$attribute] ?? '';
18-
}
9+
protected ?string $theadClass = null;
1910

2011
/**
2112
* Get table computed table attributes.
@@ -38,16 +29,6 @@ public function setTableId(string $id): static
3829
return $this->setTableAttribute('id', $id);
3930
}
4031

41-
/**
42-
* Get HTML table "id" attribute.
43-
*
44-
* @return string
45-
*/
46-
public function getTableId(): string
47-
{
48-
return $this->getTableAttribute('id');
49-
}
50-
5132
/**
5233
* Sets HTML table attribute(s).
5334
*
@@ -81,6 +62,27 @@ public function setTableAttributes(array $attributes): static
8162
return $this;
8263
}
8364

65+
/**
66+
* Get HTML table "id" attribute.
67+
*
68+
* @return string
69+
*/
70+
public function getTableId(): string
71+
{
72+
return $this->getTableAttribute('id');
73+
}
74+
75+
/**
76+
* Retrieves HTML table attribute value.
77+
*
78+
* @param string $attribute
79+
* @return string
80+
*/
81+
public function getTableAttribute(string $attribute): string
82+
{
83+
return $this->tableAttributes[$attribute] ?? '';
84+
}
85+
8486
/**
8587
* Add class names to the "class" attribute of HTML table.
8688
*
@@ -98,6 +100,19 @@ public function addTableClass(array|string $class): static
98100
return $this->setTableAttribute('class', $class);
99101
}
100102

103+
/**
104+
* Set table > thead class names.
105+
*
106+
* @param string $class
107+
* @return $this
108+
*/
109+
public function setTableHeadClass(string $class): static
110+
{
111+
$this->theadClass = " class=\"$class\"";
112+
113+
return $this;
114+
}
115+
101116
/**
102117
* Remove class names from the "class" attribute of HTML table.
103118
*

tests/BuilderTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ class BuilderTest extends TestCase
1515
public function it_can_get_script_default_type_attribute()
1616
{
1717
$html = $this->getHtmlBuilder()->scripts()->toHtml();
18-
18+
1919
$this->assertStringContainsString('type="text/javascript"', $html);
2020
}
2121

2222
/** @test */
2323
public function it_can_set_script_type_attribute()
2424
{
2525
$html = $this->getHtmlBuilder()->scripts(attributes: ['type' => 'module'])->toHtml();
26-
26+
2727
$this->assertStringContainsString('type="module"', $html);
2828
}
2929

@@ -267,6 +267,9 @@ public function it_has_table_options()
267267

268268
$this->assertInstanceOf(HtmlString::class, $builder->table());
269269
$this->assertEquals('<table class="table" id="my-table"><thead><tr></tr></thead></table>', $builder->table()->toHtml());
270+
271+
$builder->setTableHeadClass('thead-dark');
272+
$this->assertEquals('<table class="table" id="my-table"><thead class="thead-dark"><tr></tr></thead></table>', $builder->table()->toHtml());
270273
}
271274

272275
/** @test */

0 commit comments

Comments
 (0)