Skip to content

Commit 9a79ff7

Browse files
committed
bind row data to column value-callback.
1 parent ef04213 commit 9a79ff7

File tree

4 files changed

+69
-8
lines changed

4 files changed

+69
-8
lines changed

src/Grid.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ public function build()
355355

356356
$data = $this->processFilter();
357357

358+
Column::setOriginalGridData($data);
359+
358360
$this->columns->map(function (Column $column) use (&$data) {
359361
$data = $column->fill($data);
360362

src/Grid/Column.php

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ class Column
8383
*/
8484
protected $relationColumn;
8585

86+
/**
87+
* Original grid data.
88+
*
89+
* @var array
90+
*/
91+
protected static $originalGridData = [];
92+
8693
/**
8794
* @param string $name
8895
* @param string $label
@@ -331,7 +338,7 @@ public function editable()
331338
*/
332339
public function value(Closure $callable)
333340
{
334-
$this->valueCallback = $callable->bindTo($this);
341+
$this->valueCallback = $callable;
335342

336343
return $this;
337344
}
@@ -410,27 +417,58 @@ protected function htmlWrap($value, $row = [])
410417
*/
411418
public function fill(array $data)
412419
{
413-
foreach ($data as &$item) {
414-
$this->original = $value = array_get($item, $this->name);
420+
foreach ($data as $key => &$row) {
421+
$this->original = $value = array_get($row, $this->name);
422+
423+
$isCustomColumn = !array_has($row, $this->name);
415424

416425
$value = $this->htmlEntityEncode($value);
417426

418-
array_set($item, $this->name, $value);
427+
array_set($row, $this->name, $value);
419428

420429
if ($this->hasValueCallback()) {
421-
$value = call_user_func($this->valueCallback, $this->original);
422-
array_set($item, $this->name, $value);
430+
431+
$input = $isCustomColumn ? $row : $this->original;
432+
433+
$callback = $this->bindOriginalRow($this->valueCallback, $key);
434+
$value = call_user_func($callback, $input);
435+
array_set($row, $this->name, $value);
423436
}
424437

425438
if ($this->hasHtmlCallback()) {
426-
$value = $this->htmlWrap($value, $item);
427-
array_set($item, $this->name, $value);
439+
$value = $this->htmlWrap($value, $row);
440+
array_set($row, $this->name, $value);
428441
}
429442
}
430443

431444
return $data;
432445
}
433446

447+
/**
448+
* Set original grid data to column.
449+
*
450+
* @param Closure $callback
451+
* @param int $key
452+
*
453+
* @return Closure
454+
*/
455+
protected function bindOriginalRow(Closure $callback, $key)
456+
{
457+
$originalRow = static::$originalGridData[$key];
458+
459+
return $callback->bindTo((object)$originalRow);
460+
}
461+
462+
/**
463+
* Set original data for column.
464+
*
465+
* @param array $input
466+
*/
467+
public static function setOriginalGridData(array $input)
468+
{
469+
static::$originalGridData = $input;
470+
}
471+
434472
/**
435473
* Convert characters to HTML entities recursively.
436474
*

tests/UserGridTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ public function testFilterRelation()
158158
->seeInElement('td', $user->end_at);
159159
}
160160

161+
public function testValueCallback()
162+
{
163+
$this->seedsTable(1);
164+
165+
$user = UserModel::with('profile')->find(1);
166+
167+
$this->visit('admin/users')
168+
->seeInElement('th', 'Column1 not in table')
169+
->seeInElement('th', 'Column2 not in table')
170+
->seeInElement('td', "full name:{$user->profile->first_name} {$user->profile->last_name}")
171+
->seeInElement('td', "{$user->email}#{$user->profile->color}");
172+
}
173+
161174
public function testHasManyRelation()
162175
{
163176
factory(\Tests\Models\User::class, 10)

tests/controllers/UserController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ protected function grid()
8585
$grid->profile()->start_at('开始时间');
8686
$grid->profile()->end_at('结束时间');
8787

88+
$grid->column('column1_not_in_table')->display(function ($row) {
89+
return 'full name:'.$row['full_name'];
90+
});
91+
92+
$grid->column('column2_not_in_table')->display(function ($row) {
93+
return $this->email.'#'.$this->profile['color'];
94+
});
95+
8896
$grid->tags()->value(function ($tags) {
8997
$tags = collect($tags)->map(function ($tag) {
9098
return "<code>{$tag['name']}</code>";

0 commit comments

Comments
 (0)