Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Commit

Permalink
Add an AttributeColumn with data-* attributes (#837)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippemilink authored and stephanvierkant committed May 10, 2019
1 parent fc833c5 commit fb24430
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 0 deletions.
204 changes: 204 additions & 0 deletions Datatable/Column/AttributeColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php

/**
* This file is part of the SgDatatablesBundle package.
*
* (c) stwe <https://github.com/stwe/DatatablesBundle>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sg\DatatablesBundle\Datatable\Column;

use Sg\DatatablesBundle\Datatable\Helper;
use Sg\DatatablesBundle\Datatable\Filter\TextFilter;

use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Class AttributeColumn
*
* @package Sg\DatatablesBundle\Datatable\Column
*/
class AttributeColumn extends AbstractColumn
{
/**
* The AttributeColumn is filterable.
*/
use FilterableTrait;

/**
* The Attributes container.
* A required option.
*
* @var Closure
*/
protected $attributes;

//-------------------------------------------------
// ColumnInterface
//-------------------------------------------------

/**
* {@inheritdoc}
*/
public function renderSingleField(array &$row)
{
$renderAttributes = array();

$renderAttributes = call_user_func($this->attributes, $row);

$path = Helper::getDataPropertyPath($this->data);

$content = $this->twig->render(
$this->getCellContentTemplate(),
array(
'attributes' => $renderAttributes,
'data' => $this->accessor->getValue($row, $path)
)
);

$this->accessor->setValue($row, $path, $content);

}

/**
* {@inheritdoc}
*/
public function renderToMany(array &$row)
{
$value = null;
$path = Helper::getDataPropertyPath($this->data, $value);

if ($this->accessor->isReadable($row, $path)) {

if ($this->isEditableContentRequired($row)) {
// e.g. comments[ ].createdBy.username
// => $path = [comments]
// => $value = [createdBy][username]

$entries = $this->accessor->getValue($row, $path);

if (count($entries) > 0) {
foreach ($entries as $key => $entry) {
$currentPath = $path . '[' . $key . ']' . $value;
$currentObjectPath = Helper::getPropertyPathObjectNotation($path, $key, $value);

$content = $this->renderTemplate(
$this->accessor->getValue($row, $currentPath),
$row[$this->editable->getPk()],
$currentObjectPath
);

$this->accessor->setValue($row, $currentPath, $content);
}
} else {
// no placeholder - leave this blank
}
}

}

return $this;
}

/**
* {@inheritdoc}
*/
public function getCellContentTemplate()
{
return '@SgDatatables/render/attributeColumn.html.twig';
}

/**
* {@inheritdoc}
*/
public function renderPostCreateDatatableJsContent()
{
return null;
}

/**
* {@inheritdoc}
*/
public function getColumnType()
{
return parent::ACTION_COLUMN;
}

//-------------------------------------------------
// Options
//-------------------------------------------------

/**
* Config options.
*
* @param OptionsResolver $resolver
*
* @return $this
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);

$resolver->setDefaults(array(
'filter' => array(TextFilter::class, array()),
'attributes' => null,
));

$resolver->setAllowedTypes('filter', 'array');
$resolver->setAllowedTypes('attributes', array('null', 'array', 'Closure'));

return $this;
}

//-------------------------------------------------
// Helper
//-------------------------------------------------

/**
* Render template.
*
* @param string|null $data
* @param string $pk
* @param string|null $path
*
* @return mixed|string
*/
private function renderTemplate($data)
{
return $this->twig->render(
$this->getCellContentTemplate(),
array(
'data' => $data,
)
);
}


/**
* Get attributes.
*
* @return Attributes[]
*/
public function getAttributes()
{
return $this->attributes;
}

/**
* Set attributes.
*
* @param Closure $attributes
*
* @return $this
* @throws Exception
*/
public function setAttributes($attributes)
{
$this->attributes = $attributes;

return $this;
}
}
42 changes: 42 additions & 0 deletions Resources/doc/columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
6. [Action Column](#6-action-column)
7. [Multiselect Column](#7-multiselect-column)
8. [Number Column](#8-number-column)
9. [Attribute Column](#9-number-column)

## 1. Column

Expand Down Expand Up @@ -713,3 +714,44 @@ public function buildDatatable(array $options = array())
}
```
___
## 9. Attribute column

Represents a column, with a `span` tag with `data-*` attributes. The displayed data is in the `span` tag.


### Options template

@SgDatatables/column/attributeColumn.html.twig

### Options

All options of [Column](#1-column).

**Additional:**

| Option | Type | Default | Required | Description |
|---------------------|---------------------------|---------------------------------------|----------|-----------------|
| attributes | null or string or Closure | | X | Attributes to display in `data-*` attributes |


### Example

``` php
public function buildDatatable(array $options = array())
{
// ...

$this->columnBuilder
->add('description', AttributeColumn::class, array(
'title' => 'Message',
'attributes' => function($row) {
return array(
'severity' => $row['severity']
);
}
))

// ...
;
}
```
20 changes: 20 additions & 0 deletions Resources/views/render/attributeColumn.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{##
# This file is part of the SgDatatablesBundle package.
#
# (c) stwe <https://github.com/stwe/DatatablesBundle>
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
#}

{% import _self as macros %}

{% macro attributes(attributes) %}
{% for key, value in attributes %}
data-{{ key }}="{{ value }}"
{% endfor %}
{% endmacro %}

<span {{ macros.attributes(attributes) }}>
{{ data|raw }}
</span>

0 comments on commit fb24430

Please sign in to comment.