From 0b629a3a9c59c848dff55ca7b21cf42711541f73 Mon Sep 17 00:00:00 2001 From: Philippe MILINK Date: Wed, 22 Aug 2018 22:09:27 +0200 Subject: [PATCH 1/2] Create attributeColumn * a text column with attributes generated by the server for each row and displayed in the HTML in data-* attributes --- Datatable/Column/AttributeColumn.php | 204 ++++++++++++++++++ .../views/render/attributeColumn.html.twig | 20 ++ 2 files changed, 224 insertions(+) create mode 100644 Datatable/Column/AttributeColumn.php create mode 100644 Resources/views/render/attributeColumn.html.twig diff --git a/Datatable/Column/AttributeColumn.php b/Datatable/Column/AttributeColumn.php new file mode 100644 index 00000000..d86f6225 --- /dev/null +++ b/Datatable/Column/AttributeColumn.php @@ -0,0 +1,204 @@ + + * + * 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; + } +} diff --git a/Resources/views/render/attributeColumn.html.twig b/Resources/views/render/attributeColumn.html.twig new file mode 100644 index 00000000..dbf599ea --- /dev/null +++ b/Resources/views/render/attributeColumn.html.twig @@ -0,0 +1,20 @@ +{## + # This file is part of the SgDatatablesBundle package. + # + # (c) stwe + # + # 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 %} + + + {{ data|raw }} + From e0eba448517b289cb5b964886bc7bea3b3139078 Mon Sep 17 00:00:00 2001 From: Philippe MILINK Date: Tue, 23 Oct 2018 17:18:24 +0200 Subject: [PATCH 2/2] Add documentation for AttributeColumn --- Resources/doc/columns.md | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Resources/doc/columns.md b/Resources/doc/columns.md index 10578300..6635c3eb 100644 --- a/Resources/doc/columns.md +++ b/Resources/doc/columns.md @@ -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 @@ -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'] + ); + } + )) + + // ... + ; +} +``` \ No newline at end of file