Lançado Adianti Framework 7.6!
Clique aqui para saber mais
Form não é preenchido no onEdit ao usar componente TDBSortList Olá, Estou tentando usar o componente TDBSortList na tela de usuário, porém ao editar informações não são preenchidas no form, e não estou conseguindo identificar onde estou errando, alguém pode me ajudar / Segue código ...
RB
Form não é preenchido no onEdit ao usar componente TDBSortList  
Olá,

Estou tentando usar o componente TDBSortList na tela de usuário, porém ao editar informações não são preenchidas no form, e não estou conseguindo identificar onde estou errando, alguém pode me ajudar /

Segue código

  1. <?php
  2. class SystemUserForm extends TPage
  3. {
  4.     protected $form// form
  5.     protected $program_list;
  6.     protected $datagrid;
  7.     protected $loaded;
  8.     /**
  9.      * Class constructor
  10.      * Creates the page and the registration form
  11.      */
  12.     function __construct()
  13.     {
  14.         parent::__construct();
  15.         parent::include_css('app/resources/builder.css');
  16.         parent::include_css('app/resources/blue_modificado.css');
  17.         parent::include_css('app/resources/custom-table.css');
  18.         
  19.         $organizacao_id TSession::getValue('organizacion_id'); // pega id da empresa
  20.         // creates the form
  21.         $this->form = new BootstrapFormBuilder('form_System_user');
  22.         $this->form->setFormTitle_t('User') );
  23.         
  24.         // create the form fields
  25.         $lbl_id                 = new TLabel('Id');
  26.         $lbl_name               = new TLabel(_t('Name'));
  27.         //$lbl_login              = new TLabel(_t('Login'));
  28.         $lbl_password           = new TLabel(_t('Password'));
  29.         $lbl_confirm_password   = new TLabel(_t('Password confirmation'));
  30.         $lbl_email              = new TLabel(_t('Email'));
  31.         $lbl_unit               = new TLabel(_t('Main unit'));
  32.         $lbl_front_page         = new TLabel(_t('Front page'));
  33.         $lbl_id->class 'lbl_text';             
  34.         $lbl_name->class 'lbl_text';            
  35.         //$lbl_login->class = 'lbl_text';          
  36.         $lbl_password->class 'lbl_text';       
  37.         $lbl_confirm_password->class 'lbl_text';
  38.         $lbl_email->class 'lbl_text';       
  39.         $lbl_unit->class 'lbl_text';          
  40.         $lbl_front_page->class 'lbl_text';       
  41.         $id                  = new TEntry('id');
  42.         $name                = new TEntry('name');
  43.         //$login               = new TEntry('login');
  44.         $password            = new TPassword('password');
  45.         $repassword          = new TPassword('repassword');
  46.         $email               = new TEntry('email');
  47.         $filter1 = new TCriteria;
  48.         $filter1->add(new TFilter('organizacao_id','=',$organizacao_id));
  49.         $unit_id    = new TDBCombo('system_unit_id','permission','SystemOrganization','id','name');
  50.         $program_id   = new  ">TDBSeekButton('program_id''permission''form_System_user''SystemProgram''name''program_id''program_name');
  51.         $program_name = new TEntry('program_name');
  52.         
  53.         //alterado para grids
  54.         //$groups       = new TDBCheckGroup('groups','permission','SystemGroup','id','name');
  55.         
  56.         $frontpage_id = new  ">TDBSeekButton('frontpage_id''permission''form_System_user''SystemProgram''name''frontpage_id''frontpage_name');
  57.         $frontpage_name = new TEntry('frontpage_name');
  58.         $filter = new TCriteria;
  59.         $filter->add(new TFilter('organizacao_id','<','0'));
  60.         
  61.         $organizacao = new TDBCheckGroup('organizacao','centinel','Organizacao','id','juridica_nome','id',$filter);
  62.         $units       = new TDBCheckGroup('units','permission','SystemUnit','id','name');        
  63.          $organizacao->setLayout('horizontal');
  64.         if ($organizacao->getLabels())
  65.         {
  66.             foreach ($units->getLabels() as $label)
  67.             {
  68.                 $label->setSize(200);
  69.             }
  70.         }
  71.         
  72.         $units->setLayout('horizontal');
  73.         if ($units->getLabels())
  74.         {
  75.             foreach ($units->getLabels() as $label)
  76.             {
  77.                 $label->setSize(200);
  78.             }
  79.         }
  80.         
  81.         $btn $this->form->addAction_t('Save'), new TAction(array($this'onSave')), 'fa:floppy-o');
  82.         $btn->class 'btn btn-sm btn-primary';
  83.         $this->form->addAction_t('Clear'), new TAction(array($this'onEdit')), 'fa:eraser red');
  84.         $this->form->addAction_t('Back'), new TAction(array('SystemUserList','onReload')), 'fa:arrow-circle-o-left blue');
  85.         
  86.         $add_button  TButton::create('add',  array($this,'onAddProgram'), _t('Add'), 'fa:plus green');
  87.         
  88.         $this->form->addField($program_id);
  89.         $this->form->addField($program_name);
  90.         $this->form->addField($add_button);
  91.         
  92.         $this->program_list = new TQuickGrid;
  93.         $this->program_list->setHeight(180);
  94.         $this->program_list->makeScrollable();
  95.         $this->program_list->style='width: 100%';
  96.         $this->program_list->id 'program_list';
  97.         $this->program_list->disableDefaultClick();
  98.         $this->program_list->addQuickColumn('''delete''center''5%');
  99.         $this->program_list->addQuickColumn('Id''id''left''10%');
  100.         $this->program_list->addQuickColumn(_t('Program'), 'name''left''85%');
  101.         $this->program_list->createModel();
  102.         
  103.         $hbox = new THBox;
  104.         $hbox->add($program_id);
  105.         $hbox->add($program_name'display:initial');
  106.         $hbox->add($add_button);
  107.         $hbox->style 'margin: 4px';
  108.         $vbox = new TVBox;
  109.         $vbox->style='width:100%';
  110.         $vbox->add$hbox );
  111.         $vbox->add($this->program_list);
  112.         // define the sizes
  113.         //$id->setSize('50%');
  114.         $name->setSize('100%');
  115.         $password->setSize('100%');
  116.         $repassword->setSize('100%');
  117.         $email->setSize('100%');
  118.         $unit_id->setSize('100%');
  119.         $frontpage_id->setSize('60');
  120.         $frontpage_name->setSize('calc(100% - 60px)');
  121.         $program_id->setSize('30');
  122.         $program_name->setSize('calc(100% - 200px)');
  123.         
  124.         // outros
  125.         $id->setEditable(false);
  126.         $program_name->setEditable(false);
  127.         $frontpage_name->setEditable(false);
  128.         
  129.         // validations
  130.         $name->addValidation(_t('Name'), new TRequiredValidator);
  131.         $email->addValidation('Email', new TEmailValidator);
  132.         
  133.         $this->form->addFields( [$lbl_id], [$id]);
  134.         $this->form->addFields( [$lbl_name], [$name] );
  135.         $this->form->addFields( [$lbl_email], [$email] );
  136.         $this->form->addFields( [$lbl_unit], [$unit_id],  [$lbl_front_page], [$frontpage_id$frontpage_name] );
  137.         $this->form->addFields( [$lbl_password], [$password],  [$lbl_confirm_password], [$repassword] );
  138.         $this->form->addFields( [new TFormSeparator('')]);
  139.      
  140.         $dados_unit        = new TLabel(_t('Units'));
  141.         $dados_unit->class ='lbl_titulo';
  142.         $this->form->addContent( [$dados_unit] );
  143.         $this->form->addFields( [$units] );        
  144.         $this->form->addFields( [new TFormSeparator('')] );
  145.         $dados_grupo       = new TLabel(_t('Groups'));
  146.         $dados_grupo->class ='lbl_titulo';
  147.         $this->form->addContent( [$dados_grupo] );
  148.         //lista de grupos
  149.         $list1 = new TDBSortList('list1''permission''SystemGroup''id''name','id asc'  );
  150.         $list2 = new TSortList('list2');
  151.         $list1->style 'float:left;width:250px;height:250px';
  152.         $list2->style 'float:left;width:250px;height:250px';
  153.         $list1->connectTo($list2);
  154.         $list2->connectTo($list1);
  155.         // connect the change method
  156.         $list1->setChangeAction(new TAction(array($this'onChangeActionList')));
  157.         
  158.         // creates the action button
  159.         $button1 = new TButton('action1');
  160.         $button1->setAction(new TAction(array($this'onSend')), '');
  161.         $button1->setImage('fa:check-circle-o');
  162.         // creates the action button
  163.         $button2 = new TButton('action2');
  164.         $button2->setAction(new TAction(array($this'onSend')), '');
  165.         $button2->setImage('fa:check-circle-o');
  166.         // creates the action button
  167.         $button3 = new TButton('action3');
  168.         $button3->setAction(new TAction(array($this'onSend')), '');
  169.         $button3->setImage('fa:check-circle-o');
  170.         // creates the action button
  171.         $button4 = new TButton('action4');
  172.         $button4->setAction(new TAction(array($this'onSend')), 'Send');
  173.         $button4->setImage('fa:check-circle-o');
  174.         
  175.         $table1 = new TTable;
  176.         $table2 = new TTable;
  177.         $row $table1->addRow();
  178.         $row->addCell($list1);
  179.         $table2->addRowSet($button1);
  180.         $table2->addRowSet($button2);
  181.         $table2->addRowSet($button3);
  182.         $row->addCell($table2);
  183.         
  184.         $row->addCell($list2);
  185.         $table1->addRow()->addCell($button4);
  186.         $this->form->setFields(array($list1,$list2$button1,$button2,$button3,$button4));
  187.         
  188.         $this->form->addFields([$table1]);
  189.         $dados_programs       = new TLabel(_t('Programs'));
  190.         $dados_programs->class ='lbl_titulo';
  191.         $this->form->addContent( [$dados_programs] );
  192.         $this->form->addFields( [$vbox] );
  193.         
  194.         $container = new TVBox;
  195.         $container->style 'width: 90%';
  196.         $container->add(new TXMLBreadCrumb('menu.xml''SystemUserList'));
  197.         $container->add($this->form);
  198.         // add the container to the page
  199.         parent::add($container);
  200.     }
  201.     /**
  202.      * Remove program from session
  203.      */
  204.     public static function deleteProgram($param)
  205.     {
  206.         $programs TSession::getValue('program_list');
  207.         unset($programs$param['id'] ]);
  208.         TSession::setValue('program_list'$programs);
  209.     }
  210.     
  211.     /**
  212.      * method onSave()
  213.      * Executed whenever the user clicks at the save button
  214.      */
  215.     public static function onSave($param)
  216.     {
  217.         try
  218.         {
  219.             // open a transaction with database 'permission'
  220.             TTransaction::open('permission');
  221.             
  222.             $object = new SystemUser;
  223.             $object->fromArray$param );
  224.             
  225.             $senha $object->password;
  226.             
  227.             /*
  228.             if( empty($object->login) )
  229.             {
  230.                 throw new Exception(TAdiantiCoreTranslator::translate('The field ^1 is required', _t('Login')));
  231.             }
  232.             */
  233.             
  234.             if( empty($object->id) )
  235.             {
  236.                 if (SystemUser::newFromLogin($object->login) instanceof SystemUser)
  237.                 {
  238.                     throw new Exception(_t('An user with this login is already registered'));
  239.                 }
  240.                 
  241.                 if ( empty($object->password) )
  242.                 {
  243.                     throw new Exception(TAdiantiCoreTranslator::translate('The field ^1 is required'_t('Password')));
  244.                 }
  245.                 
  246.                 $object->active 'Y';
  247.             }
  248.             
  249.             if( $object->password )
  250.             {
  251.                 if( $object->password !== $param['repassword'] )
  252.                     throw new Exception(_t('The passwords do not match'));
  253.                 
  254.                 $object->password md5($object->password);
  255.             }
  256.             else
  257.             {
  258.                 unset($object->password);
  259.             }
  260.             
  261.             $object->store();
  262.             $object->clearParts();
  263.             
  264.             if( !empty($param['groups']) )
  265.             {
  266.                 foreach( $param['groups'] as $group_id )
  267.                 {
  268.                     $object->addSystemUserGroup( new SystemGroup($group_id) );
  269.                 }
  270.             }
  271.             
  272.             if( !empty($param['units']) )
  273.             {
  274.                 foreach( $param['units'] as $unit_id )
  275.                 {
  276.                     $object->addSystemUserUnit( new SystemUnit($unit_id) );
  277.                 }
  278.             }
  279.             
  280.             $programs TSession::getValue('program_list');
  281.             if (!empty($programs))
  282.             {
  283.                 foreach ($programs as $program)
  284.                 {
  285.                     $object->addSystemUserProgram( new SystemProgram$program['id'] ) );
  286.                 }
  287.             }
  288.             
  289.             $data = new stdClass;
  290.             $data->id $object->id;
  291.             TForm::sendData('form_System_user'$data);
  292.             
  293.             // close the transaction
  294.             TTransaction::close();
  295.             
  296.             // shows the success message
  297.             new TMessage('info'TAdiantiCoreTranslator::translate('Record saved'));
  298.         }
  299.         catch (Exception $e// in case of exception
  300.         {
  301.             // shows the exception error message
  302.             new TMessage('error'$e->getMessage());
  303.             
  304.             // undo all pending operations
  305.             TTransaction::rollback();
  306.         }
  307.     }
  308.     
  309.     /**
  310.      * method onEdit()
  311.      * Executed whenever the user clicks at the edit button da datagrid
  312.      */
  313.     function onEdit($param)
  314.     {
  315.         try
  316.         {
  317.             if (isset($param['key']))
  318.             {
  319.                 // get the parameter $key
  320.                 $key=$param['key'];
  321.                 
  322.                 // open a transaction with database 'permission'
  323.                 TTransaction::open('permission');
  324.                 
  325.                 // instantiates object System_user
  326.                 $object = new SystemUser($key);
  327.                 
  328.                 unset($object->password);
  329.                 $units  = array();                
  330.                 
  331.                 if( $units_db $object->getSystemUserUnits() )
  332.                 {
  333.                     foreach( $units_db as $unit )
  334.                     {
  335.                         $units[] = $unit->id;
  336.                     }
  337.                 }
  338.                 
  339.                 $data = array();
  340.                 foreach ($object->getSystemUserPrograms() as $program)
  341.                 {
  342.                     $data[$program->id] = $program->toArray();
  343.                     
  344.                     $item = new stdClass;
  345.                     $item->id $program->id;
  346.                     $item->name $program->name;
  347.                     
  348.                     $i = new TElement('i');
  349.                     $i->{'class'} = 'fa fa-trash red';
  350.                     $btn = new TElement('a');
  351.                     $btn->{'onclick'} = "__adianti_ajax_exec('class=SystemUserForm&method=deleteProgram&id={$program->id}');$(this).closest('tr').remove();";
  352.                     $btn->{'class'} = 'btn btn-default btn-sm';
  353.                     $btn->add$i );
  354.                     
  355.                     $item->delete $btn;
  356.                     $tr $this->program_list->addItem($item);
  357.                     $tr->{'style'} = 'width: 100%;display: inline-table;';
  358.                 }
  359.                 
  360.                 $object->units  $units;
  361.                 
  362.                 // fill the form with the active record data
  363.                 $this->form->setData($object);
  364.                 
  365.                 // close the transaction
  366.                 TTransaction::close();
  367.                 
  368.                 TSession::setValue('program_list'$data);
  369.             }
  370.             else
  371.             {
  372.                 $this->form->clear();
  373.                 TSession::setValue('program_list'null);
  374.             }
  375.         }
  376.         catch (Exception $e// in case of exception
  377.         {
  378.             // shows the exception error message
  379.             new TMessage('error'$e->getMessage());
  380.             
  381.             // undo all pending operations
  382.             TTransaction::rollback();
  383.         }
  384.     }
  385.     
  386.     /**
  387.      * Add a program
  388.      */
  389.     public static function onAddProgram($param)
  390.     {
  391.         try
  392.         {
  393.             $id $param['program_id'];
  394.             $program_list TSession::getValue('program_list');
  395.             
  396.             if (!empty($id) AND empty($program_list[$id]))
  397.             {
  398.                 TTransaction::open('permission');
  399.                 $program SystemProgram::find($id);
  400.                 $program_list[$id] = $program->toArray();
  401.                 TSession::setValue('program_list'$program_list);
  402.                 TTransaction::close();
  403.                 
  404.                 $i = new TElement('i');
  405.                 $i->{'class'} = 'fa fa-trash red';
  406.                 $btn = new TElement('a');
  407.                 $btn->{'onclick'} = "__adianti_ajax_exec(\'class=SystemGroupForm&method=deleteProgram&id=$id\');$(this).closest(\'tr\').remove();";
  408.                 $btn->{'class'} = 'btn btn-default btn-sm';
  409.                 $btn->add($i);
  410.                 
  411.                 $tr = new TTableRow;
  412.                 $tr->{'class'} = 'tdatagrid_row_odd';
  413.                 $tr->{'style'} = 'width: 100%;display: inline-table;';
  414.                 $cell $tr->addCell$btn );
  415.                 $cell->{'style'}='text-align:center';
  416.                 $cell->{'class'}='tdatagrid_cell';
  417.                 $cell->{'width'} = '5%';
  418.                 $cell $tr->addCell$program->id );
  419.                 $cell->{'class'}='tdatagrid_cell';
  420.                 $cell->{'width'} = '10%';
  421.                 $cell $tr->addCell$program->name );
  422.                 $cell->{'class'}='tdatagrid_cell';
  423.                 $cell->{'width'} = '85%';
  424.                 
  425.                 TScript::create("tdatagrid_add_serialized_row('program_list', '$tr');");
  426.                 
  427.                 $data = new stdClass;
  428.                 $data->program_id '';
  429.                 $data->program_name '';
  430.                 TForm::sendData('form_System_user'$data);
  431.             }
  432.         }
  433.         catch (Exception $e)
  434.         {
  435.             new TMessage('error'$e->getMessage());
  436.         }
  437.     }
  438.     static function onChangeAction($param)
  439.     {
  440.         try
  441.         {
  442.             TTransaction::open('centinel'); //base de dados
  443.             if ($organizacao_id)//se existe parâmetro da primeira combo(nome da primeira combo)
  444.              {
  445.                $criteria TCriteria::create( ['organizacao_id' => $organizacao_id ] );  
  446.                 //organizacao_id = parametro chave estrangeira da tabela master       
  447.                 // formname,field(nome do campo),database , nome da model,key,value,ordercolumn = NULL,criteria, startEmpty = FALSE
  448.                 TDBCombo::reloadFromModel('form_System_user','organizacao_id','centinel','Organizacao','id','{juridica_nome}''juridica_nome',$criteria,TRUE);
  449.             }
  450.             else
  451.             {
  452.               TCombo::clearField('form_System_user''organizacao'); //reload do usuário form_System_user na check "organizacao_id"
  453.             }
  454.             TTransaction::close();
  455.         }
  456.         catch (Exception $e)
  457.         {
  458.             new TMessage('error'$e->getMessage());
  459.         }  
  460.     }
  461.     /**
  462.      * Executed when a user change some item in list
  463.      */
  464.     public static function onChangeActionList($param)
  465.     {
  466.         new TMessage('info''Change action<br>'.
  467.                              'List1: ' implode(','$param['list1']) . '<br>' .
  468.                              'List2: ' implode(','$param['list2']));
  469.     }
  470.     
  471.     /**
  472.      * Send data
  473.      */
  474.     public function onSend($param)
  475.     {
  476.         // get form data
  477.         $data $this->form->getData();
  478.         
  479.         // put the data back to the form
  480.         $this->form->setData($data);
  481.         
  482.         // creates a string with the form element's values
  483.         $message 'List 1: '  implode(','$data->list1) . '<br>';
  484.         $message.= 'List 2 : ' implode(','$data->list2) . '<br>';
  485.         
  486.         // show the message
  487.         new TMessage('info'$message);
  488.     }
  489. }

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

Ao usar a função setFields somente os campos informados serão considerados pela classe, demais campos serão ignorados.
  1. <?php
  2. //$this->form->setFields(array($list1,$list2, $button1,$button2,$button3,$button4));
  3. $this->form->setFields(array_merge($this->form->getFields(),array($list1,$list2$button1,$button2,$button3,$button4)));
  4. ?>
RB

Nataniel,

Funcionou perfeito, valeu ....

Muito obrigado.