Skip to content

Commit

Permalink
bind row data to column value-callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
z-song committed Dec 6, 2016
1 parent ef04213 commit 9a79ff7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ public function build()

$data = $this->processFilter();

Column::setOriginalGridData($data);

$this->columns->map(function (Column $column) use (&$data) {
$data = $column->fill($data);

Expand Down
54 changes: 46 additions & 8 deletions src/Grid/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ class Column
*/
protected $relationColumn;

/**
* Original grid data.
*
* @var array
*/
protected static $originalGridData = [];

/**
* @param string $name
* @param string $label
Expand Down Expand Up @@ -331,7 +338,7 @@ public function editable()
*/
public function value(Closure $callable)
{
$this->valueCallback = $callable->bindTo($this);
$this->valueCallback = $callable;

return $this;
}
Expand Down Expand Up @@ -410,27 +417,58 @@ protected function htmlWrap($value, $row = [])
*/
public function fill(array $data)
{
foreach ($data as &$item) {
$this->original = $value = array_get($item, $this->name);
foreach ($data as $key => &$row) {
$this->original = $value = array_get($row, $this->name);

$isCustomColumn = !array_has($row, $this->name);

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

array_set($item, $this->name, $value);
array_set($row, $this->name, $value);

if ($this->hasValueCallback()) {
$value = call_user_func($this->valueCallback, $this->original);
array_set($item, $this->name, $value);

$input = $isCustomColumn ? $row : $this->original;

$callback = $this->bindOriginalRow($this->valueCallback, $key);
$value = call_user_func($callback, $input);
array_set($row, $this->name, $value);
}

if ($this->hasHtmlCallback()) {
$value = $this->htmlWrap($value, $item);
array_set($item, $this->name, $value);
$value = $this->htmlWrap($value, $row);
array_set($row, $this->name, $value);
}
}

return $data;
}

/**
* Set original grid data to column.
*
* @param Closure $callback
* @param int $key
*
* @return Closure
*/
protected function bindOriginalRow(Closure $callback, $key)
{
$originalRow = static::$originalGridData[$key];

return $callback->bindTo((object)$originalRow);
}

/**
* Set original data for column.
*
* @param array $input
*/
public static function setOriginalGridData(array $input)
{
static::$originalGridData = $input;
}

/**
* Convert characters to HTML entities recursively.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/UserGridTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ public function testFilterRelation()
->seeInElement('td', $user->end_at);
}

public function testValueCallback()
{
$this->seedsTable(1);

$user = UserModel::with('profile')->find(1);

$this->visit('admin/users')
->seeInElement('th', 'Column1 not in table')
->seeInElement('th', 'Column2 not in table')
->seeInElement('td', "full name:{$user->profile->first_name} {$user->profile->last_name}")
->seeInElement('td', "{$user->email}#{$user->profile->color}");
}

public function testHasManyRelation()
{
factory(\Tests\Models\User::class, 10)
Expand Down
8 changes: 8 additions & 0 deletions tests/controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ protected function grid()
$grid->profile()->start_at('开始时间');
$grid->profile()->end_at('结束时间');

$grid->column('column1_not_in_table')->display(function ($row) {
return 'full name:'.$row['full_name'];
});

$grid->column('column2_not_in_table')->display(function ($row) {
return $this->email.'#'.$this->profile['color'];
});

$grid->tags()->value(function ($tags) {
$tags = collect($tags)->map(function ($tag) {
return "<code>{$tag['name']}</code>";
Expand Down

0 comments on commit 9a79ff7

Please sign in to comment.