Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion Datatable/Action/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setAllowedTypes('route', array('null', 'string'));
$resolver->setAllowedTypes('route_parameters', array('null', 'array', 'Closure'));
$resolver->setAllowedTypes('icon', array('null', 'string'));
$resolver->setAllowedTypes('label', array('null', 'string'));
$resolver->setAllowedTypes('label', array('null', 'string', 'Closure'));
$resolver->setAllowedTypes('confirm', 'bool');
$resolver->setAllowedTypes('confirm_message', array('null', 'string'));
$resolver->setAllowedTypes('attributes', array('null', 'array', 'Closure'));
Expand Down Expand Up @@ -290,6 +290,22 @@ public function setLabel($label)
return $this;
}

/**
* Call label closure.
*
* @param array $row
*
* @return string
*/
public function callLabelClosure(array $row = array())
{
if ($this->label instanceof Closure) {
return call_user_func($this->label, $row);
}

return $this->label;
}

/**
* Get confirm.
*
Expand Down
4 changes: 4 additions & 0 deletions Datatable/Column/ActionColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function renderSingleField(array &$row)
$parameters = array();
$attributes = array();
$values = array();
$labels = array();

/** @var Action $action */
foreach ($this->actions as $actionKey => $action) {
Expand Down Expand Up @@ -136,12 +137,15 @@ public function renderSingleField(array &$row)
$values[$actionKey] = null;
}
}

$labels[$actionKey] = $action->callLabelClosure($row);
}

$row[$this->getIndex()] = $this->twig->render(
$this->getCellContentTemplate(),
array(
'actions' => $this->actions,
'labels' => $labels,
'route_parameters' => $parameters,
'attributes' => $attributes,
'values' => $values,
Expand Down
40 changes: 25 additions & 15 deletions Resources/doc/columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,21 +434,21 @@ A Column to display CRUD action labels or buttons.

### Action options

| Option | Type | Default | Required | Description |
|---------------------|------------------------|---------|----------|-------------|
| route | null or string | null | | The name of the Action route. |
| route_parameters | null, array or Closure | null | | The route parameters. |
| icon | null or string | null | | An icon for the Action. |
| label | null or string | null | | A label for the Action. |
| confirm | bool | false | | Show confirm message if true. |
| confirm_message | null or string | null | | The confirm message. |
| attributes | null, array or Closure | null | | HTML Tag attributes (except 'href' and 'value'). |
| button | bool | false | | Render a button instead of a link. |
| button_value | null or string | null | | The button value. |
| button_value_prefix | bool | false | | Use the Datatable-Name as prefix for the button value. |
| render_if | null or Closure | null | | Render an Action only if conditions are TRUE. |
| start_html | null or string | null | | HTML code before the <a> Tag. |
| end_html | null or string | null | | HTML code after the <a> Tag. |
| Option | Type | Default | Required | Description |
|---------------------|-------------------------|---------|----------|-------------|
| route | null or string | null | | The name of the Action route. |
| route_parameters | null, array or Closure | null | | The route parameters. |
| icon | null or string | null | | An icon for the Action. |
| label | null, string or Closure | null | | A label for the Action. |
| confirm | bool | false | | Show confirm message if true. |
| confirm_message | null or string | null | | The confirm message. |
| attributes | null, array or Closure | null | | HTML Tag attributes (except 'href' and 'value'). |
| button | bool | false | | Render a button instead of a link. |
| button_value | null or string | null | | The button value. |
| button_value_prefix | bool | false | | Use the Datatable-Name as prefix for the button value. |
| render_if | null or Closure | null | | Render an Action only if conditions are TRUE. |
| start_html | null or string | null | | HTML code before the <a> Tag. |
| end_html | null or string | null | | HTML code after the <a> Tag. |

### Example

Expand Down Expand Up @@ -535,6 +535,16 @@ $this->columnBuilder
'start_html' => '<div class="start_show_action">',
'end_html' => '</div>',
),
array(
'route' => 'comments_show',
'route_parameters' => array(
'id' => 'id',
),
'label' => function($row) {
return sprintf('Show %s comments', '$row['comment_count']); // 'comment_count' may be a virtual column
},
'confirm' => false,
),
),
))
;
Expand Down
8 changes: 4 additions & 4 deletions Resources/views/render/action.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
#}
{% import _self as macros %}

{% macro link_title(action) %}
{% macro link_title(action, label) %}
{% if action.label is same as(null) and action.icon is same as(null) %}
{% if action.route is not same as(null) %}
{{ action.route }}
{% else %}
null
{% endif %}
{% else %}
<span class="{{ action.icon }}"></span> {{ action.label }}
<span class="{{ action.icon }}"></span> {{ label }}
{% endif %}
{% endmacro %}

Expand Down Expand Up @@ -65,13 +65,13 @@
{% if action.button is same as(false) %}
{{ action.startHtml|raw }}
<a {{ macros.href(action, route_parameters[actionKey]) }} {{ macros.attributes(attributes[actionKey]) }} {{ macros.confirm_dialog(action) }}>
{{ macros.link_title(action) }}
{{ macros.link_title(action, labels[actionKey]) }}
</a>
{{ action.endHtml|raw }}
{% else %}
{{ action.startHtml|raw }}
<button type="button" {{ macros.value(values[actionKey]) }} {{ macros.attributes(attributes[actionKey]) }} {{ macros.confirm_dialog(action) }}>
{{ macros.link_title(action) }}
{{ macros.link_title(action, labels[actionKey]) }}
</button>
{{ action.endHtml|raw }}
{% endif %}
Expand Down