Lançado Adianti Framework 7.6!
Clique aqui para saber mais
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 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 (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_fieldTCriteria $criteria NULL$display_field2=NULL)
  31.     {
  32.         parent::__construct($name);
  33.         
  34.         if (empty($database))
  35.         {
  36.             throw new Exception(AdiantiCoreTranslator::translate('The parameter (^1) of ^2 is required''database'__CLASS__));
  37.         }
  38.         
  39.         if (empty($model))
  40.         {
  41.             throw new Exception(AdiantiCoreTranslator::translate('The parameter (^1) of ^2 is required''model'__CLASS__));
  42.         }
  43.         
  44.         if (empty($display_field))
  45.         {
  46.             throw new Exception(AdiantiCoreTranslator::translate('The parameter (^1) of ^2 is required''display_field'__CLASS__));
  47.         }
  48.         
  49.         $obj = new TStandardSeek2;
  50.         
  51.         // define the action parameters
  52.         $action = new TAction(array($obj'onSetup'));
  53.         $action->setParameter('database',      $database);
  54.         $action->setParameter('parent',        $form);
  55.         $action->setParameter('model',         $model);
  56.         $action->setParameter('display_field'$display_field);
  57.         $action->setParameter('receive_key',   $receive_key);
  58.         $action->setParameter('receive_field'$receive_display_field);
  59.         $action->setParameter('criteria',      base64_encode(serialize($criteria)));
  60.         $action->setParameter('display_field2'$display_field2);
  61.         parent::setAction($action);
  62.     }
  63. }
  64. ?>


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.     /**
  34.      * Class Constructor
  35.      * @param  $name name of the field
  36.      */
  37.     public function __construct($name)
  38.     {
  39.         parent::__construct($name);
  40.         $this->useOutEvent TRUE;
  41.         $this->setProperty('class''tfield tseekentry'TRUE);   // classe CSS
  42.         $image = new TImage('lib/adianti/images/ico_find.png');
  43.         
  44.         $this->button = new TElement('button');
  45.         $this->button->{'class'} = 'btn btn-default tseekbutton';
  46.         $this->button->{'type'} = 'button';
  47.         $this->button->{'onmouseover'} = 'style.cursor = \'pointer\'';
  48.         $this->button->{'name'} = '_' $this->name '_link';
  49.         $this->button->{'onmouseout'}  = 'style.cursor = \'default\'';
  50.         $this->button->add($image);
  51.     }
  52.     
  53.     /**
  54.      * Returns a property value
  55.      * @param $name     Property Name
  56.      */
  57.     public function __get($name)
  58.     {
  59.         if ($name == 'button')
  60.         {
  61.             return $this->button;
  62.         }
  63.         else
  64.         {
  65.             return parent::__get($name);
  66.         }
  67.     }
  68.     
  69.     /**
  70.      * Define it the out event will be fired
  71.      */
  72.     public function setUseOutEvent($bool)
  73.     {
  74.         $this->useOutEvent $bool;
  75.     }
  76.     
  77.     /**
  78.      * Define the action for the SeekButton
  79.      * @param $action Action taken when the user
  80.      * clicks over the Seek Button (A TAction object)
  81.      */
  82.     public function setAction(TAction $action)
  83.     {
  84.         $this->action $action;
  85.     }
  86.     
  87.     /**
  88.      * Define an auxiliar field
  89.      * @param $object any TField object
  90.      */
  91.     public function setAuxiliar(TField $object)
  92.     {
  93.         $this->auxiliar $object;
  94.     }
  95.     
  96.     /**
  97.      * Enable the field
  98.      * @param $form_name Form name
  99.      * @param $field Field name
  100.      */
  101.     public static function enableField($form_name$field)
  102.     {
  103.         TScript::create" tseekbutton_enable_field('{$form_name}', '{$field}'); " );
  104.     }
  105.     
  106.     /**
  107.      * Disable the field
  108.      * @param $form_name Form name
  109.      * @param $field Field name
  110.      */
  111.     public static function disableField($form_name$field)
  112.     {
  113.         TScript::create" tseekbutton_disable_field('{$form_name}', '{$field}'); " );
  114.     }
  115.     
  116.     /**
  117.      * Show the widget
  118.      */
  119.     public function show()
  120.     {
  121.         // check if it's not editable
  122.         if (parent::getEditable())
  123.         {
  124.             if (!TForm::getFormByName($this->formName) instanceof TForm)
  125.             {
  126.                 throw new Exception(AdiantiCoreTranslator::translate('You must pass the ^1 (^2) as a parameter to ^3'__CLASS__$this->name'TForm::setFields()') );
  127.             }
  128.             
  129.             $serialized_action '';
  130.             if ($this->action)
  131.             {
  132.                 // get the action class name
  133.                 if (is_array($callback $this->action->getAction()))
  134.                 {
  135.                     if (is_object($callback[0]))
  136.                     {
  137.                         $rc = new ReflectionClass($callback[0]);
  138.                         $classname $rc->getShortName();
  139.                     }
  140.                     else
  141.                     {
  142.                         $classname  $callback[0];
  143.                     }
  144.                     $inst       = new $classname;
  145.                     $ajaxAction = new TAction(array($inst'onSelect'));
  146.                     
  147.                     if (in_array($classname, array('TStandardSeek2')))
  148.                     {
  149.                         $ajaxAction->setParameter('parent',  $this->action->getParameter('parent'));
  150.                         $ajaxAction->setParameter('database',$this->action->getParameter('database'));
  151.                         $ajaxAction->setParameter('model',   $this->action->getParameter('model'));
  152.                         $ajaxAction->setParameter('display_field'$this->action->getParameter('display_field'));
  153.                         $ajaxAction->setParameter('receive_key',   $this->action->getParameter('receive_key'));
  154.                         $ajaxAction->setParameter('receive_field'$this->action->getParameter('receive_field'));
  155.                         $ajaxAction->setParameter('criteria',      $this->action->getParameter('criteria'));
  156.                         $ajaxAction->setParameter('display_field2'$this->action->getParameter('display_field2'));
  157.                     }
  158.                     else
  159.                     {
  160.                         if($actionParameters $this->action->getParameters())
  161.                         {
  162.                             foreach ($actionParameters as $key => $value
  163.                             {
  164.                                 $ajaxAction->setParameter($key$value);
  165.                             }                            
  166.                         }                                            
  167.                     }
  168.                     $ajaxAction->setParameter('form_name'$this->formName);
  169.                     $string_action $ajaxAction->serialize(FALSE);
  170.                     if ($this->useOutEvent)
  171.                     {
  172.                         $this->setProperty('seekaction'"__adianti_post_lookup('{$this->formName}', '{$string_action}', document.{$this->formName}.{$this->name})");
  173.                         $this->setProperty('onBlur'$this->getProperty('seekaction'), FALSE);
  174.                     }
  175.                 }
  176.                 $this->action->setParameter('form_name'$this->formName);
  177.                 $serialized_action $this->action->serialize(FALSE);
  178.             }
  179.             parent::show();
  180.             
  181.             $this->button-> onclick "javascript:serialform=(\$('#{$this->formName}').serialize());
  182.                   __adianti_append_page('engine.php?{$serialized_action}&'+serialform)";
  183.             
  184.             $this->button->show();
  185.             
  186.             if ($this->auxiliar)
  187.             {
  188.                 echo '&nbsp;';
  189.                 $this->auxiliar->show();
  190.             }
  191.         }
  192.         else
  193.         {
  194.             parent::show();
  195.         }
  196.     }
  197. }
  198. ?>


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.     /**
  44.      * Constructor Method
  45.      * Creates the page, the search form and the listing
  46.      */
  47.     public function __construct()
  48.     {
  49.         parent::__construct();
  50.         parent::setTitleAdiantiCoreTranslator::translate('Search record') );
  51.         parent::setSize(0.7640);
  52.         
  53.         // creates a new form
  54.         $this->form = new TForm('form_standard_seek');
  55.         // creates a new table
  56.         $table = new TTable;
  57.         $table->{'width'} = '100%';
  58.         // adds the table into the form
  59.         $this->form->add($table);
  60.         
  61.         // create the form fields
  62.         $display_field= new TEntry('display_field');
  63.         $display_field->setSize('90%');
  64.         
  65.         // keeps the field's value
  66.         $display_field->setValue(TSession::getValue('tstandardseek_display_value') );
  67.         
  68.         // create the action button
  69.         $find_button = new TButton('busca');
  70.         // define the button action
  71.         $find_button->setAction(new TAction(array($this'onSearch')), AdiantiCoreTranslator::translate('Search'));
  72.         $find_button->setImage('fa:search blue');
  73.         
  74.         // add a row for the filter field
  75.         $table->addRowSet( new TLabel(_t('Search').': '), $display_field$find_button);
  76.         
  77.         // define wich are the form fields
  78.         $this->form->setFields(array($display_field$find_button));
  79.         
  80.         // creates a new datagrid
  81.         $this->datagrid = new TDataGrid;
  82.         $this->datagrid->{'style'} = 'width: 100%';
  83.         
  84.         // create two datagrid columns
  85.         $id      = new TDataGridColumn('id',            'ID',    'right''16%');
  86.         $display = new TDataGridColumn('display_field'AdiantiCoreTranslator::translate('Field'), 'left');
  87.         $display2 = new TDataGridColumn('display_field2'AdiantiCoreTranslator::translate('Field'), 'left');
  88.         
  89.         // add the columns to the datagrid
  90.         $this->datagrid->addColumn($id);
  91.         $this->datagrid->addColumn($display);
  92.         $this->datagrid->addColumn($display2);
  93.         
  94.         // create a datagrid action
  95.         $action1 = new TDataGridAction(array($this'onSelect'));
  96.         $action1->setLabel(AdiantiCoreTranslator::translate('Select'));
  97.         $action1->setImage('fa:check-circle-o green');
  98.         $action1->setUseButton(TRUE);
  99.         $action1->setField('id');
  100.         
  101.         // add the actions to the datagrid
  102.         $this->datagrid->addAction($action1);
  103.         
  104.         // create the datagrid model
  105.         $this->datagrid->createModel();
  106.         
  107.         // creates the paginator
  108.         $this->pageNavigation = new TPageNavigation;
  109.         $this->pageNavigation->setAction(new TAction(array($this'onReload')));
  110.         $this->pageNavigation->setWidth($this->datagrid->getWidth());
  111.         
  112.         $panel = new TPanelGroup();
  113.         $panel->add($this->form);
  114.         
  115.         // creates the container
  116.         $vbox = new TVBox;
  117.         $vbox->add($panel);
  118.         $vbox->add($this->datagrid);
  119.         $vbox->add($this->pageNavigation);
  120.         $vbox->{'style'} = 'width: 100%';
  121.         
  122.         // add the container to the page
  123.         parent::add($vbox);
  124.     }
  125.     
  126.     /**
  127.      * Register the user filter in the section
  128.      */
  129.     public function onSearch()
  130.     {
  131.         // get the form data
  132.         $data $this->form->getData();
  133.         
  134.         // check if the user has filled the form
  135.         if (isset($data-> display_field) AND ($data-> display_field))
  136.         {
  137.             // creates a filter using the form content
  138.             $display_field TSession::getValue('standard_seek_display_field');
  139.             $filter = new TFilter($display_field'like'"%{$data-> display_field}%");
  140.             
  141.             // store the filter in section
  142.             TSession::setValue('tstandardseek_filter',        $filter);
  143.             TSession::setValue('tstandardseek_display_value'$data-> display_field);
  144.         }
  145.         else
  146.         {
  147.             TSession::setValue('tstandardseek_filter',        NULL);
  148.             TSession::setValue('tstandardseek_display_value''');
  149.         }
  150.         
  151.         TSession::setValue('tstandardseek_filter_data'$data);
  152.         
  153.         // set the data back to the form
  154.         $this->form->setData($data);
  155.         
  156.         $param=array();
  157.         $param['offset']    =0;
  158.         $param['first_page']=1;
  159.         $this->onReload($param);
  160.     }
  161.     
  162.     /**
  163.      * Load the datagrid with the active record objects
  164.      */
  165.     public function onReload($param NULL)
  166.     {
  167.         try
  168.         {
  169.             $model    TSession::getValue('standard_seek_model');
  170.             $database TSession::getValue('standard_seek_database');
  171.             
  172.             $pk   constant("{$model}::PRIMARYKEY");
  173.             
  174.             // begins the transaction with database
  175.             TTransaction::open($database);
  176.             
  177.             // creates a repository for the model
  178.             $repository = new TRepository($model);
  179.             $limit 10;
  180.             
  181.             // creates a criteria
  182.             if (TSession::getValue('standard_seek_criteria'))
  183.             {
  184.                 $criteria = clone TSession::getValue('standard_seek_criteria');
  185.             }
  186.             else
  187.             {
  188.                 $criteria = new TCriteria;
  189.                 
  190.                 // default order
  191.                 if (empty($param['order']))
  192.                 {
  193.                     $param['order'] = $pk;
  194.                     $param['direction'] = 'asc';
  195.                 }
  196.             }
  197.             $criteria->setProperties($param); // order, offset
  198.             $criteria->setProperty('limit'$limit);
  199.             
  200.             if (TSession::getValue('tstandardseek_filter'))
  201.             {
  202.                 // add the filter to the criteria
  203.                 $criteria->add(TSession::getValue('tstandardseek_filter'));
  204.             }
  205.             
  206.             // load all objects according with the criteria
  207.             $objects $repository->load($criteriaFALSE);
  208.             $this->datagrid->clear();
  209.             if ($objects)
  210.             {
  211.                 $display_field TSession::getValue('standard_seek_display_field');
  212.                 $display_field2 TSession::getValue('standard_seek_display_field2');
  213.                 foreach ($objects as $object)
  214.                 {
  215.                     
  216.                     $item $object;
  217.                     $item-> id $object->$pk;
  218.                     $item-> display_field $object->$display_field;
  219.                     // @todo testar
  220.                     $item-> display_field2 $object->$display_field2;
  221.                     // add the object into the datagrid
  222.                     $this->datagrid->addItem($item);
  223.                 }
  224.             }
  225.             
  226.             // clear the crieteria to count the records
  227.             $criteria->resetProperties();
  228.             $count$repository->count($criteria);
  229.             
  230.             $this->pageNavigation->setCount($count); // count of records
  231.             $this->pageNavigation->setProperties($param); // order, page
  232.             $this->pageNavigation->setLimit($limit); // limit
  233.             
  234.             // closes the transaction
  235.             TTransaction::close();
  236.             $this->loaded true;
  237.         }
  238.         catch (Exception $e// in case of exception
  239.         {
  240.             // shows the exception genearated message
  241.             new TMessage('error''<b>Erro</b> ' $e->getMessage());
  242.             // rollback all the database operations 
  243.             TTransaction::rollback();
  244.         }
  245.     }
  246.     
  247.     /**
  248.      * define the standars seek parameters
  249.      */
  250.     public function onSetup($param=NULL)
  251.     {
  252.         // store the parameters in the section
  253.         TSession::setValue('tstandardseek_filter'NULL);
  254.         TSession::setValue('tstandardseek_display_value'NULL);
  255.         TSession::setValue('standard_seek_receive_key',   $param['receive_key']);
  256.         TSession::setValue('standard_seek_receive_field'$param['receive_field']);
  257.         TSession::setValue('standard_seek_display_field'$param['display_field']);
  258.         TSession::setValue('standard_seek_display_field2'$param['display_field2']);
  259.         TSession::setValue('standard_seek_model',         $param['model']);
  260.         TSession::setValue('standard_seek_database',      $param['database']);
  261.         TSession::setValue('standard_seek_parent',        $param['parent']);
  262.         if (isset($param['criteria']) AND $param['criteria'])
  263.         {
  264.             TSession::setValue('standard_seek_criteria',  unserialize(base64_decode($param['criteria'])));
  265.         }
  266.         $this->onReload();
  267.     }
  268.     
  269.     /**
  270.      * Select the register by ID and return the information to the main form
  271.      *     When using onblur signal, AJAX passes all needed parameters via GET
  272.      *     instead of calling onSetup before.
  273.      */
  274.     public static function onSelect($param)
  275.     {
  276.         $key $param['key'];
  277.         $database      = isset($param['database'])      ? $param['database'] : TSession::getValue('standard_seek_database');
  278.         $receive_key   = isset($param['receive_key'])   ? $param['receive_key']   : TSession::getValue('standard_seek_receive_key');
  279.         $receive_field = isset($param['receive_field']) ? $param['receive_field'] : TSession::getValue('standard_seek_receive_field');
  280.         $display_field = isset($param['display_field']) ? $param['display_field'] : TSession::getValue('standard_seek_display_field');
  281.         $parent        = isset($param['parent'])        ? $param['parent']        : TSession::getValue('standard_seek_parent');
  282.         
  283.         try
  284.         {
  285.             TTransaction::open($database);
  286.             // load the active record
  287.             $model = isset($param['model']) ? $param['model'] : TSession::getValue('standard_seek_model');
  288.             $activeRecord = new $model($key);
  289.             
  290.             $pk   constant("{$model}::PRIMARYKEY");
  291.             
  292.             $object = new StdClass;
  293.             $object->$receive_key   = isset($activeRecord->$pk) ? $activeRecord->$pk '';
  294.             $object->$receive_field = isset($activeRecord->$display_field) ? $activeRecord->$display_field '';
  295.             TTransaction::close();
  296.             
  297.             TForm::sendData($parent$object);
  298.             parent::closeWindow(); // closes the window
  299.         }
  300.         catch (Exception $e// in case of exception
  301.         {
  302.             // clear fields
  303.             $object = new StdClass;
  304.             $object->$receive_key   '';
  305.             $object->$receive_field '';
  306.             TForm::sendData($parent$object);
  307.             
  308.             // undo all pending operations
  309.             TTransaction::rollback();
  310.         }
  311.     }
  312. }
  313. ?>

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* :)