MR
Como pegar dados do DataGrid
Olá,
Estou precisando de ajuda para pegar dados do datagrid e gravar em banco de dados... quando uso botão salvar não aparece nada ... já olhei outros post e nada ... não consigo entender a lógica do TdataGrid.
Segue código abaixo:
Grato pela ajuda !
Estou precisando de ajuda para pegar dados do datagrid e gravar em banco de dados... quando uso botão salvar não aparece nada ... já olhei outros post e nada ... não consigo entender a lógica do TdataGrid.
Segue código abaixo:
- <?php
- use Adianti\Widget\Base\TScript;
- use Adianti\Widget\Container\TPanelGroup;
- use Adianti\Widget\Datagrid\TDataGrid;
- use Adianti\Widget\Datagrid\TDataGridColumn;
- use Adianti\Widget\Dialog\TMessage;
- use Adianti\Widget\Form\TButton;
- use Adianti\Widget\Form\TEntry;
- use Adianti\Widget\Form\TNumeric;
- use Adianti\Wrapper\BootstrapDatagridWrapper;
- use Adianti\Wrapper\BootstrapFormBuilder;
- class ReposicaoProduto extends TPage
- {
- protected $form, $datagrid;
- public function __construct()
- {
- parent::__construct();
- $this->setTargetContainer('adianti_right_panel');
- $this->form = new BootstrapFormBuilder('form_Sale_View');;
- $this->form->addHeaderActionLink( _t('Close'), new TAction([__CLASS__, 'onClose'], ['static'=>'1']), 'fa:times red');
- $descricao = new TEntry('Nome');
- $descricao->setSize('100%');
- $this->form->addFields([new TLabel('Digite produto')], [$descricao]);
- $this->form->addAction('Buscar', new TAction([$this, 'onSearch']), 'fa:search blue');
- $this->form->addAction('Salvar', new TAction([$this, 'onsave']), 'fa:save green');
- // DataGrid
- $this->datagrid = new TDataGrid;
- $this->datagrid->style = 'width:100%';
- $this->datagrid->disableDefaultClick();
- $this->datagrid->datatable = true;
- $codigo = new TDataGridColumn('codigo', 'Código', 'left','10%');
- $nome = new TDataGridColumn('nome', 'Nome', 'left','60%') ;
- $preco = new TDataGridColumn('preco', 'Custo', 'left','20%');
- $quantidade = new TDataGridColumn('quantidade', 'Qtd Solicitada', 'center', '10%');
- $quantidade->setDataProperty('style','font-weight: bold');
- $quantidade->setTransformer( function($solicitado, $object, $row) {
- $widget = new TEntry('solicitado_' . $object->id);
- $widget->setValue( $solicitado );
- $widget->setSize(50);
- $widget->setFormName('Form_seach_produt');
- return $widget;
- });
- $formata_valor = function($valor, $objeto, $row) {
- if (is_numeric($valor))
- {
- return 'R$ ' . number_format($valor, 2, ',', '.');
- }
- return $valor;
- };
- $preco->setTransformer( $formata_valor );
- // add the columns to the DataGrid
- $this->datagrid->addColumn($codigo);
- $this->datagrid->addColumn($nome);
- $this->datagrid->addColumn($preco);
- $this->datagrid->addColumn($quantidade);
- // creates the datagrid model
- $this->datagrid->createModel();
- // wrap the page content using vertical box
- $vbox = new TVBox;
- $vbox->style = 'width: 100%';
- $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $vbox->add($this->form);
- $vbox->add($this->datagrid);
- parent::add($vbox);
- }
- public function onsave($param)
- {
- // Aqui Gostaria de pegar os dados do Datagrid, fazer Foreach(loop) e Gravar em uma tabela
- $items = $this->datagrid->getitems();
- var_dump($items);
- var_dump($param);
- //return;
- }
- public function onClear()
- {
- $this->form->clear( TRUE );
- }
- public function onSearch($param)
- {
- try
- {
- $this->datagrid->clear(true);
- $obj = new stdClass;
- $obj->nome =$param['Nome'];
- if (empty($obj->nome))
- {
- new TMessage('Info','precisa informar nome do produto');
- return;
- }
- $url = 'produtos.pesquisa.php';
- $token = 'xxxx';
- $data = "token=$token&pesquisa=$obj->nome&formato=JSON";
- $retorno = json_decode(ReposicaoLista::enviarREST($url, $data),true);
- if ($retorno['retorno']['status_processamento'] == 1){
- new TMessage('error','Token invalido');
- return;
- }
- if ($retorno['retorno']['status_processamento'] == 2){
- new TMessage('info','A Consulta não retornou registros');
- return;
- }
- if ($retorno['retorno']['status_processamento'] == 3){
- $pesq = ($retorno['retorno']['produtos']);
- foreach($pesq as $key => $value){
- $item = new StdClass;
- $item->id = $value['produto']['id'];
- $item->codigo = $value['produto']['codigo'] ;
- $item->nome = $value['produto']['nome'] ;
- $item->preco = $value['produto']['preco_custo'] ;
- $this->datagrid->addItem($item);
- }
- }
- }
- catch (Exception $e)
- {
- // shows the exception error message
- new TMessage('error', $e->getMessage());
- }
- }
- public static function onClose()
- {
- TScript::create("Template.closeRightPanel()");
- }
- }
- ?>
Grato pela ajuda !
Quando você clica no salvar(ou executa qualquer ação), todo o código é processado novamente. No caso do salvar, duas funções vão ser executadas: a construct e depois a onSave. Como a grid está sendo preenchida pela função onSearch(que não está sendo chamada), a função getItems não vai retornar nada dentro da onSave.
Você pode tentar chamar a onSearch dentro da onSave, antes de pegar os itens da grid. Ex: