Lançado Adianti Framework 7.6!
Clique aqui para saber mais
Preencher Datagrid com método static Olá, estou usando o setExitAction para preencher alguns campos automaticamente, como o função precisa ser static não consigo mandar diretamente do meu onExitInsumos, então oque fiz foi enviar do metodo static os dados por TSession, só que quando tento chamar a instancia do meu metodo onReload que iria preencher a datagrid, ele simplesmente não popula, ja dei alguns var_dump e sim, o metodo ...
FH
Preencher Datagrid com método static  
Olá, estou usando o setExitAction para preencher alguns campos automaticamente, como o função precisa ser static não consigo mandar diretamente do meu onExitInsumos, então oque fiz foi enviar do metodo static os dados por TSession, só que quando tento chamar a instancia do meu metodo onReload que iria preencher a datagrid, ele simplesmente não popula, ja dei alguns var_dump e sim, o metodo é executado, os dados estão sendo passados, só que no additem ele não adiciona na datagrid, só que se eu colocar ele em um botão e chamar o onReload funciona...



aqui esta meu código:


  1. <?php
  2. /**
  3.  * SC2010Form Master/Detail
  4.  * @author  <your name here>
  5.  */
  6. class SC2010Form extends TPage
  7. {
  8.     protected $form// form
  9.     protected $detail_list;
  10.     protected $sumario;
  11.     
  12.     /**
  13.      * Page constructor
  14.      */
  15.     public function __construct()
  16.     {
  17.         parent::__construct();
  18.         
  19.         
  20.         // creates the form
  21.         $this->form = new BootstrapFormBuilder('form_SC2010');
  22.         $this->form->setFormTitle('SC2010');
  23.         
  24.         $criteria = new TCriteria;
  25.         $subquery "(SELECT C2_NUM
  26.                      FROM SC2010 SC
  27.                      JOIN SB1000 SB ON SC.C2_PRODUTO = SB.B1_COD
  28.                      JOIN SBM000 SM ON SB.B1_GRUPO = SM.BM_GRUPO AND SM.BM_TIPGRU = 'P'
  29.                      WHERE SC.C2_OPBROK = '' AND SC.D_E_L_E_T_ = '')";
  30.         $criteria->add(new TFilter('C2_NUM''IN'$subquery));
  31.         
  32.         $C2_NUM = new  ">TDBSeekButton('C2_NUM''banco''form_SC2010''SC2010''C2_PRODUTO''C2_NUM''PRODUTO'$criteria); 
  33.         $C2_QUANT = new TEntry('C2_QUANT');
  34.         $C2_QUANT->setEditable(FALSE);
  35.         $PRODUTO = new TEntry('PRODUTO');
  36.         $PRODUTO->setEditable(FALSE);
  37.         $REMESSA = new TDropDown ('REMESSA');
  38.         
  39.         $RECEITA = new TEntry ('RECEITA');
  40.         $RECEITA->setEditable(FALSE);
  41.         $INSUMO1 = new TEntry ('INSUMO1');
  42.         $INSUMO1->setEditable(FALSE);
  43.         $INSUMO2 = new TEntry ('INSUMO2');
  44.         $INSUMO2->setEditable(FALSE);   
  45.         $INSUMO3 = new TEntry ('INSUMO3');
  46.         $INSUMO3->setEditable(FALSE);
  47.         $INSUMO4 = new TEntry ('INSUMO4');
  48.         $INSUMO4->setEditable(FALSE);
  49.         $INSUMO5 = new TEntry ('INSUMO5');
  50.         $INSUMO5->setEditable(FALSE);
  51.         
  52.         $PORCEN1 = new TEntry ('PORCEN1');
  53.         $PORCEN1->setEditable(FALSE);
  54.         $PORCEN2 = new TEntry ('PORCEN2');
  55.         $PORCEN2->setEditable(FALSE);
  56.         $PORCEN3 = new TEntry ('PORCEN3');
  57.         $PORCEN3->setEditable(FALSE);
  58.         $PORCEN4 = new TEntry ('PORCEN4');
  59.         $PORCEN4->setEditable(FALSE);
  60.         $PORCEN5 = new TEntry ('PORCEN5');
  61.         $PORCEN5->setEditable(FALSE);
  62.      
  63.         $PRODUTO->setExitAction(new TAction(array($this'onExitProduct')));
  64.         $C2_NUM->setExitAction(new TAction(array($this'onExitOP')));
  65.         $RECEITA->setExitAction(new TAction(array($this'onExitInsumos')));
  66.         
  67.         // detail fields
  68.         $detail_uniqid = new THidden('detail_uniqid');
  69.         $detail_R_E_C_N_O_ = new THidden('detail_R_E_C_N_O_');
  70.         $detail_Z32_BARRA = new TEntry('detail_Z32_BARRA');
  71.         $detail_Z32_PESOLI = new TEntry('detail_Z32_PESOLI');
  72.         // master fields
  73.         $row $this->form->addFields( [new TLabel('Ordem Produção')], [$C2_NUM],[new TLabel('PA')], [$PRODUTO]);
  74.         $row->layout = ['col-sm-2 control-label''col-sm-2','col-sm-0.5 control-label''col-sm-2'];
  75.         $row2 $this->form->addFields( [new TLabel('KG')], [$C2_QUANT],[new TLabel('Codigo Receita')], [$RECEITA] );
  76.         $row2->layout = ['col-sm-2 control-label''col-sm-2','col-sm-0.1 control-label''col-sm-1'];
  77.         $this->form->addFields( [new TLabel('Remessa')], [$REMESSA]);
  78.         
  79.         // detail fields
  80.         $this->form->addContent( ['<h4>Details</h4><hr>'] );
  81.         $this->form->addFields( [$detail_uniqid] );
  82.         $this->form->addFields( [$detail_R_E_C_N_O_] );
  83.         
  84.         $this->form->addFields( [new TLabel('Z32 Barra')], [$detail_Z32_BARRA] );
  85.         $this->form->addFields( [new TLabel('Z32 Pesoli')], [$detail_Z32_PESOLI] );
  86.         $add TButton::create('add', [$this'onReload'], 'Register''fa:plus-circle green');
  87.         //$add->getAction()->setParameter('static','1');
  88.         $this->form->addFields( [], [$add] );
  89.         
  90.         
  91.         
  92.         
  93.         $this->detail_list = new BootstrapDatagridWrapper(new TDataGrid);
  94.         $this->detail_list->setId('Z32010_list');
  95.         $this->detail_list->generateHiddenFields();
  96.         $this->detail_list->style "min-width: 700px; width:100%;margin-bottom: 10px";
  97.         
  98.         // items
  99.         $this->detail_list->addColumn( new TDataGridColumn('uniqid''Uniqid''center') )->setVisibility(false);
  100.         $this->detail_list->addColumn( new TDataGridColumn('R_E_C_N_O_''Id''center') )->setVisibility(false);
  101.         $this->detail_list->addColumn( new TDataGridColumn('Z32_BARRA''Z32 Barra''left'100) );
  102.         $this->detail_list->addColumn( new TDataGridColumn('Z32_PESOLI''Z32 Pesoli''left'100) );
  103.         // detail actions
  104.         $action1 = new TDataGridAction([$this'onDetailEdit'] );
  105.         $action1->setFields( ['uniqid''*'] );
  106.         
  107.         $action2 = new TDataGridAction([$this'onDetailDelete']);
  108.         $action2->setField('uniqid');
  109.         
  110.         // add the actions to the datagrid
  111.         $this->detail_list->addAction($action1_t('Edit'), 'fa:edit blue');
  112.         $this->detail_list->addAction($action2_t('Delete'), 'far:trash-alt red');
  113.         
  114.         $this->detail_list->createModel();
  115.         
  116.         $panel = new TPanelGroup;
  117.         $panel->add($this->detail_list);
  118.         $panel->getBody()->style 'overflow-x:auto';
  119.         $this->form->addContent( [$panel] );
  120.         
  121.         
  122.         //$rowfooter = $this->form->addFields( [new TLabel('Insumos')], [$INSUMO1],[$INSUMO2], [$INSUMO3], [$INSUMO4], [$INSUMO5]);
  123.         //$rowfooter->layout = ['col-sm-2 control-label', 'col-sm-1','col-sm-1 control-label', 'col-sm-1','col-sm-1 control-label', 'col-sm-1','col-sm-1 control-label', 'col-sm-1'];
  124.         
  125.         //$rowfooter2 = $this->form->addFields( [new TLabel('Kilos por Insumo')], [$PORCEN1],[$PORCEN2], [$PORCEN3], [$PORCEN4], [$PORCEN5]);
  126.         //$rowfooter2->layout = ['col-sm-2 control-label', 'col-sm-1','col-sm-1 control-label', 'col-sm-1','col-sm-1 control-label', 'col-sm-1','col-sm-1 control-label', 'col-sm-1'];
  127.         
  128.         $this->form->addAction'Save',  new TAction([$this'onSave'], ['static'=>'1']), 'fa:save green');
  129.         $this->form->addAction'Clear', new TAction([$this'onClear']), 'fa:eraser red');
  130.         
  131.         $this->sumario = new BootstrapDatagridWrapper(new TDataGrid);
  132.         $this->sumario->style 'width: 100%';
  133.         
  134.         $prodmp  = new TDataGridColumn('prodmp',        'PRODUTO',        'center'100);
  135.         $peso  = new TDataGridColumn('peso''PESO''center',  100);
  136.         $this->sumario->addColumn($prodmp);
  137.         $this->sumario->addColumn($peso);
  138.         $this->sumario->createModel();
  139.         
  140.         $panel2 = new TPanelGroup;
  141.         $panel2->add($this->sumario);
  142.         $panel2->getBody()->style 'overflow-x:auto';
  143.         $this->form->addContent( [$panel2] );
  144.         
  145.         // create the page container
  146.         $container = new TVBox;
  147.         $container->style 'width: 100%';
  148.         // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  149.         $container->add($this->form);
  150.         parent::add($container);
  151.          
  152.     }
  153.     
  154.     /**
  155.      * Clear form
  156.      * @param $param URL parameters
  157.      */
  158.     public function onClear($param)
  159.     {
  160.         $this->form->clear(TRUE);
  161.     }
  162.     
  163.     /**
  164.      * Add detail item
  165.      * @param $param URL parameters
  166.      */
  167.     public function onDetailAdd$param )
  168.     {
  169.         try
  170.         {
  171.             $this->form->validate();
  172.             $data $this->form->getData();
  173.             
  174.             $uniqid = !empty($data->detail_uniqid) ? $data->detail_uniqid uniqid();
  175.             
  176.             $grid_data = [];
  177.             $grid_data['uniqid'] = $uniqid;
  178.             $grid_data['R_E_C_N_O_'] = $data->detail_R_E_C_N_O_;
  179.             $grid_data['Z32_BARRA'] = $data->detail_Z32_BARRA;
  180.             $grid_data['Z32_PESOLI'] = $data->detail_Z32_PESOLI;
  181.             
  182.             // insert row dynamically
  183.             $row $this->detail_list->addItem( (object) $grid_data );
  184.             $row->id $uniqid;
  185.             
  186.             TDataGrid::replaceRowById('Z32010_list'$uniqid$row);
  187.             
  188.             // clear detail form fields
  189.             $data->detail_uniqid '';
  190.             $data->detail_R_E_C_N_O_ '';
  191.             $data->detail_Z32_BARRA '';
  192.             $data->detail_Z32_PESOLI '';
  193.             
  194.             // send data, do not fire change/exit events
  195.             TForm::sendData'form_SC2010'$datafalsefalse );
  196.         }
  197.         catch (Exception $e)
  198.         {
  199.             $this->form->setData$this->form->getData());
  200.             new TMessage('error'$e->getMessage());
  201.         }
  202.     }
  203.     
  204.     /**
  205.      * Edit detail item
  206.      * @param $param URL parameters
  207.      */
  208.     public static function onDetailEdit$param )
  209.     {
  210.         $data = new stdClass;
  211.         $data->detail_uniqid $param['uniqid'];
  212.         $data->detail_R_E_C_N_O_ $param['R_E_C_N_O_'];
  213.         $data->detail_Z32_BARRA $param['Z32_BARRA'];
  214.         $data->detail_Z32_PESOLI $param['Z32_PESOLI'];
  215.         
  216.         // send data, do not fire change/exit events
  217.         TForm::sendData'form_SC2010'$datafalsefalse );
  218.     }
  219.     
  220.     /**
  221.      * Delete detail item
  222.      * @param $param URL parameters
  223.      */
  224.     public static function onDetailDelete$param )
  225.     {
  226.         // clear detail form fields
  227.         $data = new stdClass;
  228.         $data->detail_uniqid '';
  229.         $data->detail_R_E_C_N_O_ '';
  230.         $data->detail_Z32_BARRA '';
  231.         $data->detail_Z32_PESOLI '';
  232.         
  233.         // send data, do not fire change/exit events
  234.         TForm::sendData'form_SC2010'$datafalsefalse );
  235.         
  236.         // remove row
  237.         TDataGrid::removeRowById('Z32010_list'$param['uniqid']);
  238.     }
  239.     
  240.     /**
  241.      * Load Master/Detail data from database to form
  242.      */
  243.     public function onEdit($param)
  244.     {
  245.         try
  246.         {
  247.             TTransaction::open('banco');
  248.             
  249.             if (isset($param['key']))
  250.             {
  251.                 $key $param['key'];
  252.                 
  253.                 $object = new SC2010($key);
  254.                 $items  Z32010::where('Z32_PRODUT''='$key)->load();
  255.                 
  256.                 foreach( $items as $item )
  257.                 {
  258.                     $item->uniqid uniqid();
  259.                     $row $this->detail_list->addItem$item );
  260.                     $row->id $item->uniqid;
  261.                 }
  262.                 $this->form->setData($object);
  263.                 TTransaction::close();
  264.             }
  265.             else
  266.             {
  267.                 $this->form->clear(TRUE);
  268.             }
  269.         }
  270.         catch (Exception $e// in case of exception
  271.         {
  272.             new TMessage('error'$e->getMessage());
  273.             TTransaction::rollback();
  274.         }
  275.     }
  276.     
  277.     /**
  278.      * Save the Master/Detail data from form to database
  279.      */
  280.     public function onSave($param)
  281.     {
  282.         try
  283.         {
  284.             // open a transaction with database
  285.             TTransaction::open('banco');
  286.             
  287.             $data $this->form->getData();
  288.             $this->form->validate();
  289.             
  290.             $master = new SC2010;
  291.             $master->fromArray( (array) $data);
  292.             $master->store();
  293.             
  294.             Z32010::where('Z32_PRODUT''='$master->C2_NUM)->delete();
  295.             
  296.             if( $param['Z32010_list_Z32_BARRA'] )
  297.             {
  298.                 foreach( $param['Z32010_list_Z32_BARRA'] as $key => $item_id )
  299.                 {
  300.                     $detail = new Z32010;
  301.                     $detail->Z32_BARRA  $param['Z32010_list_Z32_BARRA'][$key];
  302.                     $detail->Z32_PESOLI  $param['Z32010_list_Z32_PESOLI'][$key];
  303.                     $detail->Z32_PRODUT $master->C2_NUM;
  304.                     $detail->store();
  305.                 }
  306.             }
  307.             TTransaction::close(); // close the transaction
  308.             
  309.             TForm::sendData('form_SC2010', (object) ['id' => $master->C2_NUM]);
  310.             
  311.             new TMessage('info'AdiantiCoreTranslator::translate('Record saved'));
  312.         }
  313.         catch (Exception $e// in case of exception
  314.         {
  315.             new TMessage('error'$e->getMessage());
  316.             $this->form->setData$this->form->getData() ); // keep form data
  317.             TTransaction::rollback();
  318.         }
  319.     }
  320.     
  321. public static function onExitOP($param)
  322. {
  323.     $product_code $param['C2_NUM'];
  324.     try
  325.     {
  326.         TTransaction::open('banco');
  327.         $product = new SC2010($product_code);
  328.         if ($product->C2_NUM !== null) {
  329.             $obj = new StdClass;
  330.             $obj->REMESSA $product->C2_REMESSA;
  331.             if ($product->C2_REMESSA != 0) {
  332.                 $calculatedQuant $product->C2_QUANT $product->C2_REMESSA;
  333.             } else {
  334.                 $calculatedQuant 0;
  335.             }
  336.             $obj->C2_QUANT $calculatedQuant;
  337.             TTransaction::close();
  338.             TForm::sendData('form_SC2010'$obj);
  339.         
  340.         } else {
  341.             TTransaction::close();
  342.         }
  343.     }
  344.     catch (Exception $e)
  345.     {
  346.         // Handle exceptions
  347.     }
  348.     }
  349.     
  350.         
  351.                 
  352.    public static function onExitProduct($param)
  353.         {  
  354.             $product_code $param['PRODUTO']; 
  355.             
  356.             try
  357.             {    
  358.                 TTransaction::open('banco');
  359.                 
  360.                 $criteria = new TCriteria;
  361.                 $criteria->add(new TFilter('Z46_PRODPA''='$product_code));
  362.                 //$criteria->setProperty('limit', 1);
  363.                 $repository = new TRepository('Z46010');
  364.                 $productList $repository->load($criteria);
  365.         
  366.                 if (!empty($productList)) {
  367.                     $product $productList[0];
  368.                     $obj = new StdClass;
  369.                     $obj->RECEITA $product->Z46_CODREC;                             
  370.         
  371.                     TTransaction::close();
  372.                     TForm::sendData('form_SC2010'$obj);
  373.                 } else {
  374.                     TTransaction::close();
  375.                     new TMessage('info','Ordem de Produção sem Receita');
  376.                 }
  377.             }
  378.             catch (Exception $e)
  379.             {
  380.                 // Handle exceptions
  381.             }
  382.         }
  383.  public static function onExitInsumos($param)
  384. {
  385.     $product_code $param['RECEITA']; 
  386.     $quant $param ['C2_QUANT'];
  387.     
  388.     try
  389.     {    
  390.         TTransaction::open('banco');
  391.         
  392.         $criteria = new TCriteria;
  393.         $criteria->add(new TFilter('Z46_CODREC''='$product_code));
  394.         $criteria->setProperty('limit'5);
  395.         $repository = new TRepository('Z46010');
  396.         $productList $repository->load($criteria);
  397.         TTransaction::close();
  398.         if (!empty($productList)) {
  399.             $obj = new StdClass;
  400.             
  401.          
  402.                 foreach ($productList as $index => $product) {
  403.             $fieldName 'INSUMO' . ($index 1);
  404.             $obj->$fieldName trim($product->Z46_PRODMP) . '-' $product->Z46_PERCEN.'%';
  405.         
  406.             $fieldNamePorcen 'PORCEN' . ($index 1);    
  407.                     
  408.             if ($quant != 0) {
  409.                 $calculatedValue = ($product->Z46_PERCEN 100) * $quant;
  410.                 $obj->$fieldNamePorcen $calculatedValue .' KG';
  411.             } else {
  412.                 $obj->$fieldNamePorcen 0;
  413.             }
  414.         }
  415.         TSession::setValue('form_SC2010'$obj);
  416.         $instance = new self ();
  417.         $instance->onReload();
  418.    
  419.         } else {
  420.             new TMessage('info''Ordem de Produção sem Receita');
  421.         }
  422.     }
  423.     catch (Exception $e)
  424.     {
  425.         // Handle exceptions
  426.     }
  427. }
  428.         
  429.      function onReload()
  430.         {  
  431.         
  432.         $this->sumario->clear();
  433.         $dataFromSession TSession::getValue('form_SC2010', []);
  434.         
  435.         if (!empty($dataFromSession)) {
  436.             $insumos = [];
  437.             $porcentagens = [];
  438.            
  439.             foreach ($dataFromSession as $propertyName => $propertyValue) {
  440.                 if (strpos($propertyName'INSUMO') === 0) {
  441.                     $insumos[$propertyName] = $propertyValue;
  442.                 } elseif (strpos($propertyName'PORCEN') === 0) {
  443.                     $porcentagens[$propertyName] = $propertyValue;
  444.                 }
  445.             }
  446.     
  447.             foreach ($insumos as $insumoName => $insumoValue) {
  448.                 $rowData = new stdClass();
  449.                 $rowData->prodmp $insumoValue
  450.                 $rowData->peso $porcentagens[str_replace('INSUMO''PORCEN'$insumoName)]; 
  451.                     
  452.             
  453.                 $this->sumario->addItem($rowData);
  454.             
  455.             }
  456.         } else {
  457.             new TMessage('Sumário não disponível');
  458.         }
  459.     }
  460. }
  461. ?>

Curso completo Meu Negócio Pronto
Use para si, ou transforme em um negócio: Inclui aulas e códigos-fontes
Gestor de conteúdo (SITE) + Loja Virtual (E-Commerce) + Emissor de Notas para infoprodutos


Meu negócio pronto Quero me inscrever agora!

Comentários (1)


FC

Nem tudo da para fazer de forma estática, mas pode forçar o reload com a função postData

TApplication::postData('seu_form', 'FormNovo', 'onReload');