Skip to content

Commit b39afed

Browse files
committed
setThSortButtonAttributes
1 parent a93fa1d commit b39afed

File tree

8 files changed

+274
-1
lines changed

8 files changed

+274
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
99
- Added table event listeners to sort/filter/clear from other components.
1010
- Added text filter.
1111
- Added $row as second parameter to BooleanColumn `setCallback()`.
12+
- Added `setThSortButtonAttributes()` to set attributes for th sort button.
1213

1314
## [2.3.0] - 2022-04-28
1415

docs/datatable/available-methods.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,26 @@ public function configure(): void
158158
}
159159
```
160160

161+
### setThSortButtonAttributes
162+
163+
Set a list of attributes to override on the th sort button elements
164+
165+
```php
166+
public function configure(): void
167+
{
168+
// Takes a callback that gives you the current column.
169+
$this->setThSortButtonAttributes(function(Column $column) {
170+
if ($column->isField('name')) {
171+
return [
172+
'class' => 'bg-green-500',
173+
];
174+
}
175+
176+
return [];
177+
});
178+
}
179+
```
180+
161181
By default, this replaces the default classes on the th, if you would like to keep them, set the default flag to true.
162182

163183
```php

output.txt

Lines changed: 208 additions & 0 deletions
Large diffs are not rendered by default.

resources/views/components/table/th.blade.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
$attributes = $attributes->merge(['wire:key' => 'header-col-'.$index.'-'.$component->id]);
66
$theme = $component->getTheme();
77
$customAttributes = $component->getThAttributes($column);
8+
$customSortButtonAttributes = $component->getThSortButtonAttributes($column);
89
$direction = $column->hasField() ? $component->getSort($column->getColumnSelectName()) : null;
910
@endphp
1011

@@ -21,7 +22,10 @@
2122
@else
2223
<button
2324
wire:click="sortBy('{{ $column->getColumnSelectName() }}')"
24-
class="flex items-center space-x-1 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider group focus:outline-none dark:text-gray-400"
25+
{{
26+
$attributes->merge($customSortButtonAttributes)
27+
->class(['flex items-center space-x-1 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider group focus:outline-none dark:text-gray-400' => $customSortButtonAttributes['default'] ?? true])
28+
}}
2529
>
2630
<span>{{ $column->getTitle() }}</span>
2731

src/Traits/ComponentUtilities.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ trait ComponentUtilities
2525
protected array $theadAttributes = [];
2626
protected array $tbodyAttributes = [];
2727
protected $thAttributesCallback;
28+
protected $thSortButtonAttributesCallback;
2829
protected $trAttributesCallback;
2930
protected $trUrlCallback;
3031
protected $trUrlTargetCallback;

src/Traits/Configuration/ComponentConfiguration.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ public function setThAttributes(callable $callback): self
9898
return $this;
9999
}
100100

101+
/**
102+
* Get a list of attributes to override on the th sort button elements
103+
*
104+
* @param callable $callback
105+
*
106+
* @return $this
107+
*/
108+
public function setThSortButtonAttributes(callable $callback): self
109+
{
110+
$this->thSortButtonAttributesCallback = $callback;
111+
112+
return $this;
113+
}
114+
101115
/**
102116
* Get a list of attributes to override on the td elements
103117
*

src/Traits/Helpers/ComponentHelpers.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ public function getThAttributes(Column $column): array
109109
return $this->thAttributesCallback ? call_user_func($this->thAttributesCallback, $column) : ['default' => true];
110110
}
111111

112+
/**
113+
* @param Column $column
114+
*
115+
* @return bool[]
116+
*/
117+
public function getThSortButtonAttributes(Column $column): array
118+
{
119+
return $this->thSortButtonAttributesCallback ? call_user_func($this->thSortButtonAttributesCallback, $column) : ['default' => true];
120+
}
121+
112122
/**
113123
* @param Model $row
114124
* @param int $index

tests/Traits/Configuration/ComponentConfigurationTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ public function can_set_th_attributes(): void
7474
$this->assertSame($this->basicTable->getThAttributes($this->basicTable->columns()[1]), ['default' => true, 'here' => 'there']);
7575
}
7676

77+
/** @test */
78+
public function can_set_th_sort_button_attributes(): void
79+
{
80+
$this->basicTable->setThSortButtonAttributes(function (Column $column) {
81+
if ($column->isField('id')) {
82+
return ['default' => false, 'this' => 'that'];
83+
}
84+
85+
return ['default' => true, 'here' => 'there'];
86+
});
87+
88+
$this->assertSame($this->basicTable->getThSortButtonAttributes($this->basicTable->columns()[0]), ['default' => false, 'this' => 'that']);
89+
$this->assertSame($this->basicTable->getThSortButtonAttributes($this->basicTable->columns()[1]), ['default' => true, 'here' => 'there']);
90+
}
91+
7792
/** @test */
7893
public function can_set_tr_attributes(): void
7994
{

0 commit comments

Comments
 (0)