GG
Ajuda na Criação de um formulário simples
Pessoal,
tentei criar um formulário master/detail no theme 4 e não tive muito sucesso. Tentei então pegar um formulário pronto "SystemGroupForm" e padroniza-lo para utilizar em outros que eu precisar.
No entanto, estou com problemas simples aqui que não consigo resolver.
Nesse exemplo tenho uma tabela PESSOA_JURIDICA que quero adicionar OPORTUNIDADES. O formulário é acionado através de uma listagem de pessoas jurídicas. Quando este é acionado
traz o id e o nome fantasia da pessoa jurídica. Possuo então 3 campos da tabela OPORTUNIDADE que deverão ser adicionados.
Quando clico em Register (pra colocar os dados informados da oportunidade no grid), nada acontece.
Segue abaixo o código que estou usando. Mais uma vez agradeço a ajuda de todos.
tentei criar um formulário master/detail no theme 4 e não tive muito sucesso. Tentei então pegar um formulário pronto "SystemGroupForm" e padroniza-lo para utilizar em outros que eu precisar.
No entanto, estou com problemas simples aqui que não consigo resolver.
Nesse exemplo tenho uma tabela PESSOA_JURIDICA que quero adicionar OPORTUNIDADES. O formulário é acionado através de uma listagem de pessoas jurídicas. Quando este é acionado
traz o id e o nome fantasia da pessoa jurídica. Possuo então 3 campos da tabela OPORTUNIDADE que deverão ser adicionados.
Quando clico em Register (pra colocar os dados informados da oportunidade no grid), nada acontece.
Segue abaixo o código que estou usando. Mais uma vez agradeço a ajuda de todos.
- <?php
- class cotacao_pj_form_v5a extends TPage
- {
- protected $form; // form
- protected $detail_list;
- /**
- * Class constructor
- * Creates the page and the registration form
- */
- public function __construct()
- {
- parent::__construct();
- // creates the form
- $this->form = new BootstrapFormBuilder('cotacao_pj_form_v5a');
- $this->form->setFormTitle('Cotações');
- // create the form fields
- $id = new TEntry('id');
- $nm_fantasia = new TEntry('nm_fantasia');
- $detail_id = new TEntry('detail_id');
- $detail_nr_op_crm = new TEntry('detail_nr_op_crm');
- $detail_ds_op = new TEntry('detail_ds_op');
- // define the sizes
- //$label_id->setSize('40%');
- //$detail_id->setSize('40%');
- //$label_nr_op_crm->setSize('40%');
- //$detail_nr_op_crm->setSize('40%');
- //$label_ds_op->setSize('40%');
- //$detail_ds_op->setSize('70%');
- // validations
- //$name->addValidation('name', new TRequiredValidator);
- // outras propriedades
- $id->setEditable(false);
- $this->form->addFields( [new TLabel('ID')], [$id]);
- $this->form->addFields( [new TLabel('Cliente ')], [$nm_fantasia]);
- $this->detail_list = new TQuickGrid();
- $this->detail_list->setHeight(200);
- $this->detail_list->makeScrollable();
- $this->detail_list->style='width: 100%';
- $this->detail_list->id = 'detail_list';
- $this->detail_list->disableDefaultClick();
- $this->detail_list->addQuickColumn('', 'delete', 'center', '5%');
- $this->detail_list->addQuickColumn('Id', 'id', 'left', '10%');
- $this->detail_list->addQuickColumn('OP CRM', 'nr_op_crm', 'left', '10%');
- $this->detail_list->addQuickColumn('Desc. OP', 'ds_op', 'left', '85%');
- $this->detail_list->createModel();
- $add_button = TButton::create('add', array($this,'onAddDetail'), _t('Add'), 'fa:plus green');
- $hbox = new THBox;
- //$hbox->add($label_id);
- //$hbox->add($detail_id);
- //$hbox->add($label_nr_op_crm, 'display:initial');
- //$hbox->add($detail_nr_op_crm, 'display:initial');
- ///$hbox->add($label_ds_op, 'display:initial');
- //$hbox->add($detail_ds_op, 'display:initial');
- $hbox->add($add_button);
- //$hbox->style = 'margin: 4px';
- $vbox = new TVBox;
- $vbox->style='width:100%';
- $vbox->add( $hbox );
- $vbox->add($this->detail_list);
- $this->form->addFields( [new TFormSeparator('Oportunidades')] );
- $this->form->addFields( [new TLabel('OP ID ')], [$detail_id]);
- $this->form->addFields( [new TLabel('NR.CRM ')], [$detail_nr_op_crm]);
- $this->form->addFields( [new TLabel('Descr. ')], [$detail_ds_op]);
- $this->form->addField($add_button);
- $this->form->addFields( [$vbox] );
- $btn = $this->form->addAction( _t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o' );
- $btn->class = 'btn btn-sm btn-primary';
- $this->form->addAction( _t('Clear'), new TAction(array($this, 'onEdit')), 'fa:eraser red' );
- $this->form->addAction( _t('Back'), new TAction(array('pessoa_juridicaListv1','onReload')), 'fa:arrow-circle-o-left blue' );
- //$this->form->addField($detail_id);
- //$this->form->addField($detail_nr_op_crm);
- //$this->form->addField($detail_ds_op);
- $container = new TVBox;
- $container->style = 'width:90%';
- //$container->add(new TXMLBreadCrumb('menu.xml', 'SystemGroupList'));
- $container->add($this->form);
- // add the form to the page
- parent::add($container);
- }
- /**
- * Remove program from session
- */
- public static function deleteDetail($param)
- {
- $details = TSession::getValue('detail_list');
- unset($details[ $param['id'] ]);
- TSession::setValue('detail_list', $details);
- }
- /**
- * method onSave()
- * Executed whenever the user clicks at the save button
- */
- public static function onSave($param)
- {
- try
- {
- // open a transaction with database 'permission'
- TTransaction::open('solicitacoes');
- // get the form data into an active record System_group
- $object = new pessoa_juridica;
- $object->fromArray( $param );
- $object->store();
- $object->clearParts();
- $details = TSession::getValue('detail_list');
- if (!empty($details))
- {
- foreach ($details as $detail)
- {
- $object->addoportunidade( new oportunidade( $detail['id'] ) );
- }
- }
- $data = new stdClass;
- $data->id = $object->id;
- TForm::sendData('cotacao_pj_form_v5a', $data);
- TTransaction::close(); // close the transaction
- new TMessage('info', _t('Record saved')); // shows the success message
- }
- catch (Exception $e) // in case of exception
- {
- // shows the exception error message
- new TMessage('error', $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * method onEdit()
- * Executed whenever the user clicks at the edit button da datagrid
- */
- function onEdit($param)
- {
- try
- {
- if (isset($param['key']))
- {
- // get the parameter $key
- $key=$param['key'];
- // open a transaction with database 'permission'
- TTransaction::open('solicitacoes');
- // instantiates object System_group
- $object = new pessoa_juridica($key);
- $data = array();
- foreach ($object->getOportunidades() as $program)
- {
- $data[$program->id] = $program->toArray();
- $item = new stdClass;
- $item->id = $program->id;
- $item->name = $program->name;
- $i = new TElement('i');
- $i->{'class'} = 'fa fa-trash red';
- $btn = new TElement('a');
- $btn->{'onclick'} = "__adianti_ajax_exec('class=SystemGroupForm&method=deleteProgram&id={$program->id}');$(this).closest('tr').remove();";
- $btn->{'class'} = 'btn btn-default btn-sm';
- $btn->add( $i );
- $item->delete = $btn;
- $tr = $this->detail_list->addItem($item);
- $tr->{'style'} = 'width: 100%;display: inline-table;';
- }
- // fill the form with the active record data
- $this->form->setData($object);
- // close the transaction
- TTransaction::close();
- TSession::setValue('detail_list', $data);
- }
- else
- {
- $this->form->clear();
- TSession::setValue('detail_list', null);
- }
- }
- catch (Exception $e) // in case of exception
- {
- // shows the exception error message
- new TMessage('error', $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * Load an item from session list to detail form
- * @param $param URL parameters
- */
- public function onEditDetail( $param )
- {
- $data = $this->form->getData();
- // read session items
- $items = TSession::getValue(__CLASS__.'_items');
- // get the session item
- $item = $items[ $param['item_key'] ];
- $data->detail_id = $item['id'];
- $data->detail_nr_op_crm = $item['nr_op_crm'];
- $data->detail_ds_op = $item['ds_op'];
- // fill detail fields
- $this->form->setData( $data );
- $this->onReload( $param );
- }
- /**
- * Delete an item from session list
- * @param $param URL parameters
- */
- public function onDeleteDetail( $param )
- {
- $data = $this->form->getData();
- // reset items
- $data->detail_nr_op_crm = '';
- $data->detail_ds_op = '';
- // clear form data
- $this->form->setData( $data );
- // read session items
- $items = TSession::getValue(__CLASS__.'_items');
- // delete the item from session
- unset($items[ $param['item_key'] ] );
- TSession::setValue(__CLASS__.'_items', $items);
- // reload items
- $this->onReload( $param );
- }
- /**
- * Add a program
- */
- public static function onAddDetail($param)
- {
- try
- {
- $id = $param['detail_id'];
- $detail_list = TSession::getValue('detail_list');
- if (!empty($id) AND empty($program_list[$id]))
- {
- //TTransaction::open('solicitacoes');
- //$program = SystemProgram::find($id);
- //$program_list[$id] = $program->toArray();
- //TSession::setValue('program_list', $program_list);
- //TTransaction::close();
- $i = new TElement('i');
- $i->{'class'} = 'fa fa-trash red';
- $btn = new TElement('a');
- $btn->{'onclick'} = "__adianti_ajax_exec(\'class=cotacao_pj_form_v5a&method=deleteProgram&id=$id\');$(this).closest(\'tr\').remove();";
- $btn->{'class'} = 'btn btn-default btn-sm';
- $btn->add($i);
- $tr = new TTableRow;
- $tr->{'class'} = 'tdatagrid_row_odd';
- $tr->{'style'} = 'width: 100%;display: inline-table;';
- $cell = $tr->addCell( $btn );
- $cell->{'style'}='text-align:center';
- $cell->{'class'}='tdatagrid_cell';
- $cell->{'width'} = '5%';
- $cell = $tr->addCell( $param['detail_id'] );
- $cell->{'class'}='tdatagrid_cell';
- $cell->{'width'} = '10%';
- $cell = $tr->addCell( $param['detail_id'] );
- $cell->{'class'}='tdatagrid_cell';
- $cell->{'width'} = '10%';
- $cell = $tr->addCell( $param['detail_ds_op'] );
- $cell->{'class'}='tdatagrid_cell';
- $cell->{'width'} = '85%';
- TScript::create("tdatagrid_add_serialized_row('$detail_list', '$tr');");
- $data = new stdClass;
- $data->program_id = '';
- $data->program_name = '';
- TForm::sendData('cotacao_pj_form_v5a', $data);
- }
- }
- catch (Exception $e)
- {
- new TMessage('error', $e->getMessage());
- }
- }
- }
Função tdatagrid_add_serialized_row, linha 329:
O primeiro parâmetro é o id da grid('detail_list' no caso) e não a variável $detail_list
Obrigado Nataniel.... funcionou !!!