Skip to content

Commit 4fe59f0

Browse files
committed
feat: allow closure on formatColumn
1 parent 5fd8996 commit 4fe59f0

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/DataTableAbstract.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ public static function create($source)
176176
* @param string|array $columns
177177
* @param string|\Yajra\DataTables\Contracts\Formatter $formatter
178178
* @return $this
179-
*
180-
* @throws \Yajra\DataTables\Exceptions\Exception
181179
*/
182180
public function formatColumn($columns, $formatter): static
183181
{
@@ -193,7 +191,29 @@ public function formatColumn($columns, $formatter): static
193191
return $this;
194192
}
195193

196-
throw new Exception('$formatter must be an instance of '.Formatter::class);
194+
if (is_callable($formatter)) {
195+
foreach ((array) $columns as $column) {
196+
$this->addColumn(
197+
$column.'_formatted',
198+
function ($row) use ($column, $formatter) {
199+
return $formatter(data_get($row, $column), $row);
200+
}
201+
);
202+
}
203+
204+
return $this;
205+
}
206+
207+
foreach ((array) $columns as $column) {
208+
$this->addColumn(
209+
$column.'_formatted',
210+
function ($row) use ($column) {
211+
return data_get($row, $column);
212+
}
213+
);
214+
}
215+
216+
return $this;
197217
}
198218

199219
/**

0 commit comments

Comments
 (0)