Skip to content

Commit d728776

Browse files
committed
Optimize grid total row
1 parent 8bf0fe3 commit d728776

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

resources/views/grid/total-row.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<tfoot>
22
<tr>
33
@foreach($columns as $column)
4-
<td>{!! $column !!}</td>
4+
<td class="{{ $column['class'] }}">{!! $column['value'] !!}</td>
55
@endforeach
66
</tr>
77
</tfoot>

src/Grid/Concerns/HasTotalRow.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function addTotalRow($column, $callback)
2828
/**
2929
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
3030
*/
31-
public function renderTotalRow()
31+
public function renderTotalRow($columns = null)
3232
{
3333
if (empty($this->totalRowColumns)) {
3434
return '';
@@ -40,6 +40,10 @@ public function renderTotalRow()
4040

4141
$totalRow->setGrid($this);
4242

43+
if ($columns) {
44+
$totalRow->setVisibleColumns($columns);
45+
}
46+
4347
return $totalRow->render();
4448
}
4549
}

src/Grid/Tools/TotalRow.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Encore\Admin\Grid\Column;
66
use Illuminate\Database\Query\Builder;
77
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Collection;
89

910
class TotalRow extends AbstractTool
1011
{
@@ -18,6 +19,11 @@ class TotalRow extends AbstractTool
1819
*/
1920
protected $columns;
2021

22+
/**
23+
* @var Collection
24+
*/
25+
protected $visibleColumns;
26+
2127
/**
2228
* TotalRow constructor.
2329
*
@@ -54,14 +60,34 @@ protected function total($column, $display = null)
5460
return $sum;
5561
}
5662

63+
/**
64+
* @param Collection $columns
65+
*/
66+
public function setVisibleColumns($columns)
67+
{
68+
$this->visibleColumns = $columns;
69+
}
70+
71+
/**
72+
* @return Collection|static
73+
*/
74+
public function getVisibleColumns()
75+
{
76+
if ($this->visibleColumns) {
77+
return $this->visibleColumns;
78+
}
79+
80+
return $this->getGrid()->visibleColumns();
81+
}
82+
5783
/**
5884
* Render total-row.
5985
*
6086
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
6187
*/
6288
public function render()
6389
{
64-
$columns = $this->getGrid()->visibleColumns()->flatMap(function (Column $column) {
90+
$columns = $this->getVisibleColumns()->map(function (Column $column) {
6591
$name = $column->getName();
6692

6793
$total = '';
@@ -70,8 +96,11 @@ public function render()
7096
$total = $this->total($name, Arr::get($this->columns, $name));
7197
}
7298

73-
return [$name => $total];
74-
})->toArray();
99+
return [
100+
'class' => $column->getClassName(),
101+
'value' => $total
102+
];
103+
});
75104

76105
return view('admin::grid.total-row', compact('columns'));
77106
}

0 commit comments

Comments
 (0)