From 3450d30be70a762e76500f58866bb6b5e0f05464 Mon Sep 17 00:00:00 2001 From: Philippe MILINK Date: Mon, 20 Aug 2018 23:52:29 +0200 Subject: [PATCH 1/2] Add sent_in_response column parameter * to get access to a field without sending it to the browser --- Datatable/Column/AbstractColumn.php | 35 +++++++++++++++++++ Resources/doc/columns.md | 1 + Resources/views/column/column.html.twig | 5 --- Resources/views/datatable/columns.html.twig | 4 ++- .../views/datatable/datatable_html.html.twig | 35 +++++++++++++------ .../views/datatable/datatable_js.html.twig | 2 +- Response/DatatableFormatter.php | 6 ++++ 7 files changed, 70 insertions(+), 18 deletions(-) diff --git a/Datatable/Column/AbstractColumn.php b/Datatable/Column/AbstractColumn.php index 7da35c1c..b8916b99 100644 --- a/Datatable/Column/AbstractColumn.php +++ b/Datatable/Column/AbstractColumn.php @@ -290,6 +290,14 @@ abstract class AbstractColumn implements ColumnInterface */ protected $originalTypeOfField; + /** + * If the field is sent in the response, to show in the webpage + * Is set in the ColumnBuilder. + * Default: true + * + * @var bool + */ + protected $sentInResponse; //------------------------------------------------- // Options //------------------------------------------------- @@ -323,6 +331,7 @@ public function configureOptions(OptionsResolver $resolver) 'join_type' => 'leftJoin', 'type_of_field' => null, 'responsive_priority' => null, + 'sent_in_response' => true, )); $resolver->setAllowedTypes('cell_type', array('null', 'string')); @@ -343,6 +352,7 @@ public function configureOptions(OptionsResolver $resolver) $resolver->setAllowedTypes('join_type', 'string'); $resolver->setAllowedTypes('type_of_field', array('null', 'string')); $resolver->setAllowedTypes('responsive_priority', array('null', 'int')); + $resolver->setAllowedTypes('sent_in_response', array('bool')); $resolver->setAllowedValues('cell_type', array(null, 'th', 'td')); $resolver->setAllowedValues('join_type', array(null, 'join', 'leftJoin', 'innerJoin')); @@ -1075,4 +1085,29 @@ public function setOriginalTypeOfField($originalTypeOfField) return $this; } + + /** + * Get sentInResponse. + * + * @return bool + */ + public function getSentInResponse() + { + return $this->sentInResponse; + } + + /** + * Set sentIntResponse. + * + * @param bool $sentInResponse + * + * @return $this + */ + public function setSentInResponse($sentInResponse) + { + $this->sentInResponse = $sentInResponse; + + return $this; + } + } diff --git a/Resources/doc/columns.md b/Resources/doc/columns.md index 10578300..00b27609 100644 --- a/Resources/doc/columns.md +++ b/Resources/doc/columns.md @@ -40,6 +40,7 @@ With 'null' initialized options uses the default value of the DataTables plugin. | searchable | bool | true | | Enable or disable filtering on the data in this column. | | title | null or string | null | | Set the column title. | | visible | bool | true | | Enable or disable the display of this column. | +| sent_in_response | bool | true | | When set to `false`, allows to access to this field in the `$row` array in Closures without sending this column to the browser (parameter `visible` set to `false` only hides the column in the rendered table). | | width | null or string | null | | Column width assignment. | | add_if | null or Closure | null | | Add column only if conditions are TRUE. | | join_type | string | 'leftJoin' | | Join type (default: 'leftJoin'), if the column represents an association. | diff --git a/Resources/views/column/column.html.twig b/Resources/views/column/column.html.twig index 4bc7ab49..5ed6211a 100644 --- a/Resources/views/column/column.html.twig +++ b/Resources/views/column/column.html.twig @@ -23,11 +23,6 @@ {% if column.width is not same as(null) %} "width": "{{ column.width }}", {% endif %} - {% block title %} - {% if column.title is not same as(null) %} - "title": "{{ column.title|raw }}", - {% endif %} - {% endblock %} {% if column.searchable is same as(true) or column.searchable is same as(false) %} "searchable": {{ column.searchable|sg_datatables_bool_var }}, {% endif %} diff --git a/Resources/views/datatable/columns.html.twig b/Resources/views/datatable/columns.html.twig index 1acdb52f..ac3eeaf0 100644 --- a/Resources/views/datatable/columns.html.twig +++ b/Resources/views/datatable/columns.html.twig @@ -8,6 +8,8 @@ #} "columns": [ {% for column in sg_datatables_view.columnBuilder.columns %} - {% include column.getOptionsTemplate %} + {% if column.sentInResponse %} + {% include column.getOptionsTemplate %} + {% endif %} {% endfor %} ] diff --git a/Resources/views/datatable/datatable_html.html.twig b/Resources/views/datatable/datatable_html.html.twig index 2424c7fc..cb377e1a 100644 --- a/Resources/views/datatable/datatable_html.html.twig +++ b/Resources/views/datatable/datatable_html.html.twig @@ -20,31 +20,44 @@ {% if 'head' == sg_datatables_view.options.individualFilteringPosition or 'both' == sg_datatables_view.options.individualFilteringPosition%} {% for column in sg_datatables_view.columnBuilder.columns %} - {{ column.title }} + {% if column.sentInResponse %} + {{ column.title }} + {% endif %} {% endfor %} {% for column in sg_datatables_view.columnBuilder.columns %} - - {% if column.searchable %} - {{ sg_datatables_render_filter(sg_datatables_view, column, 'head') }} - {% endif %} - + {% if column.sentInResponse %} + + {% if column.searchable %} + {{ sg_datatables_render_filter(sg_datatables_view, column, 'head') }} + {% endif %} + + {% endif %} {% endfor %} {% endif %} {% endif %} + + {% for column in sg_datatables_view.columnBuilder.columns %} + {% if column.sentInResponse %} + {{ column.title }} + {% endif %} + {% endfor %} + {% if true == individual_filtering %} {% if 'foot' == sg_datatables_view.options.individualFilteringPosition or 'both' == sg_datatables_view.options.individualFilteringPosition%} {% for column in sg_datatables_view.columnBuilder.columns %} - - {% if column.searchable %} - {{ sg_datatables_render_filter(sg_datatables_view, column, 'foot') }} - {% endif %} - + {% if column.sentInResponse %} + + {% if column.searchable %} + {{ sg_datatables_render_filter(sg_datatables_view, column, 'foot') }} + {% endif %} + + {% endif %} {% endfor %} diff --git a/Resources/views/datatable/datatable_js.html.twig b/Resources/views/datatable/datatable_js.html.twig index 0857e5fe..bfb98dcc 100644 --- a/Resources/views/datatable/datatable_js.html.twig +++ b/Resources/views/datatable/datatable_js.html.twig @@ -50,7 +50,7 @@ function postCreateDatatable(pipeline) { {% for column in sg_datatables_view.columnBuilder.columns %} - {% if column.renderPostCreateDatatableJsContent is not null %} + {% if column.sentInResponse and column.renderPostCreateDatatableJsContent is not null %} {{ column.renderPostCreateDatatableJsContent|raw }} {% endif %} {% endfor %} diff --git a/Response/DatatableFormatter.php b/Response/DatatableFormatter.php index 6d72f0b2..cbaa7520 100644 --- a/Response/DatatableFormatter.php +++ b/Response/DatatableFormatter.php @@ -121,6 +121,12 @@ public function runFormatter(Paginator $paginator, DatatableInterface $datatable $column->renderCellContent($row); } + foreach ($columns as $column) { + if (!$column->getSentInResponse()) { + unset($row[$column->getDql()]); + } + } + $this->output['data'][] = $row; } } From 9832731612c83b0f2c6331ac6b6d6da7cefe63aa Mon Sep 17 00:00:00 2001 From: Philippe MILINK Date: Wed, 22 Aug 2018 08:32:16 +0200 Subject: [PATCH 2/2] Fix removed title key in column JSON --- Resources/views/column/column.html.twig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Resources/views/column/column.html.twig b/Resources/views/column/column.html.twig index 5ed6211a..4bc7ab49 100644 --- a/Resources/views/column/column.html.twig +++ b/Resources/views/column/column.html.twig @@ -23,6 +23,11 @@ {% if column.width is not same as(null) %} "width": "{{ column.width }}", {% endif %} + {% block title %} + {% if column.title is not same as(null) %} + "title": "{{ column.title|raw }}", + {% endif %} + {% endblock %} {% if column.searchable is same as(true) or column.searchable is same as(false) %} "searchable": {{ column.searchable|sg_datatables_bool_var }}, {% endif %}