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

Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework 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 !!!