DD
Ocultar campos no form
Estou tentando ocultar alguns campos dentro do form através de um evento. Vi que com o TQuickForm::hideField dá pra ocultar campos dentro do form, mas para isso tem que usar o TQuickForm, Como eu estou usando o TForm com TNotebook, li uma postagem do D'aloglio que posso usar o TQuickForm dentro de um TForm, mas não está funcionando.
separei um pedaço do código pra mostrar a voçês, talvez alquem possa me dizer o que está errado, ou sugerir outra maneira de eu fazer.
$this->form = new TForm('form_name');
$this->form->class = 'tform'; // CSS class
$table = new TTable;
$this->notebook = new TNotebook(850, 650);
$this->notebook->appendPage('Notebook', $table);
$this->form->add($this->notebook);
$tipo = new TDBCombo('tipo','database','Tabela','codigo','descricao','codigo');
$cnpj = new TEntry('cnpj');
$cpf = new TEntry('cpf');
$identificacao = new TEntry('identificacao');
// define the action for tab_tp_importador
$change_action = new TAction(array($this, 'onTipoAction'));
$tipo->setChangeAction($change_action);
// create the form using TQuickForm class
$this->form_tipo = new TQuickForm('form_tipo');
$this->form_tipo->class = 'tform';
$this->form_tipo->setFormTitle('Tipo');
$row = $table->addRow();
$row->addCell($this->form_tipo);
$this->form_tipo->addQuickFields('Tipo', array($tipo) );
$this->form_tipo->addQuickField('CNPJ', $cnpj);
$this->form_tipo->addQuickField('CPF', $cpf);
$this->form_tipo->addQuickField('Identificação', $identificacao);
$this->formFields = array($tipo, $cnpj, $cpf, $identificacao);
$this->form->setFields( $this->formFields );
$vbox = new TVBox;
$vbox->add(new TXMLBreadCrumb('menu.xml', 'FormList'));
$vbox->add($this->form);
// add the form inside the page
parent::add($vbox);
}
public static function onTipoAction($param)
{
$obj = new StdClass;
if ($param['tipo'] == '1'){
TQuickForm::showField('form_tipo', 'cnpj');
TQuickForm::hideField('form_tipo', 'cpf');
TQuickForm::hideField('form_tipo', 'identificacao');
$obj->import_cpf = '';
$obj->import_md_identificacao = '';
}
if ($param['tipo'] == '2' OR $param['tipo'] == '3'){
TQuickForm::showField('form_tipo', 'cpf');
TQuickForm::hideField('form_tipo', 'cnpj');
TQuickForm::hideField('form_tipo', 'identificacao');
$obj->import_cnpj = '';
$obj->import_md_identificacao = '';
}
if ($param['tipo'] == '4' OR $param['tipo'] == '5'){
TQuickForm::showField('form_tipo', 'identificacao');
TQuickForm::hideField('form_tipo', 'cnpj');
TQuickForm::hideField('form_tipo', 'cpf');
$obj->import_cpf = '';
$obj->import_cnpj = '';
}
TForm::sendData('form_tipo', $obj);
}
separei um pedaço do código pra mostrar a voçês, talvez alquem possa me dizer o que está errado, ou sugerir outra maneira de eu fazer.
$this->form = new TForm('form_name');
$this->form->class = 'tform'; // CSS class
$table = new TTable;
$this->notebook = new TNotebook(850, 650);
$this->notebook->appendPage('Notebook', $table);
$this->form->add($this->notebook);
$tipo = new TDBCombo('tipo','database','Tabela','codigo','descricao','codigo');
$cnpj = new TEntry('cnpj');
$cpf = new TEntry('cpf');
$identificacao = new TEntry('identificacao');
// define the action for tab_tp_importador
$change_action = new TAction(array($this, 'onTipoAction'));
$tipo->setChangeAction($change_action);
// create the form using TQuickForm class
$this->form_tipo = new TQuickForm('form_tipo');
$this->form_tipo->class = 'tform';
$this->form_tipo->setFormTitle('Tipo');
$row = $table->addRow();
$row->addCell($this->form_tipo);
$this->form_tipo->addQuickFields('Tipo', array($tipo) );
$this->form_tipo->addQuickField('CNPJ', $cnpj);
$this->form_tipo->addQuickField('CPF', $cpf);
$this->form_tipo->addQuickField('Identificação', $identificacao);
$this->formFields = array($tipo, $cnpj, $cpf, $identificacao);
$this->form->setFields( $this->formFields );
$vbox = new TVBox;
$vbox->add(new TXMLBreadCrumb('menu.xml', 'FormList'));
$vbox->add($this->form);
// add the form inside the page
parent::add($vbox);
}
public static function onTipoAction($param)
{
$obj = new StdClass;
if ($param['tipo'] == '1'){
TQuickForm::showField('form_tipo', 'cnpj');
TQuickForm::hideField('form_tipo', 'cpf');
TQuickForm::hideField('form_tipo', 'identificacao');
$obj->import_cpf = '';
$obj->import_md_identificacao = '';
}
if ($param['tipo'] == '2' OR $param['tipo'] == '3'){
TQuickForm::showField('form_tipo', 'cpf');
TQuickForm::hideField('form_tipo', 'cnpj');
TQuickForm::hideField('form_tipo', 'identificacao');
$obj->import_cnpj = '';
$obj->import_md_identificacao = '';
}
if ($param['tipo'] == '4' OR $param['tipo'] == '5'){
TQuickForm::showField('form_tipo', 'identificacao');
TQuickForm::hideField('form_tipo', 'cnpj');
TQuickForm::hideField('form_tipo', 'cpf');
$obj->import_cpf = '';
$obj->import_cnpj = '';
}
TForm::sendData('form_tipo', $obj);
}
Denilson, você tem o link dessa postagem do Pablo? Não entendi como isso funcionaria, visto que teríamos um formulário dentro do outro.
Sem usar a TQuickForm você poderia fazer o seguinte:
As funções showField e hideField usam o atributo 'class=tformrow' para identificar quais linhas devem ser exibidas/ocultadas.
Obs: lembre de alterar o nome do formulário na função onTipoAction para 'form_name' no lugar de 'form_tipo'
Obrigado pela dica Nataniel.
consegui ocultar através do Jquery, descrevo abaixo como ficou:
$subtable = new TTable;
$frame->add($subtable);
$subtable->addRowSet( new TLabel('Tipo'), array($tipo) );
$row_cnpj = $subtable->addRowSet( new TLabel('CNPJ'), array($cnpj) );
$row_cnpj->id = 'cnpj';
$row_cpf = $subtable->addRowSet( new TLabel('CPF'), array($cpf) );
$row_cpf->id = 'cpf';
$row_id = $subtable->addRowSet( new TLabel('Identificação'), array($identificacao) );
$row_id->id = 'id';
na função do evento onExit ficou assim:
public static function onTipoAction($param)
{
$obj = new StdClass;
if ($param['tipo'] == '1'){
TScript::create("$('[id=cnpj]').show();");
TScript::create("$('[id=cpf]').hide();");
TScript::create("$('[id=id]').hide();");
$obj->cpf = '';
$obj->identificacao = '';
}
if ($param['tipo'] == '2'){
TScript::create("$('[id=cnpj]').hide();");
TScript::create("$('[id=cpf]').show();");
TScript::create("$('[id=id]').hide();");
$obj->cnpj = '';
$obj->identificacao = '';
}
if ($param['tipo'] == '3'){
TScript::create("$('[id=cnpj]').hide();");
TScript::create("$('[id=cpf]').hide();");
TScript::create("$('[id=id]').show();");
$obj->cpf = '';
$obj->cnpj = '';
}
TForm::sendData('form', $obj);
}