PG
Incluir Grid dentro de um formulario !
estou tentando incluir um grid
dentro desse formulario.
Criei $subform (2 abas) endereco princiapal - endereco outros.
coloquei os campos no endereco princial ,
so que agora nao consigo colocar um grid, com a lista dos outros enderecos
na outra aba, ????
dentro desse formulario.
Criei $subform (2 abas) endereco princiapal - endereco outros.
coloquei os campos no endereco princial ,
so que agora nao consigo colocar um grid, com a lista dos outros enderecos
na outra aba, ????
- <?php
- /**
- * PessoaForm
- *
- * @version 1.0
- * @package erphouse
- * @subpackage control
- * @author Pablo Dall'Oglio
- * @copyright Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
- * @license http://www.adianti.com.br/framework-license
- */
- class PessoaForm extends TWindow
- {
- protected $form; // form
- /**
- * Form constructor
- * @param $param Request
- */
- public function __construct( $param )
- {
- parent::__construct();
- parent::setSize(0.8, null);
- parent::removePadding();
- parent::removeTitleBar();
- //parent::disableEscape();
- // creates the form
- $this->form = new BootstrapFormBuilder('form_Pessoa');
- $this->form->setFormTitle('Pessoa');
- $this->form->setProperty('style', 'margin:0;border:0');
- $this->form->setClientValidation(true);
- // create the form fields
- $id = new TEntry('id');
- $nome = new TEntry('nome');
- $nome_fantasia = new TEntry('nome_fantasia');
- $tipo = new TCombo('tipo');
- $codigo_nacional = new TEntry('codigo_nacional');
- $codigo_estadual = new TEntry('codigo_estadual');
- $codigo_municipal = new TEntry('codigo_municipal');
- $fone = new TEntry('fone');
- $celular = new TEntry('celular');
- $email = new TEntry('email');
- $email_nfe = new TEntry('email_nfe');
- $observacao = new TText('observacao');
- $cep = new TEntry('cep');
- $logradouro = new TEntry('logradouro');
- $numero = new TEntry('numero');
- $complemento = new TEntry('complemento');
- $bairro = new TEntry('bairro');
- $filter = new TCriteria;
- $filter->add(new TFilter('id', '<', '0'));
- $cidade_id = new TDBCombo('cidade_id', 'tutorerpdfe', 'Cidade', 'id', 'nome', 'nome', $filter);
- $grupo_id = new TDBUniqueSearch('grupo_id', 'tutorerpdfe', 'Grupo', 'id', 'nome');
- $papeis_id = new TDBMultiSearch('papeis_id', 'tutorerpdfe', 'Papel', 'id', 'nome');
- $estado_id = new TDBCombo('estado_id', 'tutorerpdfe', 'Estado', 'id', '{nome} ({uf})');
- $estado_id->setChangeAction( new TAction( [$this, 'onChangeEstado'] ) );
- $cep->setExitAction( new TAction([ $this, 'onExitCEP']) );
- $codigo_nacional->setExitAction( new TAction( [$this, 'onExitCNPJ'] ) );
- $cidade_id->enableSearch();
- $estado_id->enableSearch();
- $grupo_id->setMinLength(0);
- $papeis_id->setMinLength(0);
- $papeis_id->setSize('100%', 60);
- $observacao->setSize('100%', 60);
- $tipo->addItems( ['F' => 'Física', 'J' => 'Jurídica' ] );
- // add the fields
- $row1=$this->form->addFields( [ new TLabel('Id') ], [ $id ] );
- $row2=$this->form->addFields( [ new TLabel('Tipo') ], [ $tipo ], [ new TLabel('CPF/CNPJ') ], [ $codigo_nacional ] );
- $row3=$this->form->addFields( [ new TLabel('Nome') ], [ $nome ] );
- $row4=$this->form->addFields( [ new TLabel('Nome Fantasia') ], [ $nome_fantasia ] );
- $row5=$this->form->addFields( [ new TLabel('Papéis')], [ $papeis_id ], [ new TLabel('Grupo') ], [ $grupo_id ] );
- $row6=$this->form->addFields( [ new TLabel('I.E.') ], [ $codigo_estadual ], [ new TLabel('I.M.') ], [ $codigo_municipal ] );
- $row7=$this->form->addFields( [ new TLabel('Fone') ], [ $fone ], [ new TLabel('Celular') ], [ $celular ] );
- $row8=$this->form->addFields( [ new TLabel('Email') ], [ $email ], [ new TLabel('Email-NFe') ], [ $email_nfe ] );
- $row9=$this->form->addFields( [ new TLabel('Observacao') ], [ $observacao ] );
- $row10=$this->form->addContent( [new TFormSeparator('Endereço')]);
- $subform = new BootstrapFormBuilder;
- $subform->setFieldSizes('100%');
- $subform->setProperty('style', 'border:none');
- $subform->appendPage( 'Endereço Principal' );
- $row11=$subform->addFields( [ new TLabel('Cep') ], [ $cep ] );
- $row11->layout = [ 'col-sm-2 control-label', 'col-sm-2', 'col-sm-6', 'col-sm-2' ];
- $row12=$subform->addFields( [ new TLabel('Logradouro') ], [ $logradouro ], [ new TLabel('Numero') ], [ $numero ] );
- $row13=$subform->addFields( [ new TLabel('Complemento') ], [ $complemento ], [ new TLabel('Bairro') ], [ $bairro ] );
- $row14=$subform->addFields( [ new TLabel('Estado') ], [$estado_id], [ new TLabel('Cidade') ], [ $cidade_id ] );
- $row1->class = 'form-control-sm';
- $row2->class = 'form-control-sm';
- $row3->class = 'form-control-sm';
- $row4->class = 'form-control-sm';
- $row5->class = 'form-control-sm';
- $row6->class = 'form-control-sm';
- $row7->class = 'form-control-sm';
- $row8->class = 'form-control-sm';
- $row9->class = 'form-control-sm';
- $row10->class = 'form-control-sm';
- $row11->class = 'form-control-sm';
- $row12->class = 'form-control-sm';
- $row13->class = 'form-control-sm';
- $row14->class = 'form-control-sm';
- // set sizes
- $id->setSize('100%');
- $nome->setSize('100%');
- $nome_fantasia->setSize('100%');
- $tipo->setSize('100%');
- $codigo_nacional->setSize('100%');
- $codigo_estadual->setSize('100%');
- $codigo_municipal->setSize('100%');
- $fone->setSize('100%');
- $celular->setSize('100%');
- $email_nfe->setSize('100%');
- $email->setSize('100%');
- $observacao->setSize('100%');
- $cep->setSize('100%');
- $logradouro->setSize('100%');
- $numero->setSize('100%');
- $complemento->setSize('100%');
- $bairro->setSize('100%');
- $cidade_id->setSize('100%');
- $grupo_id->setSize('100%');
- $cep->setMask('99.999-999');
- $id->setEditable(FALSE);
- $nome->addValidation('Nome', new TRequiredValidator);
- $nome_fantasia->addValidation('Nome Fantasia', new TRequiredValidator);
- $tipo->addValidation('Tipo', new TRequiredValidator);
- $codigo_nacional->addValidation('CPF/CNPJ', new TRequiredValidator);
- $grupo_id->addValidation('Grupo', new TRequiredValidator);
- $fone->addValidation('Fone', new TRequiredValidator);
- $celular->addValidation('Celular', new TRequiredValidator);
- $email->addValidation('Email', new TRequiredValidator);
- $email_nfe->addValidation('Email-NFe', new TEmailValidator);
- $cidade_id->addValidation('Cidade', new TRequiredValidator);
- $cep->addValidation('CEP', new TRequiredValidator);
- $logradouro->addValidation('Logradouro', new TRequiredValidator);
- $numero->addValidation('Número', new TRequiredValidator);
- $subform->appendPage( 'Endereço Outros' );
- $this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
- $this->datagrid->style = 'width: 100%';
- $this->datagrid->class = 'table-sm table-striped table-hover';
- // creates the datagrid columns
- $column_endereco_id = new TDataGridColumn('enderco_id', 'Endereco', 'left');
- $column_logradouro = new TDataGridColumn('logradouro', 'Logradouro', 'left');
- $column_bairro = new TDataGridColumn('bairro', 'Bairro', 'left');
- $column_cep = new TDataGridColumn('cep', 'Cep', 'left');
- $column_cidade_id = new TDataGridColumn('cidade_id', 'Cidade', 'left');
- // add the columns to the DataGrid
- $this->datagrid->addColumn($column_endereco_id);
- $this->datagrid->addColumn($column_logradouro);
- $this->datagrid->addColumn($column_bairro);
- $this->datagrid->addColumn($column_cep);
- $this->datagrid->addColumn($column_cidade_id);
- $this->datagrid->createModel();
- // create the form actions
- $this->form->addHeaderActionLink( _t('Close'), new TAction([__CLASS__, 'onClose'], ['static'=>'1']), 'fa:times red');
- $btn = $this->form->addAction(_t('Save'), new TAction([$this, 'onSave']), 'fa:save');
- $btn->class = 'btn btn-sm btn-primary';
- $this->form->addActionLink(_t('New'), new TAction([$this, 'onEdit']), 'fa:eraser red');
- $this->form->addContent( [$subform] );
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 100%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- parent::add($container);
- }
- /**
- * Save form data
- * @param $param Request
- */
- public function onSave( $param )
- {
- try
- {
- TTransaction::open('tutorerpdfe'); // open a transaction
- $this->form->validate(); // validate form data
- $data = $this->form->getData(); // get form data as array
- $object = new Pessoa; // create an empty object
- $object->fromArray( (array) $data); // load the object with data
- $object->store(); // save the object
- PessoaPapel::where('pessoa_id', '=', $object->id)->delete();
- if ($data->papeis_id)
- {
- foreach ($data->papeis_id as $papel_id)
- {
- $pp = new PessoaPapel;
- $pp->pessoa_id = $object->id;
- $pp->papel_id = $papel_id;
- $pp->store();
- }
- }
- // get the generated id
- $data->id = $object->id;
- $this->form->setData($data); // fill form data
- TTransaction::close(); // close the transaction
- new TMessage('info', AdiantiCoreTranslator::translate('Record saved'));
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- $this->form->setData( $this->form->getData() ); // keep form data
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * Clear form data
- * @param $param Request
- */
- public function onClear( $param )
- {
- $this->form->clear(TRUE);
- }
- /**
- * Load object to form data
- * @param $param Request
- */
- public function onEdit( $param )
- {
- try
- {
- if (isset($param['key']))
- {
- $key = $param['key'];
- TTransaction::open('tutorerpdfe');
- $object = new Pessoa($key);
- $object->papeis_id = PessoaPapel::where('pessoa_id', '=', $object->id)->getIndexedArray('papel_id');
- $this->form->setData($object);
- // force fire events
- $data = new stdClass;
- $data->estado_id = $object->cidade->estado->id;
- $data->cidade_id = $object->cidade_id;
- TForm::sendData('form_Pessoa', $data);
- TTransaction::close();
- }
- else
- {
- $this->form->clear(TRUE);
- }
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * Action to be executed when the user changes the state
- * @param $param Action parameters
- */
- public static function onChangeEstado($param)
- {
- try
- {
- TTransaction::open('tutorerpdfe');
- if (!empty($param['estado_id']))
- {
- $criteria = TCriteria::create( ['estado_id' => $param['estado_id'] ] );
- // formname, field, database, model, key, value, ordercolumn = NULL, criteria = NULL, startEmpty = FALSE
- TDBCombo::reloadFromModel('form_Pessoa', 'cidade_id', 'tutorerpdfe', 'Cidade', 'id', '{nome} ({id})', 'nome', $criteria, TRUE);
- }
- else
- {
- TCombo::clearField('form_Pessoa', 'cidade_id');
- }
- TTransaction::close();
- }
- catch (Exception $e)
- {
- new TMessage('error', $e->getMessage());
- }
- }
- /**
- * Autocompleta outros campos a partir do CNPJ
- */
- public static function onExitCNPJ($param)
- {
- session_write_close();
- try
- {
- $cnpj = preg_replace('/[^0-9]/', '', $param['codigo_nacional']);
- $url = 'http://receitaws.com.br/v1/cnpj/'.$cnpj;
- $content = @file_get_contents($url);
- if ($content !== false)
- {
- $cnpj_data = json_decode($content);
- $data = new stdClass;
- if (is_object($cnpj_data) && $cnpj_data->status !== 'ERROR')
- {
- $data->tipo = 'J';
- $data->nome = $cnpj_data->nome;
- $data->nome_fantasia = !empty($cnpj_data->fantasia) ? $cnpj_data->fantasia : $cnpj_data->nome;
- if (empty($param['cep']))
- {
- $data->cep = $cnpj_data->cep;
- $data->numero = $cnpj_data->numero;
- }
- if (empty($param['fone']))
- {
- $data->fone = $cnpj_data->telefone;
- }
- if (empty($param['email']))
- {
- $data->email = $cnpj_data->email;
- }
- TForm::sendData('form_Pessoa', $data, false, true);
- }
- else
- {
- $data->nome = '';
- $data->nome_fantasia = '';
- $data->cep = '';
- $data->numero = '';
- $data->telefone = '';
- $data->email = '';
- TForm::sendData('form_Pessoa', $data, false, true);
- }
- }
- }
- catch (Exception $e)
- {
- new TMessage('error', $e->getMessage());
- }
- }
- /**
- * Autocompleta outros campos a partir do CEP
- */
- public static function onExitCEP($param)
- {
- session_write_close();
- try
- {
- $cep = preg_replace('/[^0-9]/', '', $param['cep']);
- $url = 'https://viacep.com.br/ws/'.$cep.'/json/unicode/';
- $content = @file_get_contents($url);
- if ($content !== false)
- {
- $cep_data = json_decode($content);
- $data = new stdClass;
- if (is_object($cep_data) && empty($cep_data->erro))
- {
- TTransaction::open('tutorerpdfe');
- $estado = Estado::where('uf', '=', $cep_data->uf)->first();
- $cidade = Cidade::where('codigo_ibge', '=', $cep_data->ibge)->first();
- TTransaction::close();
- $data->logradouro = $cep_data->logradouro;
- $data->complemento = $cep_data->complemento;
- $data->bairro = $cep_data->bairro;
- $data->estado_id = $estado->id ?? '';
- $data->cidade_id = $cidade->id ?? '';
- TForm::sendData('form_Pessoa', $data, false, true);
- }
- else
- {
- $data->logradouro = '';
- $data->complemento = '';
- $data->bairro = '';
- $data->estado_id = '';
- $data->cidade_id = '';
- TForm::sendData('form_Pessoa', $data, false, true);
- }
- }
- }
- catch (Exception $e)
- {
- new TMessage('error', $e->getMessage());
- }
- }
- /**
- * Closes window
- */
- public static function onClose()
- {
- parent::closeWindow();
- }
- }
A outra aba é o $subform?
Se for, $subform->addConten([$sua_grid]);
Blz. deu certo.
agora da esse erro quando vou preencher o grid.
Sem transação ativa com a base de dados: AdiantiDatabaseTRepository::load "public"."pessoa_endereco"
$this->datagrid->createModel();
$items = PessoaEndereco::where('pessoa_id', '=', $this->id)->orderBy('id', 'desc')->take(5)->load();
$this->datagrid->addItems($items);
$subform->addContent([$this->datagrid]);
Paulo
para usar um model, vc precisa abrir a transação: TTransaction::open().