IL
Botao no Form
Fechado
Boa Tarde
Estou montando um GridScrollView com Checkbox no Grid, ate ai tudo bem mas quando coloco um botao para pegar as colunas que esta selecionadas pelo checkbox esta apresentando um erro.
Segue meu codigo
Estou montando um GridScrollView com Checkbox no Grid, ate ai tudo bem mas quando coloco um botao para pegar as colunas que esta selecionadas pelo checkbox esta apresentando um erro.
Segue meu codigo
- <?php
- /**
- * DatagridScrollView
- *
- * @version 1.0
- * @package samples
- * @subpackage tutor
- * @author Pablo Dall'Oglio
- * @copyright Copyright (c) 2006-2014 Adianti Solutions Ltd. (http://www.adianti.com.br)
- * @license http://www.adianti.com.br/framework-license
- */
- class DatagridScrollView extends TPage
- {
- private $datagrid;
- public function __construct()
- {
- parent::__construct();
- $this->form = new TForm;
- // creates one datagrid
- $this->datagrid = new TDataGrid;
- // $this->form->add($this->datagrid);
- // make scrollable and define height
- $this->datagrid->setHeight(300);
- $this->datagrid->makeScrollable();
- // create the datagrid columns
- $check = new TDataGridColumn('check', 'check', 'right', 70);
- $code = new TDataGridColumn('code', 'Code', 'right', 70);
- $name = new TDataGridColumn('name', 'Name', 'left', 180);
- $address = new TDataGridColumn('address', 'Address', 'left', 180);
- $telephone = new TDataGridColumn('fone', 'Phone', 'left', 160);
- // add the columns to the datagrid
- $this->datagrid->addColumn($check);
- $this->datagrid->addColumn($code);
- $this->datagrid->addColumn($name);
- $this->datagrid->addColumn($address);
- $this->datagrid->addColumn($telephone);
- // creates the datagrid model
- $this->datagrid->createModel();
- // creates the action button
- $button1=new TButton('action1');
- // define the button action
- $button1->setAction(new TAction(array($this, 'onSave')), 'Save');
- $button1->setImage('ico_save.png');
- // $this->formFields[] = $button1;
- // $this->form->setFields($this->formFields);
- // wrap the page content using vertical box
- $vbox = new TVBox;
- $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $vbox->add($this->datagrid);
- $vbox->add($this->form);
- $vbox->add($button1);
- parent::add($vbox);
- }
- /**
- * Load the data into the datagrid
- */
- function onReload()
- {
- $this->datagrid->clear();
- for ($n=1; $n<=40; $n++)
- {
- // add an regular object to the datagrid
- $item = new StdClass;
- $item->check = new TCheckButton('check1');
- $item->check->setIndexValue('on');
- $item->code = $n;
- $item->name = 'Person name';
- $item->address = 'Person address';
- $item->fone = '1111-1111';
- $this->datagrid->addItem($item);
- }
- }
- public function onSave($param)
- {
- $data = $this->form->getData(); // optional parameter: active record class
- // put the data back to the form
- $this->form->setData($data);
- // creates a string with the form element's values
- $message = 'Check 1 : ' . $data->check1 . '<br>';
- $message.= 'Check 2 : ' . $data->check2 . '<br>';
- $message.= 'Check 3 : ' . $data->check3 . '<br>';
- $message.= 'Check 4 : ' . $data->check4 . '<br>';
- // show the message
- new TMessage('info', $message);
- }
- /**
- * shows the page
- */
- function show()
- {
- $this->onReload();
- parent::show();
- }
- }
- ?>
Oi Ivan,
Você deve ter se baseado neste exemplo:
www.adianti.com.br/framework_files/tutor/index.php?class=DatagridChe
Entretanto você eliminou e comentou linhas de código fundamentais para o funcionamento adequado ;-) Eu explico...
Quando você monta alguma tela com o TForm, precisa adicionar conteúdo à ele, o que é algo somente gráfico. Mas por trás disso tudo, logicamente o TForm precisa saber quais campos ele está manipulando, e é aí que entra o setFields(), o setFields() diz para o TForm quais campos ele irá manipular. Você deve passar todos os campos do formulário e até mesmo o próprio botão para o setFields(). Como nesse caso você cria campos dentro de um loop (onReload), deverá acrescentar cada campo criado no loop também ao setFields(). Por isso o exemplo tinha um vetor chamado $this->formFields. Ele não deve ser removido, pois sem ele, o formulário não postará nada...
Esta operação só não é necessária ao usarmos o TQuickForm, pois ele já faz isso internamente, mas não é o caso nesse exemplo.
Percebi isso, pois ao rodar o seu exemplo, o erro logo apareceu:
You must pass the TButton (Save) as a parameter to TForm::setFields()
Att,
Pablo