Skip to content
7 changes: 7 additions & 0 deletions demos/collection/grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@

$grid->setModel($model);

// add country flag column
$grid->addColumn('flag', [
Table\Column\Flag::class,
'codeField' => $model->fieldName()->iso,
'nameField' => $model->fieldName()->name,
]);

// Adding Quicksearch on Name field using auto query.
$grid->addQuickSearch([$model->fieldName()->name], true);

Expand Down
29 changes: 29 additions & 0 deletions demos/collection/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/* TODO - merge this into /demos/interactive/cardtable.php */
declare(strict_types=1);

namespace Atk4\Ui\Demos;

use Atk4\Ui\CardTable;
use Atk4\Ui\Table;

/** @var \Atk4\Ui\App $app */
require_once __DIR__ . '/../init-app.php';

$m = new Country($app->db);
$m->addField('flag', [
'neverPersist' => true, // no need for actual value in this field
'ui' => [
'table' => [
Table\Column\Flag::class,
[
'codeField' => $m->fieldName()->iso,
'nameField' => $m->fieldName()->name,
],
],
],
]);

$e = $m->loadAny();
$t = CardTable::addTo($app);
$t->setModel($e);
15 changes: 10 additions & 5 deletions src/CardTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,20 @@ public function setModel(Model $model, array $columns = null): void
}

$data = [];
foreach ($model->get() as $key => $value) {
if (in_array($key, $columns, true)) {
foreach (array_keys($model->get()) as $fieldName) {
if (in_array($fieldName, $columns, true)) {
$data[] = [
'id' => $key,
'field' => $model->getField($key)->getCaption(),
'value' => $this->getApp()->uiPersistence->typecastSaveField($model->getField($key), $value),
'id' => $fieldName,
'field' => $model->getField($fieldName)->getCaption(),
'value' => new Model\EntityFieldPair($model, $fieldName),
];
}
}

$this->_bypass = true;

parent::setSource($data);
/*
$mm = parent::setSource($data);
$this->addDecorator('value', [Table\Column\Multiformat::class, function (Model $row) use ($model) {
$field = $model->getField($row->getId());
Expand All @@ -58,6 +61,8 @@ public function setModel(Model $model, array $columns = null): void

return [$ret];
}]);
*/

$this->_bypass = false;
}
}
2 changes: 1 addition & 1 deletion src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public function renderRow(): void
if (!is_array($columns)) {
$columns = [$columns];
}
$field = !is_int($name) && $this->model->hasField($name) ? $this->model->getField($name) : null;
$field = is_int($name) ? null : $this->model->getField($name);
Comment thread
DarkSide666 marked this conversation as resolved.
foreach ($columns as $column) {
$html_tags = array_merge($column->getHtmlTags($this->model, $field), $html_tags);
}
Expand Down
34 changes: 34 additions & 0 deletions src/Table/Column/Flag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Atk4\Ui\Table\Column;

use Atk4\Data\Field;
use Atk4\Data\Model;
use Atk4\Ui\Table;

/**
* Column for formatting country code as flags.
*/
class Flag extends Table\Column
{
/** @var string Name of model field which contains country ALPHA-2 (2 letter) codes. */
public $codeField;

/** @var string|null Optional name of model field which contains country names. */
public $nameField;

public function getHtmlTags(Model $row, ?Field $field): array
{
$countryCode = $row->get($this->codeField);
$countryName = $this->nameField ? $row->get($this->nameField) : null;

return [
$field->shortName => $countryCode === null ? '' : $this->getApp()->getTag('i', [
'class' => strtolower($countryCode) . ' flag',
'title' => $countryCode . ($countryName === null ? '' : ' - ' . $countryName),
Comment thread
DarkSide666 marked this conversation as resolved.
Outdated
]),
];
}
}