Definice DataGrid v samostatném souboru
- pernica@tomp.eu
- Člen | 8
Ahoj,
chtěl bych definici datagridu vyhodit z prezenteru do samostatného souboru.
DataGrid použiji na dvou místech – v prezentéru ClientPresenter s ID
klienta (vyfiltruje dodací listy podle klienta).
- Nejsem si jist, zda to mám principiálně správně.
- Funguje to celkem dobře. Ale pokud přidám do dataSource parametr id klienta, tak po ajaxovém překrelení zmizí a zobrazuje se to bez podmínky.
- Je to moje první Nette aplikace, tak přijímám jakékoliv připomínky ;-)
DataGridFactory.php – vyrobi formular s nejakym zakladnim nastavenim
<?php
declare(strict_types=1);
namespace App\Controls;
use Ublaboo\DataGrid\DataGrid;
use Ublaboo\DataGrid\Localization\SimpleTranslator;
class DataGridFactory
{
public function __construct()
{
}
public function create()
{
$translator = new SimpleTranslator([
'ublaboo_datagrid.no_item_found_reset' => 'Žádné položky nenalezeny. Filtr můžete vynulovat',
'ublaboo_datagrid.no_item_found' => 'Žádné položky nenalezeny.',
'ublaboo_datagrid.here' => 'zde',
'ublaboo_datagrid.items' => 'Položky',
'ublaboo_datagrid.all' => 'všechny',
'ublaboo_datagrid.from' => 'z',
'ublaboo_datagrid.reset_filter' => 'Resetovat filtr',
'ublaboo_datagrid.group_actions' => 'Hromadné akce',
'ublaboo_datagrid.show_all_columns' => 'Zobrazit všechny sloupce',
'ublaboo_datagrid.hide_column' => 'Skrýt sloupec',
'ublaboo_datagrid.action' => 'Akce',
'ublaboo_datagrid.previous' => 'Předchozí',
'ublaboo_datagrid.next' => 'Další',
'ublaboo_datagrid.choose' => 'Vyberte',
'ublaboo_datagrid.execute' => 'Provést',
'Name' => 'Jméno',
'Inserted' => 'Vloženo'
]);
$grid = new DataGrid;
$grid->setTranslator($translator);
$grid->setTemplateFile(__DIR__ . '/DataGridControl.latte');
return $grid;
}
public function render()
{
}
}
DeliveryNoteDataGridFactory.php – vlastni datagrid
<?php
declare(strict_types=1);
namespace App\Controls;
use App\Model\ClientManager;
use App\Model\UserManager;
use App\Model\DeliveryNoteManager;
use Ublaboo\DataGrid\DataGrid;
class DeliveryNoteDataGridFactory
{
public function __construct(
public DataGridFactory $dataGridFactory,
public DeliveryNoteManager $deliveryNoteManager,
public ClientManager $clientManager,
public UserManager $userManager,
) {
}
public function create(?int $id = 0) : DataGrid
{
$grid = $this->dataGridFactory->create();
if ($id)
$conditions = ["id_klient" => $id];
else $conditions = [];
$grid->setDataSource($this->deliveryNoteManager->find($conditions));
$grid->addColumnLink('number', 'Číslo', 'DeliveryNote:show', 'number', ['id' => 'id'])->setSortable();;
$grid->addColumnLink('faktura', 'Faktura')
->setSortable();
$grid->addColumnDateTime('date', 'Datum')
->setFormat('j. n. Y')
->setAlign('end')
->setSortable();
$grid->addColumnText('id_klient', 'Klient')
->setRenderer(function ($item) {
if ($item === null) {
return "";
} else {
$client = $this->clientManager->get($item->id_klient);
if (empty($client)) return "";
else {
$client->toArray();
return $client["showAs"];
}
}
})
->setSortable();
$grid->addColumnText('priceTotal', 'Částka')
->setRenderer(function ($item) {
return number_format($item->priceTotal + 0, 2, ",", " ") . " Kč";
})
->setSortable()
->setAlign('end');
$grid->addColumnText('remainsPaid', 'K likvidaci')
->setRenderer(function ($item) {
return number_format($item->remainsPaid + 0, 2, ",", " ") . " Kč";
})
->setSortable()
->setAlign('end');
$grid->addColumnText('id_stav', 'Stav')
->setRenderer(function ($item) {
return $this->deliveryNoteManager->getState($item->id_stav)->dl_stav_nazev;
})
->setSortable();
$grid->addColumnText('id_dealer', 'Dealer')
->setRenderer(function ($item) {
return $this->userManager->get($item->id_dealer)->nick;
})
->setSortable();
$grid->setDefaultSort(['date' => 'DESC', 'id' => 'ASC']);
return $grid;
}
}
ClientPresenter.php
<?php
declare(strict_types=1);
namespace app\Presenters\Client;
use App\Presenters\BasePresenter;
use App\Model\DeliveryNoteManager;
use App\Model\ClientManager;
use App\Controls\ClientDataGridControl;
use App\Forms\ClientFormFactory;
use Nette\Application\UI\Form;
use App\Controls\DeliveryNoteDataGridFactory;
final class ClientPresenter extends BasePresenter
{
public ?int $managedId = null;
public function __construct(
private ClientManager $clientManager,
private ClientFormFactory $formFactory,
private ClientDataGridControl $clientDataGridControl,
private DeliveryNoteDataGridFactory $deliveryNoteDataGridFactory,
private DeliveryNoteManager $deliveryNoteManager,
) {
parent::__construct();
}
public function renderDefault(): void
{
$this->template->clients = $this->clientManager->find();
}
public function renderShow(int $id, string $tab="deliveryNotes"): void
{
$this->managedId = $id;
bdump($this->managedId, "show");
$client = $this->clientManager->get($id);
if (!$client) {
$this->error('Klient nebyl nalezen');
}
$this->template->client = $client;
$this->template->clientStats = $this->clientManager->getClientStats($client->id);
$this->template->tab = $tab;
}
public function createComponentClientDataGrid()
{
return $this->clientDataGridControl->dataGrid();
}
protected function createComponentClientForm(): Form
{
return $this->formFactory->create(function ($id) {
$this->flashMessage("Provedeno úspěně.");
$this->redirect('Client:show', ['id' => $id]);
});
}
public function actionCreate()
{
}
public function actionEdit(int $id): void
{
$client = $this->clientManager->get($id);
if (!$client) {
$this->error('Klient nebyl nalezen');
}
$values = $client->toArray();
$this["clientForm"]->setDefaults($values);
$this->template->client = $client;
}
public function actionDelete(int $id): void
{
$client = $this->clientManager->get($id);
if (!$client) {
$this->error('Klient nebyl nalezen');
}
$this->clientManager->delete($id);
$this->flashMessage("Operace provedena.");
$this->redirect("Client:default");
}
public function actionRebuildShowAs()
{
$this->clientManager->rebuildShowAs();
$this->flashMessage("Rebuild Client.showAs byl proveden.");
$this->redirect("Client:default");
}
public function createComponentDeliveryNoteDataGrid()
{
$grid = $this->deliveryNoteDataGridFactory->create($this->managedId);
$grid->setItemsPerPageList([15], true);
return $grid;
}
}
Editoval pernica@tomp.eu (3. 1. 14:57)
- Kamil Valenta
- Člen | 821
Udělej si z managedId persistentní parametr. Pak se bude přenášet i pro další (ajaxové) requesty.
- pernica@tomp.eu
- Člen | 8
Ahoj. Díky. Pomohlo to.
Ale ještě jsem přehodil $this->managedId = $id; do actionShow(), provádí
se dříve než renderShow(). Funguje to. A navíc nemám ten parametr v URL
dvakrát (id, managedId), ale jen jednou (id).
public function actionShow(int $id, string $tab="deliveryNotes"): void
{
$this->managedId = $id;
}