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

Get fields from database without sending them to browser #816

Merged
Merged
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
35 changes: 35 additions & 0 deletions Datatable/Column/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
//-------------------------------------------------
Expand Down Expand Up @@ -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'));
Expand All @@ -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'));
Expand Down Expand Up @@ -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;
}

}
1 change: 1 addition & 0 deletions Resources/doc/columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
4 changes: 3 additions & 1 deletion Resources/views/datatable/columns.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#}
"columns": [
{% for column in sg_datatables_view.columnBuilder.columns %}
{% include column.getOptionsTemplate %}
{% if column.sentInResponse %}
{% include column.getOptionsTemplate %}
{% endif %}
{% endfor %}
]
35 changes: 24 additions & 11 deletions Resources/views/datatable/datatable_html.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,44 @@
{% if 'head' == sg_datatables_view.options.individualFilteringPosition or 'both' == sg_datatables_view.options.individualFilteringPosition%}
<tr>
{% for column in sg_datatables_view.columnBuilder.columns %}
<th>{{ column.title }}</th>
{% if column.sentInResponse %}
<th>{{ column.title }}</th>
{% endif %}
{% endfor %}
</tr>
<tr id="sg-datatables-{{ sg_datatables_view.uniqueName }}-filterrow">
{% for column in sg_datatables_view.columnBuilder.columns %}
<th>
{% if column.searchable %}
{{ sg_datatables_render_filter(sg_datatables_view, column, 'head') }}
{% endif %}
</th>
{% if column.sentInResponse %}
<th>
{% if column.searchable %}
{{ sg_datatables_render_filter(sg_datatables_view, column, 'head') }}
{% endif %}
</th>
{% endif %}
{% endfor %}
</tr>
{% endif %}
{% endif %}
<tr>
{% for column in sg_datatables_view.columnBuilder.columns %}
{% if column.sentInResponse %}
<th>{{ column.title }}</th>
{% endif %}
{% endfor %}
</tr>
</thead>
{% if true == individual_filtering %}
{% if 'foot' == sg_datatables_view.options.individualFilteringPosition or 'both' == sg_datatables_view.options.individualFilteringPosition%}
<tfoot>
<tr>
{% for column in sg_datatables_view.columnBuilder.columns %}
<td>
{% if column.searchable %}
{{ sg_datatables_render_filter(sg_datatables_view, column, 'foot') }}
{% endif %}
</td>
{% if column.sentInResponse %}
<td>
{% if column.searchable %}
{{ sg_datatables_render_filter(sg_datatables_view, column, 'foot') }}
{% endif %}
</td>
{% endif %}
{% endfor %}
</tr>
</tfoot>
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/datatable/datatable_js.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
6 changes: 6 additions & 0 deletions Response/DatatableFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down