Skip to content

Commit

Permalink
Serial column and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pfilsx committed Oct 23, 2019
1 parent 7e4378a commit 06696dd
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 81 deletions.
28 changes: 19 additions & 9 deletions src/Grid/DataGridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Pfilsx\DataGrid\Grid\Columns\ActionColumn;
use Pfilsx\DataGrid\Grid\Columns\DataColumn;
use InvalidArgumentException;
use Pfilsx\DataGrid\Grid\Columns\SerialColumn;
use Pfilsx\DataGrid\Grid\Providers\DataProviderInterface;

class DataGridBuilder implements DataGridBuilderInterface
Expand Down Expand Up @@ -55,18 +56,18 @@ public function __construct(DataGridServiceContainer $container)
/**
* @param string $attribute
* @param string $columnClass
* @param array $config
* @param array $options
* @return $this
*/
public function addColumn(string $attribute, string $columnClass = DataColumn::class, array $config = []): DataGridBuilderInterface
public function addColumn(string $attribute, string $columnClass = DataColumn::class, array $options = []): DataGridBuilderInterface
{
if (!is_subclass_of($columnClass, AbstractColumn::class)) {
throw new InvalidArgumentException('Expected subclass of' . AbstractColumn::class);
}
/**
* @var AbstractColumn $column
*/
$column = new $columnClass($this->container, array_merge($config, ['attribute' => $attribute]));
$column = new $columnClass($this->container, array_merge($options, ['attribute' => $attribute]));
$this->columns[] = $column;
if ($column->hasFilter() && $column->isVisible()) {
$this->hasFilters = true;
Expand All @@ -76,21 +77,30 @@ public function addColumn(string $attribute, string $columnClass = DataColumn::c

/**
* @param string $attribute
* @param array $config
* @param array $options
* @return $this
*/
public function addDataColumn(string $attribute, array $config = []): DataGridBuilderInterface
public function addDataColumn(string $attribute, array $options = []): DataGridBuilderInterface
{
return $this->addColumn($attribute, DataColumn::class, $config);
return $this->addColumn($attribute, DataColumn::class, $options);
}

/**
* @param array $config
* @param array $options
* @return $this
*/
public function addActionColumn(array $config = []): DataGridBuilderInterface
public function addActionColumn(array $options = []): DataGridBuilderInterface
{
return $this->addColumn('id', ActionColumn::class, $config);
return $this->addColumn('id', ActionColumn::class, $options);
}

/**
* @param array $options
* @return $this
*/
public function addSerialColumn(array $options = []): DataGridBuilderInterface
{
return $this->addColumn('', SerialColumn::class, $options);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/Grid/DataGridBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
interface DataGridBuilderInterface
{

public function addColumn(string $attribute, string $columnClass = DataColumn::class, array $config = []): self;
public function addColumn(string $attribute, string $columnClass = DataColumn::class, array $options = []): self;

public function addDataColumn(string $attribute, array $config = []): self;
public function addSerialColumn(array $options = []): self;

public function addActionColumn(array $config = []): self;
public function addDataColumn(string $attribute, array $options = []): self;

public function addActionColumn(array $options = []): self;

public function setTemplate(string $path): self;

Expand Down
12 changes: 5 additions & 7 deletions src/Resources/doc/columns/ActionColumn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Usage

.. code-block:: php
$builder->addColumn(self::ACTION_COLUMN, [
$builder->addActionColumn([
'pathPrefix' => 'path_prefix'
]);
Expand Down Expand Up @@ -49,18 +49,16 @@ Path prefix would be used in default url generation.

visible - boolean(default: true)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
whether to display a column.
Whether to display a column.

urlGenerator - callable(default: null)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Can be used to override default url generation function.

.. code-block:: php
$builder->addColumn(self::ACTION_COLUMN, [
'buttons' => [
'urlGenerator' => function($entity, string $action, RouterInterface $router){
$builder->addActionColumn([
'urlGenerator' => function($entity, string $action, RouterInterface $router){
return $router->generate('entity-'.$action, ['guid' => $entity->getId()]);
}
]
}
]);
11 changes: 3 additions & 8 deletions src/Resources/doc/columns/BooleanColumn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ Usage

.. code-block:: php
$builder->addColumn(self::BOOLEAN_COLUMN, [
'attribute' => 'entity_attribute',
$builder->addColumn('entity_attribute', self::BOOLEAN_COLUMN, [
'trueValue' => 'yes',
'falseValue' => 'no'
]);
Configuration
-------------

attribute - string(default: null)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Entity attribute.

attributes - array(default: [])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
html attributes to be applied to the tag <td>.
Expand All @@ -29,7 +24,7 @@ Filter configuration. See filters docs.

.. code-block:: php
$builder->addColumn(self::BOOLEAN_COLUMN, [
$builder->addColumn('entity_attribute', self::BOOLEAN_COLUMN, [
...
'filter' => [
'class' => self::FILTER_BOOLEAN
Expand Down Expand Up @@ -58,7 +53,7 @@ Can be used to override default output generation.

.. code-block:: php
$builder->addColumn(self::BOOLEAN_COLUMN, [
$builder->addColumn('entity_attribute', self::BOOLEAN_COLUMN, [
...
'value' => function($entity) {
return $entity->isEnabled ? 'enabled' : 'disabled';
Expand Down
12 changes: 3 additions & 9 deletions src/Resources/doc/columns/DataColumn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ Usage

.. code-block:: php
$builder->addColumn(self::DATA_COLUMN, [
'attribute' => 'entity_attribute'
]);
$builder->addDataColumn('entity_attribute');
Configuration
-------------

attribute - string(default: null)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Entity attribute.

attributes - array(default: [])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
html attributes to be applied to the tag <td>.
Expand All @@ -27,7 +21,7 @@ Filter configuration. See filters docs.

.. code-block:: php
$builder->addColumn(self::DATA_COLUMN, [
$builder->addDataColumn('entity_attribute', [
...
'filter' => [
'class' => self::FILTER_TEXT
Expand All @@ -48,7 +42,7 @@ Can be used to override default output generation.

.. code-block:: php
$builder->addColumn(self::DATA_COLUMN, [
$builder->addColumn('entity_attribute', self::DATA_COLUMN, [
'value' => function($entity) {
return $entity->isEnabled ? 'enabled' : 'disabled';
}
Expand Down
11 changes: 3 additions & 8 deletions src/Resources/doc/columns/DateColumn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ Usage

.. code-block:: php
$builder->addColumn(self::DATE_COLUMN, [
'attribute' => 'entity_attribute'
]);
$builder->addColumn('entity_attribute', self::DATE_COLUMN);
Configuration
-------------

attribute - string(default: null)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Entity attribute.

attributes - array(default: [])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -31,7 +26,7 @@ Filter configuration. See filters docs.

.. code-block:: php
$builder->addColumn(self::DATE_COLUMN, [
$builder->addColumn('entity_attribute', self::DATE_COLUMN, [
...
'filter' => [
'class' => self::FILTER_TEXT
Expand All @@ -52,7 +47,7 @@ Can be used to override default output generation.

.. code-block:: php
$builder->addColumn(self::DATE_COLUMN, [
$builder->addColumn('entity_attribute', self::DATE_COLUMN, [
'value' => function($entity) {
return $entity->getCreationDate()->format('d.m.Y');
}
Expand Down
14 changes: 4 additions & 10 deletions src/Resources/doc/columns/ImageColumn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ Usage

.. code-block:: php
$builder->addColumn(self::IMAGE_COLUMN, [
'attribute' => 'entity_attribute'
]);
$builder->addColumn('entity_attribute', self::IMAGE_COLUMN);
Configuration
-------------
Expand All @@ -19,17 +17,13 @@ Alt attribute callback.

.. code-block:: php
$builder->addColumn(self::IMAGE_COLUMN, [
'attribute' => 'entity_attribute',
$builder->addColumn('entity_attribute', self::IMAGE_COLUMN, [
...
'alt' => function($entity){
return $entity->getAlt();
}
]);
attribute - string(default: null)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Entity attribute.

attributes - array(default: [])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
html attributes to be applied to the tag <td>.
Expand All @@ -56,7 +50,7 @@ Can be used to override default output generation.

.. code-block:: php
$builder->addColumn(self::IMAGE_COLUMN, [
$builder->addColumn('entity_attribute', self::IMAGE_COLUMN, [
'value' => function($entity) {
return $entity->getImgTag();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/doc/columns/SerialColumn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Usage

.. code-block:: php
$builder->addColumn(self::SERIAL_COLUMN);
$builder->addSerialColumn();
Configuration
-------------
Expand Down
15 changes: 8 additions & 7 deletions src/Resources/doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ Configuration Reference
.. code-block:: yaml
data_grid:
# template used for rendering grid blocks
template: 'grid/grid.blocks.html.twig'
# message used in case of no data found
noDataMessage: 'Записей не найдено'
# pagination configuration
pagination:
instances:
# template used for rendering grid blocks
template: 'grid/grid.blocks.html.twig'
# message used in case of no data found
noDataMessage: 'Записей не найдено'
# pagination configuration
pagination_enabled: true
# num of data rows on each page
limit: 10
pagination_limit: 10
15 changes: 9 additions & 6 deletions src/Resources/doc/grid/DataGridBuilder.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ Usage
'class' => self::FILTER_TEXT
]
])
->addColumn(self::DATA_COLUMN, [
'attribute' => 'title',
->addColumn('title', self::DATA_COLUMN, [
'filter' => [
'class' => self::FILTER_TEXT
],
])
->addColumn(self::ACTION_COLUMN, [
->addActionColumn([
'pathPrefix' => 'entity'
]);
}
Expand All @@ -39,15 +38,19 @@ Usage
Methods Reference
-----------------

addColumn(string $columnFQN, array $columnOptions = [])
addColumn(string $attribute, string $columnFQN, array $columnOptions = [])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Adds column of specific type to grid. See columns reference for ``$columnOptions`` and listing of default columns.

addDataColumn(string $attribute, array $columnOptions = [])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Adds DataColumn to grid. Similar to ``addColumn(self::DATA_COLUMN, ['attribute' => ''])``
Adds DataColumn to grid. Similar to ``addColumn($attribute, self::DATA_COLUMN, $columnOptions)``

enablePagination(array|false $options = [])
addActionColumn(array $columnOptions = [])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Adds DataColumn to grid. Similar to ``addColumn('', self::ACTION_COLUMN, $columnOptions)``

enablePagination(boolean $isEnabled, int $limit = 10)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Configures grid pagination options or disable pagination. Only one option available right now - ``limit``.
``limit`` - max count of rows on each page.
Expand Down
5 changes: 2 additions & 3 deletions src/Resources/doc/grid/DataGridType.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ You must create your own DataGridType for specific entity. Your type must extend
'class' => self::FILTER_TEXT
]
])
->addColumn(self::DATA_COLUMN, [
'attribute' => 'title',
->addColumn('title', self::DATA_COLUMN, [
'filter' => [
'class' => self::FILTER_TEXT
],
])
->addColumn(self::ACTION_COLUMN, [
->addActionColumn([
'pathPrefix' => 'entity'
]);
}
Expand Down
Loading

0 comments on commit 06696dd

Please sign in to comment.