Skip to content

Commit

Permalink
2024.03.07
Browse files Browse the repository at this point in the history
- Se implementa el filtro y las columnas ordenables.

@todo: Revisar el icono derecho de los encabezados de columna para que solo el que esté con orden descendente sea el que cambie y no todos.
  • Loading branch information
Néstor Acevedo committed Mar 7, 2024
1 parent f1cd9eb commit bc44308
Show file tree
Hide file tree
Showing 21 changed files with 7,059 additions and 553 deletions.
417 changes: 206 additions & 211 deletions README.md
100755 → 100644

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions composer.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"license": "GPL-3.0+",
"minimum-stability": "dev",
"prefer-stable": true,
"version": "24.01.25-RC1",
"version": "24.03.07-RC2",
"authors": [
{
"name": "Néstor Acevedo",
Expand All @@ -37,4 +37,4 @@
]
}
}
}
}
7 changes: 4 additions & 3 deletions src/Column/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ public function renderFilterCell()
*/
public function renderFooterCell()
{
$options = Html::renderTagAttributes($this->filterOptions);
return str("<td $options>" . $this->renderFilterCellContent() . "</td>")->toHtmlString();
$options = Html::renderTagAttributes($this->footerOptions);
return str("<td $options>" . $this->renderFooterCellContent() . "</td>")->toHtmlString();
}

#region Protected
Expand All @@ -177,7 +177,7 @@ protected function getHeaderCellLabel()
* Renders the header cell content.
* The default implementation simply renders {@see Column::$header}.
* This method may be overridden to customize the rendering of the header cell.
* @return string the rendering result
* @return HtmlString|string the rendering result
*/
protected function renderHeaderCellContent()
{
Expand Down Expand Up @@ -216,6 +216,7 @@ protected function renderFilterCellContent()
*/
protected function renderFooterCellContent()
{

return $this->footer !== null && trim($this->footer) !== '' ? $this->footer : $this->grid->emptyCell;
}

Expand Down
172 changes: 147 additions & 25 deletions src/Column/DataColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,39 @@ class DataColumn extends Column
*/
public $filter;

/** @var string|null */
/**
* The atrribute name associated with this column. If not set, will have the same value as $attribute.
* @var string|null
*/
public $filterAttribute;

/**
* The HTML attributes for the filter input fields. This property is used in combination with the $filter property.
* When {@see DataColumn::$filter} is not set or is an array, this property will be used to render the HTML attributes for the generated
* filter input fields.
*
* See also {@see \neoacevedo\gridview\Support\Html::renderTagAttributes()} for details on how attributes are being rendered.
* @var array
*/
public array $filterInputOptions = [
'class' => 'form-control',
'id' => null,
];

/**
* Label to be displayed in the {@see DataColumn::$header} and also to be used as the sorting link label when sorting is enabled for this column.
* @var string|null
*/
public $label;

/**
* Whether to allow sorting by this column.
* If true and {@see \neoacevedo\gridview\Column\DataColumn::$attribute} is found in the sort definition of {@see \neoacevedo\gridview\GridView::$dataProvider},
* then the header cell of this column will contain a link that may trigger the sorting when being clicked.
* @var bool
*/
public $enableSorting = true;

/**
* Whether the header lable should be HTML-encoded.
* @var bool
Expand All @@ -53,12 +83,20 @@ class DataColumn extends Column
public $value;

/**
* @var string|array|Closure in which format should the value of each data model be displayed as (e.g. `"raw"`, `"text"`, `"html"`,
* `date`, `datetime`). Supported formats are determined by the [[GridView::formatter|formatter]] used by
* the [[GridView]]. Default format is "text" which will format the value as an HTML-encoded plain text.
* @var string|array|Closure In which format should the value of each data model be displayed as (e.g. `"raw"`, `"text"`, `"html"`,
* `date`, `datetime`).
* Supported formats are determined by the {@see \neoacevedo\gridview\GridView::$formatter formatter} used by
* the {@see \neoacevedo\gridview\GridView}. Default format is "text" which will format the value as an HTML-encoded plain text.
*/
public $format = 'text';

/**
* The HTML attributes for the link tag in the header cell when sorting is enabled for this column.
* See also {@see \neoacevedo\gridview\Support\Html::renderTagAttributes()} for details on how attributes are being rendered.
* @var array
*/
public $sortLinkOptions = [];

/**
* Constructor.
*
Expand All @@ -72,9 +110,14 @@ class DataColumn extends Column
public function __construct($config = [])
{
parent::__construct($config);
$this->attribute = $config['attribute'] ?? null;

if (isset($config['attribute'])) {
$this->attribute = $config['attribute'];
}

$this->label = $config['label'] ?? null;
$this->value = $config['value'] ?? null;
$this->filterAttribute = $config['filterAttribute'] ?? $this->attribute;

if (isset($config['encodeLabel'])) {
$this->encodeLabel = $config['encodeLabel'];
Expand All @@ -85,6 +128,26 @@ public function __construct($config = [])
}
}

/**
* Returns the data cell value.
* @param mixed $model the data model
* @param mixed $key the key associated with the data model
* @param int $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
* @return string the data cell value
*/
public function getDataCellValue($model, $key, $index)
{
if ($this->value !== null) {
if (is_string($this->value)) {
return Arr::get((array) $model, $this->value);
}
return call_user_func($this->value, $model, $key, $index, $this);
} elseif ($this->attribute !== null) {
return is_array($model) ? Arr::get($model, $this->attribute) : $model->getAttribute($this->attribute);
}
return null;
}

/**
* {@inheritdoc}
*/
Expand All @@ -99,6 +162,17 @@ protected function renderHeaderCellContent()
$label = Html::encode($label);
}

if ($this->attribute !== null && $this->enableSorting) {
$sorted = request()->get('sort');
$sortClass = substr($sorted, 0, 1) === '-' ? 'icon-link desc' : 'icon-link asc';
$transform = substr($sorted, 0, 1) === '-' ? 'transform: rotate(-180deg);' : '';

$url = request()->fullUrlWithQuery(['sort' => substr($sorted, 0, 1) === '-' ? $this->attribute : '-' . $this->attribute]);

$options = Html::renderTagAttributes(array_merge($this->sortLinkOptions, ['label' => $label, 'class' => $sortClass]));
return str("<a href=\"$url\" $options>$label\n<span class=\"bi bi-triangle-fill\" style=\"font-size: 0.5rem; $transform\"></span></a>")->toHtmlString();
}

return $label;
}

Expand Down Expand Up @@ -153,26 +227,6 @@ protected function getHeaderCellLabel()
return $label;
}

/**
* Returns the data cell value.
* @param mixed $model the data model
* @param mixed $key the key associated with the data model
* @param int $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
* @return string the data cell value
*/
public function getDataCellValue($model, $key, $index)
{
if ($this->value !== null) {
if (is_string($this->value)) {
return Arr::get((array) $model, $this->value);
}
return call_user_func($this->value, $model, $key, $index, $this);
} elseif ($this->attribute !== null) {
return is_array($model) ? Arr::get($model, $this->attribute) : $model->getAttribute($this->attribute);
}
return null;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -258,6 +312,74 @@ protected function renderFilterCellContent()
return $this->filter;
}

if ($this->filter !== false && $this->filterAttribute !== null) {
if (is_array($this->filter)) {
$options = array_merge(['prompt' => '', 'strict' => true,], $this->filterInputOptions);

$name = Arr::get($options, 'name', $this->filterAttribute);
$selection = Arr::get($$options, 'value');

Arr::forget($options, ['name', 'value']);

if (!array_key_exists('unselect', $options)) {
$options['unselect'] = '';
}

if (!array_key_exists('id', $options) || is_null($options['id'])) {
$options['id'] = "filter_$name";
}

$options['name'] = $name;

$selectOptions = Html::renderSelectOptions($selection, $this->filter, $options);

$options = Html::renderTagAttributes($options);

$dropDown = "<select $options>";
$dropDown .= $selectOptions;
$dropDown .= "</select>";
return str($dropDown)->toHtmlString();
} elseif ($this->format === 'boolean') {
// Repetimos todo lo anterior pero dentro del contenido del select reemplazamos el contenido por un array de 2 elementos.
$options = array_merge(['prompt' => '', 'strict' => true,], $this->filterInputOptions);

$name = Arr::get($options, 'name', $this->filterAttribute);
$selection = Arr::get($options, 'value');

Arr::forget($options, ['name', 'value']);

if (!array_key_exists('unselect', $options)) {
$options['unselect'] = '';
}

if (!array_key_exists('id', $options) || is_null($options['id'])) {
$options['id'] = "filter_$name";
}

$options['name'] = $name;

$selectOptions = Html::renderSelectOptions($selection, [
0 => 'No',
1 => 'Yes'
], $options);

$options = Html::renderTagAttributes($options);

$dropDown = "<select $options>";
$dropDown .= $selectOptions;
$dropDown .= "</select>";
return str($dropDown)->toHtmlString();
}

$options = array_merge(['maxlength' => true], $this->filterInputOptions);
$name = Arr::get($options, 'name', $this->filterAttribute);
$options['name'] = $name;
if (!array_key_exists('id', $options) || is_null($options['id'])) {
$options['id'] = "filter_$name";
}
$options = Html::renderTagAttributes($options);
return str("<input type=\"search\" $options />")->toHtmlString();
}
return parent::renderFilterCellContent();
}
}
Loading

0 comments on commit bc44308

Please sign in to comment.