Lançado Adianti Framework 7.6!
Clique aqui para saber mais
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...
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.



  1. <?php
  2. class cotacao_pj_form_v5a extends TPage
  3. {
  4.     protected $form// form
  5.     protected $detail_list;
  6.     
  7.     /**
  8.      * Class constructor
  9.      * Creates the page and the registration form
  10.      */
  11.     public function __construct()
  12.     {
  13.         parent::__construct();
  14.         
  15.         // creates the form
  16.         $this->form = new BootstrapFormBuilder('cotacao_pj_form_v5a');
  17.         $this->form->setFormTitle('Cotações');
  18.         // create the form fields
  19.         $id = new TEntry('id');
  20.         $nm_fantasia = new TEntry('nm_fantasia');
  21.     
  22.         $detail_id = new TEntry('detail_id');
  23.         $detail_nr_op_crm = new TEntry('detail_nr_op_crm');
  24.         $detail_ds_op = new TEntry('detail_ds_op');
  25.         // define the sizes
  26.         //$label_id->setSize('40%');
  27.         //$detail_id->setSize('40%');
  28.         //$label_nr_op_crm->setSize('40%');
  29.         //$detail_nr_op_crm->setSize('40%');
  30.         //$label_ds_op->setSize('40%');
  31.         //$detail_ds_op->setSize('70%');
  32.         
  33.         // validations
  34.         //$name->addValidation('name', new TRequiredValidator);
  35.         
  36.         // outras propriedades
  37.         $id->setEditable(false);
  38.         
  39.         $this->form->addFields( [new TLabel('ID')], [$id]);
  40.         $this->form->addFields( [new TLabel('Cliente ')], [$nm_fantasia]);
  41.         
  42.         $this->detail_list = new TQuickGrid();
  43.         $this->detail_list->setHeight(200);
  44.         $this->detail_list->makeScrollable();
  45.         $this->detail_list->style='width: 100%';
  46.         $this->detail_list->id 'detail_list';
  47.         $this->detail_list->disableDefaultClick();
  48.         $this->detail_list->addQuickColumn('''delete''center''5%');
  49.         $this->detail_list->addQuickColumn('Id''id''left''10%');
  50.         $this->detail_list->addQuickColumn('OP CRM''nr_op_crm''left''10%');
  51.         $this->detail_list->addQuickColumn('Desc. OP''ds_op''left''85%');
  52.         $this->detail_list->createModel();
  53.         
  54.         $add_button  TButton::create('add',  array($this,'onAddDetail'), _t('Add'), 'fa:plus green');
  55.         
  56.         $hbox = new THBox;
  57.         //$hbox->add($label_id);
  58.         //$hbox->add($detail_id);
  59.         //$hbox->add($label_nr_op_crm, 'display:initial');
  60.         //$hbox->add($detail_nr_op_crm, 'display:initial');
  61.         ///$hbox->add($label_ds_op, 'display:initial');
  62.         //$hbox->add($detail_ds_op, 'display:initial');
  63.         $hbox->add($add_button);
  64.         //$hbox->style = 'margin: 4px';
  65.         
  66.         $vbox = new TVBox;
  67.         $vbox->style='width:100%';
  68.         $vbox->add$hbox );
  69.         $vbox->add($this->detail_list);
  70.         
  71.         $this->form->addFields( [new TFormSeparator('Oportunidades')] );
  72.         $this->form->addFields( [new TLabel('OP ID ')], [$detail_id]);
  73.         $this->form->addFields( [new TLabel('NR.CRM ')], [$detail_nr_op_crm]);
  74.         $this->form->addFields( [new TLabel('Descr. ')], [$detail_ds_op]);
  75.         $this->form->addField($add_button);
  76.         $this->form->addFields( [$vbox] );
  77.         
  78.         $btn $this->form->addAction_t('Save'), new TAction(array($this'onSave')), 'fa:floppy-o' );
  79.         $btn->class 'btn btn-sm btn-primary';
  80.         
  81.         $this->form->addAction_t('Clear'), new TAction(array($this'onEdit')),  'fa:eraser red' );
  82.         $this->form->addAction_t('Back'), new TAction(array('pessoa_juridicaListv1','onReload')),  'fa:arrow-circle-o-left blue' );
  83.         
  84.         //$this->form->addField($detail_id);
  85.         //$this->form->addField($detail_nr_op_crm);
  86.         //$this->form->addField($detail_ds_op);        
  87.         
  88.         $container = new TVBox;
  89.         $container->style 'width:90%';
  90.         //$container->add(new TXMLBreadCrumb('menu.xml', 'SystemGroupList'));
  91.         $container->add($this->form);
  92.         
  93.         // add the form to the page
  94.         parent::add($container);
  95.     }
  96.     
  97.     
  98.     /**
  99.      * Remove program from session
  100.      */
  101.     public static function deleteDetail($param)
  102.     {
  103.         $details TSession::getValue('detail_list');
  104.         unset($details$param['id'] ]);
  105.         TSession::setValue('detail_list'$details);
  106.     }
  107.     
  108.     /**
  109.      * method onSave()
  110.      * Executed whenever the user clicks at the save button
  111.      */
  112.     public static function onSave($param)
  113.     {
  114.         try
  115.         {
  116.             // open a transaction with database 'permission'
  117.             TTransaction::open('solicitacoes');
  118.             
  119.             // get the form data into an active record System_group
  120.             $object = new pessoa_juridica;
  121.             $object->fromArray$param );
  122.             $object->store();
  123.             $object->clearParts();
  124.             
  125.             $details TSession::getValue('detail_list');
  126.             if (!empty($details))
  127.             {
  128.                 foreach ($details as $detail)
  129.                 {
  130.                     $object->addoportunidade( new oportunidade$detail['id'] ) );
  131.                 }
  132.             }
  133.             
  134.             $data = new stdClass;
  135.             $data->id $object->id;
  136.             TForm::sendData('cotacao_pj_form_v5a'$data);
  137.             
  138.             TTransaction::close(); // close the transaction
  139.             new TMessage('info'_t('Record saved')); // shows the success message
  140.         }
  141.         catch (Exception $e// in case of exception
  142.         {
  143.             // shows the exception error message
  144.             new TMessage('error'$e->getMessage());
  145.             
  146.             // undo all pending operations
  147.             TTransaction::rollback();
  148.         }
  149.     }
  150.     
  151.     /**
  152.      * method onEdit()
  153.      * Executed whenever the user clicks at the edit button da datagrid
  154.      */
  155.     function onEdit($param)
  156.     {
  157.         try
  158.         {
  159.             if (isset($param['key']))
  160.             {
  161.                 // get the parameter $key
  162.                 $key=$param['key'];
  163.                 
  164.                 // open a transaction with database 'permission'
  165.                 TTransaction::open('solicitacoes');
  166.                 
  167.                 // instantiates object System_group
  168.                 $object = new pessoa_juridica($key);
  169.                 
  170.                 $data = array();
  171.                 foreach ($object->getOportunidades() as $program)
  172.                 {
  173.                     $data[$program->id] = $program->toArray();
  174.                     
  175.                     $item = new stdClass;
  176.                     $item->id $program->id;
  177.                     $item->name $program->name;
  178.                     
  179.                     $i = new TElement('i');
  180.                     $i->{'class'} = 'fa fa-trash red';
  181.                     $btn = new TElement('a');
  182.                     $btn->{'onclick'} = "__adianti_ajax_exec('class=SystemGroupForm&method=deleteProgram&id={$program->id}');$(this).closest('tr').remove();";
  183.                     $btn->{'class'} = 'btn btn-default btn-sm';
  184.                     $btn->add$i );
  185.                     
  186.                     $item->delete $btn;
  187.                     $tr $this->detail_list->addItem($item);
  188.                     $tr->{'style'} = 'width: 100%;display: inline-table;';
  189.                 }
  190.                 
  191.                 // fill the form with the active record data
  192.                 $this->form->setData($object);
  193.                 
  194.                 // close the transaction
  195.                 TTransaction::close();
  196.                 
  197.                 TSession::setValue('detail_list'$data);
  198.             }
  199.             else
  200.             {
  201.                 $this->form->clear();
  202.                 TSession::setValue('detail_list'null);
  203.             }
  204.         }
  205.         catch (Exception $e// in case of exception
  206.         {
  207.             // shows the exception error message
  208.             new TMessage('error'$e->getMessage());
  209.             
  210.             // undo all pending operations
  211.             TTransaction::rollback();
  212.         }
  213.     }
  214.     /**
  215.      * Load an item from session list to detail form
  216.      * @param $param URL parameters
  217.      */
  218.     public function onEditDetail$param )
  219.     {
  220.         $data $this->form->getData();
  221.         
  222.         // read session items
  223.         $items TSession::getValue(__CLASS__.'_items');
  224.         
  225.         // get the session item
  226.         $item $items$param['item_key'] ];
  227.         
  228.         $data->detail_id $item['id'];
  229.         $data->detail_nr_op_crm $item['nr_op_crm'];
  230.         $data->detail_ds_op $item['ds_op'];
  231.         
  232.         // fill detail fields
  233.         $this->form->setData$data );
  234.     
  235.         $this->onReload$param );
  236.     }
  237.     
  238.     /**
  239.      * Delete an item from session list
  240.      * @param $param URL parameters
  241.      */
  242.     public function onDeleteDetail$param )
  243.     {
  244.         $data $this->form->getData();
  245.         
  246.         // reset items
  247.             $data->detail_nr_op_crm '';
  248.             $data->detail_ds_op '';
  249.         
  250.         // clear form data
  251.         $this->form->setData$data );
  252.         
  253.         // read session items
  254.         $items TSession::getValue(__CLASS__.'_items');
  255.         
  256.         // delete the item from session
  257.         unset($items$param['item_key'] ] );
  258.         TSession::setValue(__CLASS__.'_items'$items);
  259.         
  260.         // reload items
  261.         $this->onReload$param );
  262.     }
  263.     
  264.     /**
  265.      * Add a program
  266.      */
  267.     public static function onAddDetail($param)
  268.     {
  269.         try
  270.         {
  271.             $id $param['detail_id'];
  272.             $detail_list TSession::getValue('detail_list');
  273.             
  274.             if (!empty($id) AND empty($program_list[$id]))
  275.             {
  276.                 //TTransaction::open('solicitacoes');
  277.                 //$program = SystemProgram::find($id);
  278.                 //$program_list[$id] = $program->toArray();
  279.                 //TSession::setValue('program_list', $program_list);
  280.                 //TTransaction::close();
  281.                 
  282.                 $i = new TElement('i');
  283.                 $i->{'class'} = 'fa fa-trash red';
  284.                 $btn = new TElement('a');
  285.                 $btn->{'onclick'} = "__adianti_ajax_exec(\'class=cotacao_pj_form_v5a&method=deleteProgram&id=$id\');$(this).closest(\'tr\').remove();";
  286.                 $btn->{'class'} = 'btn btn-default btn-sm';
  287.                 $btn->add($i);
  288.                 
  289.                 $tr = new TTableRow;
  290.                 $tr->{'class'} = 'tdatagrid_row_odd';
  291.                 $tr->{'style'} = 'width: 100%;display: inline-table;';
  292.                 $cell $tr->addCell$btn );
  293.                 $cell->{'style'}='text-align:center';
  294.                 $cell->{'class'}='tdatagrid_cell';
  295.                 $cell->{'width'} = '5%';
  296.                 $cell $tr->addCell$param['detail_id'] );
  297.                 $cell->{'class'}='tdatagrid_cell';
  298.                 $cell->{'width'} = '10%';
  299.                 $cell $tr->addCell$param['detail_id'] );
  300.                 $cell->{'class'}='tdatagrid_cell';
  301.                 $cell->{'width'} = '10%';
  302.                                 
  303.                 $cell $tr->addCell$param['detail_ds_op'] );
  304.                 $cell->{'class'}='tdatagrid_cell';
  305.                 $cell->{'width'} = '85%';
  306.                 
  307.                 TScript::create("tdatagrid_add_serialized_row('$detail_list', '$tr');");
  308.                 
  309.                 $data = new stdClass;
  310.                 $data->program_id '';
  311.                 $data->program_name '';
  312.                 TForm::sendData('cotacao_pj_form_v5a'$data);
  313.             }
  314.         }
  315.         catch (Exception $e)
  316.         {
  317.             new TMessage('error'$e->getMessage());
  318.         }
  319.     }
  320. }

Pacotão Dominando o Adianti Framework 7
O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado! Versão 7.4


Dominando o Adianti 7 Quero me inscrever agora!

Comentários (2)


NR

Função tdatagrid_add_serialized_row, linha 329:
  1. <?php
  2. //TScript::create("tdatagrid_add_serialized_row('$detail_list', '$tr');");
  3. TScript::create("tdatagrid_add_serialized_row('detail_list', '$tr');");
  4. ?>

O primeiro parâmetro é o id da grid('detail_list' no caso) e não a variável $detail_list
GG

Obrigado Nataniel.... funcionou !!!