Inserir mais de dois campos na janela do TSeekButton Olá pessoal, boa tarde, estou com dúvidas referente a janela do TSeekButton, onde apenas mostra dois campos "ID" e "Campo" no datagrid, gostaria de saber se tem a possibilidade de inserir mais de dois campos no datagrid e inserir um titulo para cada coluna. Agraço pelas respostas. Segue um print de exemplo. ...
FV
Inserir mais de dois campos na janela do TSeekButton  
Fechado
Olá pessoal, boa tarde, estou com dúvidas referente a janela do TSeekButton, onde apenas mostra dois campos "ID" e "Campo" no datagrid, gostaria de saber se tem a possibilidade de inserir mais de dois campos no datagrid e inserir um titulo para cada coluna.

Agraço pelas respostas.
Segue um print de exemplo.

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 (3)


ES

Rapaz, fiz uma gambiarra aqui e está funcionando. Quero melhorar este código talvez com uma array com os campos adicionais, mas ainda não consegui fazer isso. Segue os arquivos modificados que devem ficar na pasta applibwidget

TDBSeekButton2.php
 
  1. <?php
  2. //namespace Adianti\Widget\Wrapper;
  3. use Adianti\Core\AdiantiCoreTranslator;
  4. use Adianti\Database\TCriteria;
  5. use Adianti\Control\TAction;
  6. //use Exception;
  7. /**
  8. * Abstract Record Lookup Widget: Creates a lookup field used to search values from associated entities
  9. *
  10. * @version 2.0
  11. * @package widget
  12. * @subpackage wrapper
  13. * @author Pablo Dall'Oglio
  14. * @copyright Copyright (c) 2006-2014 Adianti Solutions Ltd. (http://www.adianti.com.br)
  15. * @license http://www.adianti.com.br/framework-license
  16. */
  17. class TDBSeekButton2 extends TSeekButton2
  18. {
  19. /**
  20. * Class Constructor
  21. * @param $name name of the form field
  22. * @param $database name of the database connection
  23. * @param $form name of the parent form
  24. * @param $model name of the Active Record to be searched
  25. * @param $display_field name of the field to be searched and shown
  26. * @param $receive_key name of the form field to receive the primary key
  27. * @param $receive_display_field name of the form field to receive the "display field"
  28. * @param $display_field2 name of the #2 field to be searched and shown
  29. */
  30. public function __construct($name, $database, $form, $model, $display_field, $receive_key, $receive_display_field, TCriteria $criteria = NULL, $display_field2=NULL)
  31. {
  32. parent::__construct($name);
  33. if (empty($database))
  34. {
  35. throw new Exception(AdiantiCoreTranslator::translate('The parameter (^1) of ^2 is required', 'database', __CLASS__));
  36. }
  37. if (empty($model))
  38. {
  39. throw new Exception(AdiantiCoreTranslator::translate('The parameter (^1) of ^2 is required', 'model', __CLASS__));
  40. }
  41. if (empty($display_field))
  42. {
  43. throw new Exception(AdiantiCoreTranslator::translate('The parameter (^1) of ^2 is required', 'display_field', __CLASS__));
  44. }
  45. $obj = new TStandardSeek2;
  46. // define the action parameters
  47. $action = new TAction(array($obj, 'onSetup'));
  48. $action->setParameter('database', $database);
  49. $action->setParameter('parent', $form);
  50. $action->setParameter('model', $model);
  51. $action->setParameter('display_field', $display_field);
  52. $action->setParameter('receive_key', $receive_key);
  53. $action->setParameter('receive_field', $receive_display_field);
  54. $action->setParameter('criteria', base64_encode(serialize($criteria)));
  55. $action->setParameter('display_field2', $display_field2);
  56. parent::setAction($action);
  57. }
  58. }
  59. ?>


TSeekButton2.php
 
  1. <?php
  2. //namespace Adianti\Widget\Form;
  3. use Adianti\Widget\Form\AdiantiWidgetInterface;
  4. use Adianti\Control\TAction;
  5. use Adianti\Widget\Base\TElement;
  6. use Adianti\Widget\Base\TScript;
  7. use Adianti\Widget\Form\TForm;
  8. use Adianti\Widget\Form\TField;
  9. use Adianti\Widget\Form\TEntry;
  10. use Adianti\Widget\Util\TImage;
  11. use Adianti\Core\AdiantiCoreTranslator;
  12. //use Exception;
  13. //use ReflectionClass;
  14. /**
  15. * Record Lookup Widget: Creates a lookup field used to search values from associated entities
  16. *
  17. * @version 2.0
  18. * @package widget
  19. * @subpackage form
  20. * @author Pablo Dall'Oglio
  21. * @copyright Copyright (c) 2006-2014 Adianti Solutions Ltd. (http://www.adianti.com.br)
  22. * @license http://www.adianti.com.br/framework-license
  23. */
  24. class TSeekButton2 extends TEntry implements AdiantiWidgetInterface
  25. {
  26. private $action;
  27. private $auxiliar;
  28. private $useOutEvent;
  29. private $button;
  30. protected $formName;
  31. protected $name;
  32. /**
  33. * Class Constructor
  34. * @param $name name of the field
  35. */
  36. public function __construct($name)
  37. {
  38. parent::__construct($name);
  39. $this->useOutEvent = TRUE;
  40. $this->setProperty('class', 'tfield tseekentry', TRUE); // classe CSS
  41. $image = new TImage('lib/adianti/images/ico_find.png');
  42. $this->button = new TElement('button');
  43. $this->button->{'class'} = 'btn btn-default tseekbutton';
  44. $this->button->{'type'} = 'button';
  45. $this->button->{'onmouseover'} = 'style.cursor = \'pointer\'';
  46. $this->button->{'name'} = '_' . $this->name . '_link';
  47. $this->button->{'onmouseout'} = 'style.cursor = \'default\'';
  48. $this->button->add($image);
  49. }
  50. /**
  51. * Returns a property value
  52. * @param $name Property Name
  53. */
  54. public function __get($name)
  55. {
  56. if ($name == 'button')
  57. {
  58. return $this->button;
  59. }
  60. else
  61. {
  62. return parent::__get($name);
  63. }
  64. }
  65. /**
  66. * Define it the out event will be fired
  67. */
  68. public function setUseOutEvent($bool)
  69. {
  70. $this->useOutEvent = $bool;
  71. }
  72. /**
  73. * Define the action for the SeekButton
  74. * @param $action Action taken when the user
  75. * clicks over the Seek Button (A TAction object)
  76. */
  77. public function setAction(TAction $action)
  78. {
  79. $this->action = $action;
  80. }
  81. /**
  82. * Define an auxiliar field
  83. * @param $object any TField object
  84. */
  85. public function setAuxiliar(TField $object)
  86. {
  87. $this->auxiliar = $object;
  88. }
  89. /**
  90. * Enable the field
  91. * @param $form_name Form name
  92. * @param $field Field name
  93. */
  94. public static function enableField($form_name, $field)
  95. {
  96. TScript::create( " tseekbutton_enable_field('{$form_name}', '{$field}'); " );
  97. }
  98. /**
  99. * Disable the field
  100. * @param $form_name Form name
  101. * @param $field Field name
  102. */
  103. public static function disableField($form_name, $field)
  104. {
  105. TScript::create( " tseekbutton_disable_field('{$form_name}', '{$field}'); " );
  106. }
  107. /**
  108. * Show the widget
  109. */
  110. public function show()
  111. {
  112. // check if it's not editable
  113. if (parent::getEditable())
  114. {
  115. if (!TForm::getFormByName($this->formName) instanceof TForm)
  116. {
  117. throw new Exception(AdiantiCoreTranslator::translate('You must pass the ^1 (^2) as a parameter to ^3', __CLASS__, $this->name, 'TForm::setFields()') );
  118. }
  119. $serialized_action = '';
  120. if ($this->action)
  121. {
  122. // get the action class name
  123. if (is_array($callback = $this->action->getAction()))
  124. {
  125. if (is_object($callback[0]))
  126. {
  127. $rc = new ReflectionClass($callback[0]);
  128. $classname = $rc->getShortName();
  129. }
  130. else
  131. {
  132. $classname = $callback[0];
  133. }
  134. $inst = new $classname;
  135. $ajaxAction = new TAction(array($inst, 'onSelect'));
  136. if (in_array($classname, array('TStandardSeek2')))
  137. {
  138. $ajaxAction->setParameter('parent', $this->action->getParameter('parent'));
  139. $ajaxAction->setParameter('database',$this->action->getParameter('database'));
  140. $ajaxAction->setParameter('model', $this->action->getParameter('model'));
  141. $ajaxAction->setParameter('display_field', $this->action->getParameter('display_field'));
  142. $ajaxAction->setParameter('receive_key', $this->action->getParameter('receive_key'));
  143. $ajaxAction->setParameter('receive_field', $this->action->getParameter('receive_field'));
  144. $ajaxAction->setParameter('criteria', $this->action->getParameter('criteria'));
  145. $ajaxAction->setParameter('display_field2', $this->action->getParameter('display_field2'));
  146. }
  147. else
  148. {
  149. if($actionParameters = $this->action->getParameters())
  150. {
  151. foreach ($actionParameters as $key => $value)
  152. {
  153. $ajaxAction->setParameter($key, $value);
  154. }
  155. }
  156. }
  157. $ajaxAction->setParameter('form_name', $this->formName);
  158. $string_action = $ajaxAction->serialize(FALSE);
  159. if ($this->useOutEvent)
  160. {
  161. $this->setProperty('seekaction', "__adianti_post_lookup('{$this->formName}', '{$string_action}', document.{$this->formName}.{$this->name})");
  162. $this->setProperty('onBlur', $this->getProperty('seekaction'), FALSE);
  163. }
  164. }
  165. $this->action->setParameter('form_name', $this->formName);
  166. $serialized_action = $this->action->serialize(FALSE);
  167. }
  168. parent::show();
  169. $this->button-> onclick = "javascript:serialform=(\$('#{$this->formName}').serialize());
  170. __adianti_append_page('engine.php?{$serialized_action}&'+serialform)";
  171. $this->button->show();
  172. if ($this->auxiliar)
  173. {
  174. echo '&nbsp;';
  175. $this->auxiliar->show();
  176. }
  177. }
  178. else
  179. {
  180. parent::show();
  181. }
  182. }
  183. }
  184. ?>


TStandardSeek2.php
 
  1. <?php
  2. //namespace Adianti\Base;
  3. use Adianti\Core\AdiantiCoreTranslator;
  4. use Adianti\Widget\Dialog\TMessage;
  5. use Adianti\Widget\Container\TTable;
  6. use Adianti\Widget\Container\TVBox;
  7. use Adianti\Widget\Form\TForm;
  8. use Adianti\Widget\Form\TEntry;
  9. use Adianti\Widget\Form\TLabel;
  10. use Adianti\Widget\Form\TButton;
  11. use Adianti\Widget\Datagrid\TDataGrid;
  12. use Adianti\Widget\Datagrid\TDataGridColumn;
  13. use Adianti\Widget\Datagrid\TDataGridAction;
  14. use Adianti\Widget\Datagrid\TPageNavigation;
  15. use Adianti\Control\TWindow;
  16. use Adianti\Control\TAction;
  17. use Adianti\Database\TTransaction;
  18. use Adianti\Database\TRepository;
  19. use Adianti\Database\TRecord;
  20. use Adianti\Database\TFilter;
  21. use Adianti\Database\TCriteria;
  22. use Adianti\Registry\TSession;
  23. use Adianti\Widget\Container\TPanelGroup;
  24. //use Exception;
  25. //use StdClass;
  26. /**
  27. * Standard Page controller for Seek buttons
  28. *
  29. * @version 2.0
  30. * @package base
  31. * @author Pablo Dall'Oglio
  32. * @copyright Copyright (c) 2006-2014 Adianti Solutions Ltd. (http://www.adianti.com.br)
  33. * @license http://www.adianti.com.br/framework-license
  34. */
  35. class TStandardSeek2 extends TWindow
  36. {
  37. private $form; // search form
  38. private $datagrid; // listing
  39. private $pageNavigation;
  40. private $parentForm;
  41. private $loaded;
  42. /**
  43. * Constructor Method
  44. * Creates the page, the search form and the listing
  45. */
  46. public function __construct()
  47. {
  48. parent::__construct();
  49. parent::setTitle( AdiantiCoreTranslator::translate('Search record') );
  50. parent::setSize(0.7, 640);
  51. // creates a new form
  52. $this->form = new TForm('form_standard_seek');
  53. // creates a new table
  54. $table = new TTable;
  55. $table->{'width'} = '100%';
  56. // adds the table into the form
  57. $this->form->add($table);
  58. // create the form fields
  59. $display_field= new TEntry('display_field');
  60. $display_field->setSize('90%');
  61. // keeps the field's value
  62. $display_field->setValue(TSession::getValue('tstandardseek_display_value') );
  63. // create the action button
  64. $find_button = new TButton('busca');
  65. // define the button action
  66. $find_button->setAction(new TAction(array($this, 'onSearch')), AdiantiCoreTranslator::translate('Search'));
  67. $find_button->setImage('fa:search blue');
  68. // add a row for the filter field
  69. $table->addRowSet( new TLabel(_t('Search').': '), $display_field, $find_button);
  70. // define wich are the form fields
  71. $this->form->setFields(array($display_field, $find_button));
  72. // creates a new datagrid
  73. $this->datagrid = new TDataGrid;
  74. $this->datagrid->{'style'} = 'width: 100%';
  75. // create two datagrid columns
  76. $id = new TDataGridColumn('id', 'ID', 'right', '16%');
  77. $display = new TDataGridColumn('display_field', AdiantiCoreTranslator::translate('Field'), 'left');
  78. $display2 = new TDataGridColumn('display_field2', AdiantiCoreTranslator::translate('Field'), 'left');
  79. // add the columns to the datagrid
  80. $this->datagrid->addColumn($id);
  81. $this->datagrid->addColumn($display);
  82. $this->datagrid->addColumn($display2);
  83. // create a datagrid action
  84. $action1 = new TDataGridAction(array($this, 'onSelect'));
  85. $action1->setLabel(AdiantiCoreTranslator::translate('Select'));
  86. $action1->setImage('fa:check-circle-o green');
  87. $action1->setUseButton(TRUE);
  88. $action1->setField('id');
  89. // add the actions to the datagrid
  90. $this->datagrid->addAction($action1);
  91. // create the datagrid model
  92. $this->datagrid->createModel();
  93. // creates the paginator
  94. $this->pageNavigation = new TPageNavigation;
  95. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  96. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  97. $panel = new TPanelGroup();
  98. $panel->add($this->form);
  99. // creates the container
  100. $vbox = new TVBox;
  101. $vbox->add($panel);
  102. $vbox->add($this->datagrid);
  103. $vbox->add($this->pageNavigation);
  104. $vbox->{'style'} = 'width: 100%';
  105. // add the container to the page
  106. parent::add($vbox);
  107. }
  108. /**
  109. * Register the user filter in the section
  110. */
  111. public function onSearch()
  112. {
  113. // get the form data
  114. $data = $this->form->getData();
  115. // check if the user has filled the form
  116. if (isset($data-> display_field) AND ($data-> display_field))
  117. {
  118. // creates a filter using the form content
  119. $display_field = TSession::getValue('standard_seek_display_field');
  120. $filter = new TFilter($display_field, 'like', "%{$data-> display_field}%");
  121. // store the filter in section
  122. TSession::setValue('tstandardseek_filter', $filter);
  123. TSession::setValue('tstandardseek_display_value', $data-> display_field);
  124. }
  125. else
  126. {
  127. TSession::setValue('tstandardseek_filter', NULL);
  128. TSession::setValue('tstandardseek_display_value', '');
  129. }
  130. TSession::setValue('tstandardseek_filter_data', $data);
  131. // set the data back to the form
  132. $this->form->setData($data);
  133. $param=array();
  134. $param['offset'] =0;
  135. $param['first_page']=1;
  136. $this->onReload($param);
  137. }
  138. /**
  139. * Load the datagrid with the active record objects
  140. */
  141. public function onReload($param = NULL)
  142. {
  143. try
  144. {
  145. $model = TSession::getValue('standard_seek_model');
  146. $database = TSession::getValue('standard_seek_database');
  147. $pk = constant("{$model}::PRIMARYKEY");
  148. // begins the transaction with database
  149. TTransaction::open($database);
  150. // creates a repository for the model
  151. $repository = new TRepository($model);
  152. $limit = 10;
  153. // creates a criteria
  154. if (TSession::getValue('standard_seek_criteria'))
  155. {
  156. $criteria = clone TSession::getValue('standard_seek_criteria');
  157. }
  158. else
  159. {
  160. $criteria = new TCriteria;
  161. // default order
  162. if (empty($param['order']))
  163. {
  164. $param['order'] = $pk;
  165. $param['direction'] = 'asc';
  166. }
  167. }
  168. $criteria->setProperties($param); // order, offset
  169. $criteria->setProperty('limit', $limit);
  170. if (TSession::getValue('tstandardseek_filter'))
  171. {
  172. // add the filter to the criteria
  173. $criteria->add(TSession::getValue('tstandardseek_filter'));
  174. }
  175. // load all objects according with the criteria
  176. $objects = $repository->load($criteria, FALSE);
  177. $this->datagrid->clear();
  178. if ($objects)
  179. {
  180. $display_field = TSession::getValue('standard_seek_display_field');
  181. $display_field2 = TSession::getValue('standard_seek_display_field2');
  182. foreach ($objects as $object)
  183. {
  184. $item = $object;
  185. $item-> id = $object->$pk;
  186. $item-> display_field = $object->$display_field;
  187. // @todo testar
  188. $item-> display_field2 = $object->$display_field2;
  189. // add the object into the datagrid
  190. $this->datagrid->addItem($item);
  191. }
  192. }
  193. // clear the crieteria to count the records
  194. $criteria->resetProperties();
  195. $count= $repository->count($criteria);
  196. $this->pageNavigation->setCount($count); // count of records
  197. $this->pageNavigation->setProperties($param); // order, page
  198. $this->pageNavigation->setLimit($limit); // limit
  199. // closes the transaction
  200. TTransaction::close();
  201. $this->loaded = true;
  202. }
  203. catch (Exception $e) // in case of exception
  204. {
  205. // shows the exception genearated message
  206. new TMessage('error', '<b>Erro</b> ' . $e->getMessage());
  207. // rollback all the database operations
  208. TTransaction::rollback();
  209. }
  210. }
  211. /**
  212. * define the standars seek parameters
  213. */
  214. public function onSetup($param=NULL)
  215. {
  216. // store the parameters in the section
  217. TSession::setValue('tstandardseek_filter', NULL);
  218. TSession::setValue('tstandardseek_display_value', NULL);
  219. TSession::setValue('standard_seek_receive_key', $param['receive_key']);
  220. TSession::setValue('standard_seek_receive_field', $param['receive_field']);
  221. TSession::setValue('standard_seek_display_field', $param['display_field']);
  222. TSession::setValue('standard_seek_display_field2', $param['display_field2']);
  223. TSession::setValue('standard_seek_model', $param['model']);
  224. TSession::setValue('standard_seek_database', $param['database']);
  225. TSession::setValue('standard_seek_parent', $param['parent']);
  226. if (isset($param['criteria']) AND $param['criteria'])
  227. {
  228. TSession::setValue('standard_seek_criteria', unserialize(base64_decode($param['criteria'])));
  229. }
  230. $this->onReload();
  231. }
  232. /**
  233. * Select the register by ID and return the information to the main form
  234. * When using onblur signal, AJAX passes all needed parameters via GET
  235. * instead of calling onSetup before.
  236. */
  237. public static function onSelect($param)
  238. {
  239. $key = $param['key'];
  240. $database = isset($param['database']) ? $param['database'] : TSession::getValue('standard_seek_database');
  241. $receive_key = isset($param['receive_key']) ? $param['receive_key'] : TSession::getValue('standard_seek_receive_key');
  242. $receive_field = isset($param['receive_field']) ? $param['receive_field'] : TSession::getValue('standard_seek_receive_field');
  243. $display_field = isset($param['display_field']) ? $param['display_field'] : TSession::getValue('standard_seek_display_field');
  244. $parent = isset($param['parent']) ? $param['parent'] : TSession::getValue('standard_seek_parent');
  245. try
  246. {
  247. TTransaction::open($database);
  248. // load the active record
  249. $model = isset($param['model']) ? $param['model'] : TSession::getValue('standard_seek_model');
  250. $activeRecord = new $model($key);
  251. $pk = constant("{$model}::PRIMARYKEY");
  252. $object = new StdClass;
  253. $object->$receive_key = isset($activeRecord->$pk) ? $activeRecord->$pk : '';
  254. $object->$receive_field = isset($activeRecord->$display_field) ? $activeRecord->$display_field : '';
  255. TTransaction::close();
  256. TForm::sendData($parent, $object);
  257. parent::closeWindow(); // closes the window
  258. }
  259. catch (Exception $e) // in case of exception
  260. {
  261. // clear fields
  262. $object = new StdClass;
  263. $object->$receive_key = '';
  264. $object->$receive_field = '';
  265. TForm::sendData($parent, $object);
  266. // undo all pending operations
  267. TTransaction::rollback();
  268. }
  269. }
  270. }
  271. ?>

FV

Boa tarde Eduardo Soares, consegui resolver. Eu estava utilizando o padrão do Adianti Studio, onde é criado um xml e lá da opção de um campo receber o ID e outro um Campo qualquer como ta no meu print. Criei uma classe que xtends TWindow, na própia classe inseri um campo de de busca e um datagrid normal e foi resolvido.

Mesmo assim valeu, ficou legal sua adotação.
FV

adaptação* :)