Skip to content

Commit f8ae829

Browse files
committed
- Add more table methods
1 parent ab1430a commit f8ae829

File tree

4 files changed

+118
-9
lines changed

4 files changed

+118
-9
lines changed

README.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,53 @@ You can override any of these in your table component:
208208
/**
209209
* Used to set a class on a table header based on the column attribute
210210
*/
211-
public function thClass($attribute) : ?string;
211+
public function setTableHeadClass($attribute) : ?string;
212+
213+
/**
214+
* Used to set a ID on a table header based on the column attribute
215+
*/
216+
public function setTableHeadId($attribute) : ?string;
217+
218+
/**
219+
* Used to set any attributes on a table header based on the column attribute
220+
* ['name' => 'my-custom-name', 'data-key' => 'my-custom-key']
221+
*/
222+
public function setTableHeadAttributes($attribute) : array;
212223
213224
/**
214225
* Used to set a class on a table row
215226
* You have the entre model of the row to work with
216227
*/
217-
public function trClass($model) : ?string;
228+
public function setTableRowClass($model) : ?string;
229+
230+
/**
231+
* Used to set a ID on a table row
232+
* You have the entre model of the row to work with
233+
*/
234+
public function setTableRowId($model) : ?string;
235+
236+
/**
237+
* Used to set any attribute on a table row
238+
* You have the entre model of the row to work with
239+
* ['name' => 'my-custom-name', 'data-key' => 'my-custom-key']
240+
*/
241+
public function setTableRowAttributes($model) : array;
218242
219243
/**
220244
* Used to set the class of a table cell based on the column and the value of the cell
221245
*/
222-
public function tdClass($attribute, $value) : ?string;
246+
public function setTableDataClass($attribute, $value) : ?string;
247+
248+
/**
249+
* Used to set the ID of a table cell based on the column and the value of the cell
250+
*/
251+
public function setTableDataId($attribute, $value) : ?string;
252+
253+
/**
254+
* Used to set any attributes of a table cell based on the column and the value of the cell
255+
* ['name' => 'my-custom-name', 'data-key' => 'my-custom-key']
256+
*/
257+
public function setTableDataAttributes($attribute, $value) : array;
223258
```
224259

225260
## Changelog

resources/views/includes/_body.blade.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
<tr><td colspan="{{ collect($columns)->count() }}">{{ $noResultsMessage }}</td></tr>
44
@else
55
@foreach($models as $model)
6-
<tr class="{{ $this->trClass($model) }}">
6+
<tr
7+
class="{{ $this->setTableRowClass($model) }}"
8+
id="{{ $this->setTableRowId($model) }}"
9+
@foreach ($this->setTableRowAttributes($model) as $key => $value) {{ $key . '="'.$value.'"' }} @endforeach
10+
>
711
@if($checkbox && $checkboxLocation === 'left')
812
@include('laravel-livewire-tables::includes._checkbox-row')
913
@endif
1014

1115
@foreach($columns as $column)
12-
<td class="{{ $this->tdClass($column->attribute, Arr::get($model->toArray(), $column->attribute)) }}">
16+
<td
17+
class="{{ $this->setTableDataClass($column->attribute, Arr::get($model->toArray(), $column->attribute)) }}"
18+
id="{{ $this->setTableDataId($column->attribute, Arr::get($model->toArray(), $column->attribute)) }}"
19+
@foreach ($this->setTableDataAttributes($column->attribute, Arr::get($model->toArray(), $column->attribute)) as $key => $value) {{ $key . '="'.$value.'"' }} @endforeach
20+
>
1321
@if ($column->isView())
1422
@include($column->view)
1523
@else

resources/views/includes/_columns.blade.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
@endif
44

55
@foreach($columns as $column)
6-
<th class="{{ $this->thClass($column->attribute) }}">
6+
<th
7+
class="{{ $this->setTableHeadClass($column->attribute) }}"
8+
id="{{ $this->setTableHeadId($column->attribute) }}"
9+
@foreach ($this->setTableHeadAttributes($column->attribute) as $key => $value) {{ $key . '="'.$value.'"' }} @endforeach
10+
>
711
@if($column->sortable)
812
<span style="cursor: pointer;" wire:click="sort('{{ $column->attribute }}')">
913
{{ $column->text }}

src/Http/Livewire/Traits/Table.php

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,37 @@ trait Table
6464
*
6565
* @return string|null
6666
*/
67-
public function thClass($attribute) : ?string
67+
public function setTableHeadClass($attribute) : ?string
68+
{
69+
return null;
70+
}
71+
72+
/**
73+
* @param $attribute
74+
*
75+
* @return string|null
76+
*/
77+
public function setTableHeadId($attribute) : ?string
78+
{
79+
return null;
80+
}
81+
82+
/**
83+
* @param $attribute
84+
*
85+
* @return array|null
86+
*/
87+
public function setTableHeadAttributes($attribute) : array
88+
{
89+
return [];
90+
}
91+
92+
/**
93+
* @param $model
94+
*
95+
* @return string|null
96+
*/
97+
public function setTableRowClass($model) : ?string
6898
{
6999
return null;
70100
}
@@ -74,19 +104,51 @@ public function thClass($attribute) : ?string
74104
*
75105
* @return string|null
76106
*/
77-
public function trClass($model) : ?string
107+
public function setTableRowId($model) : ?string
78108
{
79109
return null;
80110
}
81111

112+
/**
113+
* @param $model
114+
*
115+
* @return array
116+
*/
117+
public function setTableRowAttributes($model) : array
118+
{
119+
return [];
120+
}
121+
82122
/**
83123
* @param $attribute
84124
* @param $value
85125
*
86126
* @return string|null
87127
*/
88-
public function tdClass($attribute, $value) : ?string
128+
public function setTableDataClass($attribute, $value) : ?string
89129
{
90130
return null;
91131
}
132+
133+
/**
134+
* @param $attribute
135+
* @param $value
136+
*
137+
* @return string|null
138+
*/
139+
public function setTableDataId($attribute, $value) : ?string
140+
{
141+
return null;
142+
}
143+
144+
/**
145+
* @param $attribute
146+
* @param $value
147+
*
148+
* @return array
149+
*/
150+
public function setTableDataAttributes($attribute, $value) : array
151+
{
152+
return [];
153+
}
92154
}

0 commit comments

Comments
 (0)