MM
Mostrar nome do campo relacionado no Datagrid
Bom dia!
Tabela de Cidades esta relacionada com tabela de Paises. Mas no datagrid traz o ID do pais na tabela Cidade como padrao.
Preciso mostrar o nome do pais tambem.
Table Country
Id
DS_country
Tabel City
Id
Ds_city
Country_id
Em anexo os fontes.
Obrigado
Tabela de Cidades esta relacionada com tabela de Paises. Mas no datagrid traz o ID do pais na tabela Cidade como padrao.
Preciso mostrar o nome do pais tambem.
Table Country
Id
DS_country
Tabel City
Id
Ds_city
Country_id
Em anexo os fontes.
Obrigado
- <?php
- /**
- * TradeCity Active Record
- * @author <your-name-here>
- */
- class Cidade extends TRecord
- {
- const TABLENAME = 'public.trade_city';
- const PRIMARYKEY= 'id';
- const IDPOLICY = 'serial'; // {max, serial}
- private $pais;
- /**
- * Constructor method
- */
- public function __construct($id = NULL, $callObjectLoad = TRUE)
- {
- parent::__construct($id, $callObjectLoad);
- parent::addAttribute('ds_city');
- parent::addAttribute('county_id');
- parent::addAttribute('ds_country');
- }
- /**
- * Method set_pais
- * Sample of usage: $trade_city->pais = $object;
- * @param $object Instance of Pais
- */
- public function set_pais(Pais $object)
- {
- $this->pais = $object;
- $this->country_id = $object->id;
- }
- /**
- * Method get_pais
- * Sample of usage: $trade_city->pais->attribute;
- * @returns Pais instance
- */
- public function get_pais()
- {
- // loads the associated object
- if (empty($this->pais))
- $this->pais = new Pais($this->country_id);
- // returns the associated object
- return $this->pais;
- }
- }
- <?php
- /**
- * CidadeForm Registration
- * @author <your name here>
- */
- class CidadeForm extends TPage
- {
- protected $form; // form
- use Adianti\Base\AdiantiStandardFormTrait; // Standard form methods
- /**
- * Class constructor
- * Creates the page and the registration form
- */
- function __construct()
- {
- parent::__construct();
- $this->setDatabase('Teste'); // defines the database
- $this->setActiveRecord('Cidade'); // defines the active record
- // creates the form
- $this->form = new TQuickForm('form_Cidade');
- $this->form->class = 'tform'; // change CSS class
- $this->form->style = 'display: table;width:100%'; // change style
- // define the form title
- $this->form->setFormTitle('Cidade');
- // create the form fields
- $id = new TEntry('id');
- $ds_city = new TEntry('ds_city');
- $county_id = new TEntry('county_id');
- // add the fields
- $this->form->addQuickField('Id', $id, 100 );
- $this->form->addQuickField('Ds City', $ds_city, 200 );
- $this->form->addQuickField('Country Id', $county_id, 100 );
- if (!empty($id))
- {
- $id->setEditable(FALSE);
- }
- /** samples
- $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
- $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
- $fieldX->setSize( 100, 40 ); // set size
- **/
- // create the form actions
- $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
- $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onEdit')), 'bs:plus-sign green');
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 90%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- parent::add($container);
- }
- }
- <?php
- /**
- * CidadeList Listing
- * @author <your name here>
- */
- class CidadeList extends TStandardList
- {
- protected $form; // registration form
- protected $datagrid; // listing
- protected $pageNavigation;
- protected $formgrid;
- protected $deleteButton;
- protected $transformCallback;
- /**
- * Page constructor
- */
- public function __construct()
- {
- parent::__construct();
- parent::setDatabase('Teste'); // defines the database
- parent::setActiveRecord('Cidade'); // defines the active record
- parent::setDefaultOrder('id', 'asc'); // defines the default order
- // parent::setCriteria($criteria) // define a standard filter
- parent::addFilterField('id', 'like', 'Cod'); // filterField, operator, formField
- parent::addFilterField('ds_city', 'like', 'Nome'); // filterField, operator, formField
- parent::addFilterField('county_id', 'like', 'Cod Pais'); // filterField, operator, formField
- parent::addFilterField('ds_country', 'like', 'Nome'); // filterField, operator, formField
- // creates the form
- $this->form = new TQuickForm('form_search_Cidade');
- $this->form->class = 'tform'; // change CSS class
- $this->form->style = 'display: table;width:100%'; // change style
- $this->form->setFormTitle('Cidade');
- // create the form fields
- $id = new TEntry('id');
- $ds_city = new TEntry('ds_city');
- $county_id = new TEntry('county_id');
- $ds_country = new TLabel('ds_country');
- // add the fields
- $this->form->addQuickField('Id', $id, 200 );
- $this->form->addQuickField('Ds City', $ds_city, 200 );
- $this->form->addQuickField('County Id', $county_id, 200 );
- $this->form->addQuickField('Nome Pais', $ds_country, 200 );
- // keep the form filled during navigation with session data
- $this->form->setData( TSession::getValue('Cidade_filter_data') );
- // add the search form actions
- $this->form->addQuickAction(_t('Find'), new TAction(array($this, 'onSearch')), 'fa:search');
- $this->form->addQuickAction(_t('New'), new TAction(array('CidadeForm', 'onEdit')), 'bs:plus-sign green');
- // creates a DataGrid
- $this->datagrid = new TDataGrid;
- $this->datagrid->style = 'width: 100%';
- $this->datagrid->datatable = 'true';
- // $this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
- // creates the datagrid columns
- $column_id = new TDataGridColumn('id', 'Cod', 'left');
- $column_ds_city = new TDataGridColumn('ds_city', 'Nome', 'left');
- $column_county_id = new TDataGridColumn('county_id', 'Cod', 'left');
- $column_ds_country = new TDataGridColumn('ds_country', 'Nome Pais', 'left');
- // add the columns to the DataGrid
- $this->datagrid->addColumn($column_id);
- $this->datagrid->addColumn($column_ds_city);
- $this->datagrid->addColumn($column_county_id);
- $this->datagrid->addColumn($column_ds_country);
- // creates the datagrid column actions
- $order_ds_country = new TAction(array($this, 'onReload'));
- $order_ds_country->setParameter('order', 'ds_country');
- $column_ds_country->setAction($order_ds_country);
- // create EDIT action
- $action_edit = new TDataGridAction(array('CidadeForm', 'onEdit'));
- $action_edit->setUseButton(TRUE);
- $action_edit->setButtonClass('btn btn-default');
- $action_edit->setLabel(_t('Edit'));
- $action_edit->setImage('fa:pencil-square-o blue fa-lg');
- $action_edit->setField('id');
- $this->datagrid->addAction($action_edit);
- // create DELETE action
- $action_del = new TDataGridAction(array($this, 'onDelete'));
- $action_del->setUseButton(TRUE);
- $action_del->setButtonClass('btn btn-default');
- $action_del->setLabel(_t('Delete'));
- $action_del->setImage('fa:trash-o red fa-lg');
- $action_del->setField('id');
- $this->datagrid->addAction($action_del);
- // create the datagrid model
- $this->datagrid->createModel();
- // create the page navigation
- $this->pageNavigation = new TPageNavigation;
- $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
- $this->pageNavigation->setWidth($this->datagrid->getWidth());
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 90%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- $container->add($this->datagrid);
- $container->add($this->pageNavigation);
- parent::add($container);
- }
- }