Lançado Adianti Framework 7.6!
Clique aqui para saber mais
datagrid->getRowIndex Boa noite galera! tenho uma datagrid onde implementei a função 'onShowDetail()', esse é o método que exibe detalhes das linhas da datagrid e usa a função $this->datagrid->getRowIndex() para recuperar a posição da linha na tabela... pois bem vamos ao problema, o método está funcionando corretamente somente quando ordeno os dados de forma crescente "asc", quando mudo para "desc" que é o...
BI
datagrid->getRowIndex  
Boa noite galera!
tenho uma datagrid onde implementei a função 'onShowDetail()', esse é o método que exibe detalhes das linhas da datagrid e usa a função $this->datagrid->getRowIndex() para recuperar a posição da linha na tabela...
pois bem vamos ao problema, o método está funcionando corretamente somente quando ordeno os dados de forma crescente "asc", quando mudo para "desc" que é o que me interessa e clico para exibir os detalhes, a função $this->datagrid->getRowIndex() retorna "null" e automaticamente a ordem dos dados é alterada para "asc". alguem já passou por isso? pode me ajudar? estou usando o template III.

  1. <?php
  2. class Ocorrencias extends TPage
  3. {
  4.     private $form// form
  5.     private $datagrid// listing
  6.     private $pageNavigation;
  7.     private $formgrid;
  8.     private $loaded;
  9.     private $deleteButton;
  10.     
  11.     /**
  12.      * Class constructor
  13.      * Creates the page, the form and the listing
  14.      */
  15.     public function __construct()
  16.     {
  17.         parent::__construct();
  18.         
  19.         // creates the form
  20.         $this->form = new TQuickForm('form_search_ViewOcorrenciasCliente');
  21.         $this->form->class 'tform'// change CSS class
  22.         $this->form = new BootstrapFormWrapper($this->form);
  23.         $this->form->style 'display: table;width:100%'// change style
  24.         $this->form->setFormTitle('ViewOcorrenciasCliente');
  25.         
  26.         // create the form fields
  27.         $id = new TEntry('id');
  28.         $data_coig = new TDate('data_coig');
  29.         $empreendimento = new TCombo('empreendimento');
  30.         $sigla_apl = new TCombo('sigla_apl');
  31.         $evento = new TDBCombo('evento','sqlserver','EventoGenerico','evento','evento');
  32.         $obs = new TEntry('obs');
  33.         
  34.         //carrega os empreendimentos do cliente
  35.         TTransaction::open('sqlserver');
  36.         $repository = new TRepository('ViewEmpreendimentocliente');
  37.         $criteria = new TCriteria;
  38.         //$log_id = TSession::getValue('login_id');
  39.         $criteria->add(new TFilter('systemuser_id''='TSession::getValue('login_id')));
  40.         $collection $repository->load($criteria);
  41.         $items = array();
  42.         foreach ($collection as $object)
  43.         {
  44.             $items[$object->empreendimento_id] = $object->sigla;
  45.         }
  46.         $empreendimento->addItems($items);
  47.         TTransaction::close();
  48.         
  49.         $id->setMask('999999999999');
  50.         
  51.         // set change action for empreendimento
  52.         $change_action = new TAction(array($this'onChange'));
  53.         $empreendimento->setChangeAction($change_action);
  54.         // add the fields
  55.         $this->form->addQuickField('Cód'$id,  '30%' );
  56.         $this->form->addQuickField('Data'$data_coig,  '47%' );
  57.         $this->form->addQuickField('Empreendimento'$empreendimento,  '50%' );
  58.         $this->form->addQuickField('Aplicação'$sigla_apl,  '50%' );
  59.         $this->form->addQuickField('Evento'$evento,  '50%' );
  60.         $this->form->addQuickField('Observação'$obs,  '50%' );
  61.         
  62.         // keep the form filled during navigation with session data
  63.         $this->form->setDataTSession::getValue('ViewOcorrenciasCliente_filter_data') );
  64.         
  65.         // add the search form actions
  66.         $this->form->addQuickAction(_t('Find'), new TAction(array($this'onSearch')), 'fa:search');
  67.         //$this->form->addQuickAction(_t('New'),  new TAction(array('', 'onEdit')), 'bs:plus-sign green');
  68.         
  69.         // creates a Datagrid
  70.         $this->datagrid = new TDataGrid;
  71.         //$this->datagrid = new BootstrapDatagridWrapper($this->datagrid);
  72.         $this->datagrid->style 'width: 100%';
  73.         $this->datagrid->setHeight(320);
  74.         //$this->datagrid->datatable = 'true';
  75.         //$this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
  76.         
  77.         // creates the datagrid columns
  78.         $column_id = new TDataGridColumn('id''Cód''center');
  79.         $column_data_coig = new TDataGridColumn('data_coig''Data''center');
  80.         $column_hora = new TDataGridColumn('hora''Hora''center');
  81.         $column_empreendimento = new TDataGridColumn('empreendimento''Empreendimento''left');
  82.         $column_sigla_apl = new TDataGridColumn('sigla_apl''Aplicação''center');
  83.         $column_evento = new TDataGridColumn('evento''Evento / Trip''left');
  84.         $column_obs = new TDataGridColumn('obs''Observação''left');
  85.         $column_imagem = new TDataGridColumn('imagem''Imagem''left');
  86.         // add the columns to the DataGrid
  87.         $this->datagrid->addColumn($column_id);
  88.         $this->datagrid->addColumn($column_data_coig);
  89.         $this->datagrid->addColumn($column_hora);
  90.         $this->datagrid->addColumn($column_empreendimento);
  91.         $this->datagrid->addColumn($column_sigla_apl);
  92.         $this->datagrid->addColumn($column_evento);
  93.         $this->datagrid->addColumn($column_obs);
  94.         $this->datagrid->addColumn($column_imagem);
  95.         
  96.         $column_id->setTransformer(array($this'formatRelevancia'));
  97.         // creates the datagrid column actions
  98.         $order_id = new TAction(array($this'onReload'));
  99.         $order_id->setParameter('order''id');
  100.         $column_id->setAction($order_id);
  101.         
  102.         $order_data_coig = new TAction(array($this'onReload'));
  103.         $order_data_coig->setParameter('order''data_coig');
  104.         $column_data_coig->setAction($order_data_coig);
  105.         
  106.         $order_hora = new TAction(array($this'onReload'));
  107.         $order_hora->setParameter('order''hora');
  108.         $column_hora->setAction($order_hora);
  109.         
  110.         // creates two datagrid actions
  111.         $action = new TDataGridAction(array($this'onShowDetail'));
  112.         $action->setLabel('Detalhes');
  113.         $action->setImage('ico_view.png');
  114.         $action->setField('id');
  115.         
  116.         // add the actions to the datagrid
  117.         $this->datagrid->addAction($action);
  118.         
  119.         // define the transformer method over image
  120.         $column_data_coig->setTransformer( function($value$object$row) {
  121.             $date = new DateTime($value);
  122.             return $date->format('d/m/Y');
  123.         });
  124.         // create EDIT action
  125.         //$action_edit = new TDataGridAction(array('', 'onEdit'));
  126.         //$action_edit->setUseButton(TRUE);
  127.         //$action_edit->setButtonClass('btn btn-default');
  128.         //$action_edit->setLabel(_t('Edit'));
  129.         //$action_edit->setImage('fa:pencil-square-o blue fa-lg');
  130.         //$action_edit->setField('id');
  131.         //$this->datagrid->addAction($action_edit);
  132.         
  133.         
  134.         // create the datagrid model
  135.         $this->datagrid->createModel();
  136.         
  137.         // creates the page navigation
  138.         $this->pageNavigation = new TPageNavigation;
  139.         $this->pageNavigation->setAction(new TAction(array($this'onReload')));
  140.         $this->pageNavigation->setWidth($this->datagrid->getWidth());
  141.         
  142.         // vertical box container
  143.         $container = new TVBox;
  144.         $container->style 'width: 90%';
  145.         $container->add(new TXMLBreadCrumb('menu.xml'__CLASS__));
  146.         $container->add(TPanelGroup::pack(''$this->form));
  147.         $container->add($this->datagrid);
  148.         $container->add($this->pageNavigation);
  149.         
  150.         parent::add($container);
  151.     }
  152.     
  153.     //transformar a linha "colorir as mais relevantes"
  154.     public function formatRelevancia($id$object$row)
  155.     {
  156.         if (PHP_SAPI !== 'cli'// just in the web version
  157.         {
  158.             if ($object->relevancia_id == 2)
  159.             {
  160.                 $row->style "color: blue";//relevancia media
  161.                 //return $relevancia_id;
  162.             }
  163.             elseif($object->relevancia_id == 3)
  164.             {
  165.                 $row->style "color: red";//relevancia alta
  166.                 //return $relevancia_id;
  167.             }
  168.             else
  169.             {
  170.                 $row->style "color: black";//relevancia normal 'background'
  171.                 
  172.             }
  173.             return $id;
  174.         }
  175.     }
  176.     
  177.     /**
  178.      DETALHES 
  179.      */
  180.     public function onShowDetail$param )
  181.     {
  182.         // get row position
  183.         $pos $this->datagrid->getRowIndex('id'$param['key']);
  184.         
  185.         var_dump($pos);
  186.         //var_dump($param);
  187.         if ($pos >= 0){
  188.             TTransaction::open('sqlserver');
  189.             $objeto = new ViewOcorrenciasGerais($param['key']);
  190.             $user_post = new SystemUser($objeto->systemuser_id);
  191.             $user_edit = new SystemUser($objeto->user_edicao);
  192.             
  193.             // get row by position
  194.             $current_row $this->datagrid->getRow($pos);
  195.             $current_row->style "background-color: #43CD80; color:white; text-shadow:none";
  196.             
  197.             // create a new row
  198.             if ($objeto->validador == 1){
  199.                 $row3 = new TTableRow;
  200.                 $row3->style "background-color: #E0DEF8; color: black";
  201.                     $cell $row3->addCell("<b><p style='color: red'>Ocorrência Validada!</p></b> ");
  202.                     $cell->colspan 9;
  203.                 $this->datagrid->insert($pos +1$row3);
  204.             }
  205.             
  206.             // create a new row
  207.             if ($objeto->user_edicao){
  208.                 $row2 = new TTableRow;
  209.                 $row2->style "background-color: #E0DEF8; color: black";
  210.                     $cell $row2->addCell("<b>Atualizado por:</b> "ucfirst($objeto->editor).", <b>na data:</b> ".date('d/m/y H:i:s'strtotime($objeto->data_edicao)));
  211.                     $cell->colspan 9;
  212.                 $this->datagrid->insert($pos +1$row2);
  213.             }
  214.             
  215.             $row1 = new TTableRow;
  216.             $row1->style "background-color: #E0DEF8; color: black";
  217.                 $cell $row1->addCell("<b>Postado por:</b> "ucfirst($objeto->user_post). ", <b>na data:</b> ".date('d/m/y H:i:s'strtotime($objeto->agora)));
  218.                 $cell->colspan 9;
  219.             $this->datagrid->insert($pos +1$row1);
  220.             
  221.             if ($objeto->nivel_montante and $objeto->potencia){
  222.                 $row = new TTableRow;
  223.                 $row->style "background-color: #E0DEF8; color: black";
  224.                     $cell $row->addCell("<b>Nível Montante:</b> $objeto->nivel_montante m. <b>- Potência ativa:</b> $objeto->potencia kW.");
  225.                     $cell->colspan 9;
  226.     
  227.                 $this->datagrid->insert($pos +1$row);
  228.             }  elseif ($objeto->nivel_montante and empty ($objeto->potencia)) {
  229.                 $row = new TTableRow;
  230.                 $row->style "background-color: #E0DEF8; color: black";
  231.                     $cell $row->addCell("<b>Nível Montante:</b> $objeto->nivel_montante m.");
  232.                     $cell->colspan 9;
  233.     
  234.                 $this->datagrid->insert($pos +1$row);
  235.             }  elseif (empty($objeto->nivel_montante) and  $objeto->potencia) {
  236.                 $row = new TTableRow;
  237.                 $row->style "background-color: #E0DEF8; color: black";
  238.                     $cell $row->addCell("<b>Potência ativa:</b> $objeto->potencia kW.");
  239.                     $cell->colspan 9;
  240.     
  241.                 $this->datagrid->insert($pos +1$row);
  242.             }  
  243.             if (!empty($objeto->evento_real_id)) {
  244.                 $real = new EventoReal($objeto->evento_real_id);
  245.                 $row = new TTableRow;
  246.                 $row->style "background-color: #E0DEF8; color: black";
  247.                     $cell $row->addCell("<b>Evendo Real: </b>$real->evento ");
  248.                     $cell->colspan 9;
  249.     
  250.                 $this->datagrid->insert($pos +1$row);
  251.             }
  252.             
  253.             
  254.             TTransaction::close();
  255.         }
  256.     }
  257.     
  258.     /**
  259.      * Register the filter in the session
  260.      */
  261.     public function onSearch()
  262.     {
  263.         // get the search form data
  264.         $data $this->form->getData();
  265.         
  266.         // clear session filters
  267.         TSession::setValue('Ocorrencias_filter_id',   NULL);
  268.         TSession::setValue('Ocorrencias_filter_data_coig',   NULL);
  269.         TSession::setValue('Ocorrencias_filter_empreendimento',   NULL);
  270.         TSession::setValue('Ocorrencias_filter_sigla_apl',   NULL);
  271.         TSession::setValue('Ocorrencias_filter_evento',   NULL);
  272.         TSession::setValue('Ocorrencias_filter_obs',   NULL);
  273.         if (isset($data->id) AND ($data->id)) {
  274.             $filter = new TFilter('id''='"$data->id"); // create the filter
  275.             TSession::setValue('Ocorrencias_filter_id',   $filter); // stores the filter in the session
  276.         }
  277.         if (isset($data->data_coig) AND ($data->data_coig)) {
  278.             $filter = new TFilter('data_coig''='"$data->data_coig"); // create the filter
  279.             TSession::setValue('Ocorrencias_filter_data_coig',   $filter); // stores the filter in the session
  280.         }
  281.         if (isset($data->empreendimento) AND ($data->empreendimento)) {
  282.             $filter = new TFilter('empreendimento_id''='$data->empreendimento); // create the filter
  283.             TSession::setValue('Ocorrencias_filter_empreendimento',   $filter); // stores the filter in the session
  284.         }
  285.         if (isset($data->sigla_apl) AND ($data->sigla_apl)) {
  286.             $filter = new TFilter('aplicacao_id''='$data->sigla_apl); // create the filter
  287.             TSession::setValue('Ocorrencias_filter_sigla_apl',   $filter); // stores the filter in the session
  288.         }
  289.         if (isset($data->evento) AND ($data->evento)) {
  290.             $evento trim($data->evento);
  291.             $filter = new TFilter('evento''like'"%{$evento}%" ); // create the filter
  292.             TSession::setValue('Ocorrencias_filter_evento',   $filter); // stores the filter in the session
  293.         }
  294.         if (isset($data->obs) AND ($data->obs)) {
  295.             $filter = new TFilter('obs''like'"%{$data->obs}%"); // create the filter
  296.             TSession::setValue('Ocorrencias_filter_obs',   $filter); // stores the filter in the session
  297.         }
  298.         
  299.         // fill the form with data again
  300.         $this->form->setData($data);
  301.         TForm::sendData('form_search_ViewOcorrenciasCliente'$data);
  302.         
  303.         // keep the search data in the session
  304.         TSession::setValue('ViewOcorrenciasCliente_filter_data'$data);
  305.         
  306.         $param=array();
  307.         $param['offset']    =0;
  308.         $param['first_page']=1;
  309.         $this->onReload($param);
  310.     }
  311.     
  312.     /**
  313.      * Load the datagrid with data
  314.      */
  315.     public function onReload($param NULL)
  316.     {
  317.         try
  318.         {
  319.             
  320.             // open a transaction with database 'sqlserver'
  321.             TTransaction::open('sqlserver');
  322.             
  323.             // creates a repository for ViewOcorrenciasCliente
  324.             $repository = new TRepository('ViewOcorrenciasCliente');
  325.             $limit 30;
  326.             // creates a criteria
  327.             $criteria = new TCriteria;
  328.             
  329.             // default order
  330.             if (empty($param['order']))
  331.             {
  332.                 $param['order'] = 'id';
  333.                 $param['direction'] = 'desc';
  334.             }
  335.             $criteria->setProperties($param); // order, offset
  336.             $criteria->setProperty('limit'$limit);
  337.             
  338.             if (TSession::getValue('Ocorrencias_filter_id')) {
  339.                 $criteria->add(TSession::getValue('Ocorrencias_filter_id')); // add the session filter
  340.             }
  341.             if (TSession::getValue('Ocorrencias_filter_data_coig')) {
  342.                 $criteria->add(TSession::getValue('Ocorrencias_filter_data_coig')); // add the session filter
  343.             }
  344.             if (TSession::getValue('Ocorrencias_filter_empreendimento')) {
  345.                 $criteria->add(TSession::getValue('Ocorrencias_filter_empreendimento')); // add the session filter
  346.             }
  347.             if (TSession::getValue('Ocorrencias_filter_sigla_apl')) {
  348.                 $criteria->add(TSession::getValue('Ocorrencias_filter_sigla_apl')); // add the session filter
  349.             }
  350.             if (TSession::getValue('Ocorrencias_filter_evento')) {
  351.                 $criteria->add(TSession::getValue('Ocorrencias_filter_evento')); // add the session filter
  352.             }
  353.             if (TSession::getValue('Ocorrencias_filter_obs')) {
  354.                 $criteria->add(TSession::getValue('Ocorrencias_filter_obs')); // add the session filter
  355.             }
  356.             
  357.             $log_id TSession::getValue('login_id');
  358.             $criteria->add( new TFilter'id_cliente''='$log_id ));
  359.             
  360.             // load the objects according to criteria
  361.             $objects $repository->load($criteriaFALSE);
  362.             
  363.             if (is_callable($this->transformCallback))
  364.             {
  365.                 call_user_func($this->transformCallback$objects$param);
  366.             }
  367.             
  368.             $this->datagrid->clear();
  369.             $this->datagrid->disableDefaultClick();
  370.             if ($objects)
  371.             {
  372.                 // iterate the collection of active records
  373.                 foreach ($objects as $object)
  374.                 {
  375.                     //$object->data_coig = date("d/m/y", strtotime($object->data_coig));
  376.                     $object->hora date("H:i:s"strtotime($object->hora));
  377.                     
  378.                     if ($object->imagem){
  379.                         //coloca o botão com a classe modal para abrir uma imagem
  380.                         $teste "";
  381.                         $teste $object->imagem;
  382.                         $object->imagem "<div class='container' style='width: 20px'>
  383.                                       <div class='row text-center' >
  384.                                           <a href='#' class='btn btn-lg btn-success' data-toggle='modal' data-target='#ocorrencia-$object->id' style='background: #ccc; border: green 1px solid; color: black; font-size: 12px;'><img style='width: 35px; height: 30px;'  src='$teste'></a>
  385.                                       </div>
  386.                                       
  387.                                     </div>
  388.                                     
  389.                                     <div class='modal fade' id='ocorrencia-$object->id' tabindex='-1' role='dialog' aria-labelledby='basicModal' aria-hidden='true'>
  390.                                       <div class='modal-dialog' style='width: 1000px; height: 800px;'>
  391.                                         <div class='modal-content'>
  392.                                           <div class='modal-header'>
  393.                                             <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
  394.                                             
  395.                                           </div>
  396.                                           <div class='modal-body' >
  397.                                             <img  src='$teste'>
  398.                                           </div>
  399.                                           
  400.                                         </div>
  401.                                       </div>
  402.                                     </div>";
  403.                     }
  404.                     // add the object inside the datagrid
  405.                     $this->datagrid->addItem($object);
  406.                 }
  407.             }
  408.             
  409.             // reset the criteria for record count
  410.             $criteria->resetProperties();
  411.             $count$repository->count($criteria);
  412.             
  413.             $this->pageNavigation->setCount($count); // count of records
  414.             $this->pageNavigation->setProperties($param); // order, page
  415.             $this->pageNavigation->setLimit($limit); // limit
  416.             
  417.             // close the transaction
  418.             TTransaction::close();
  419.             $this->loaded true;
  420.         }
  421.         catch (Exception $e// in case of exception
  422.         {
  423.             // shows the exception error message
  424.             new TMessage('error'$e->getMessage());
  425.             // undo all pending operations
  426.             TTransaction::rollback();
  427.         }
  428.         
  429.     }
  430.     
  431.     /**
  432.      * Ask before deletion
  433.      */
  434.     public function onDelete($param)
  435.     {
  436.         // define the delete action
  437.         $action = new TAction(array($this'Delete'));
  438.         $action->setParameters($param); // pass the key parameter ahead
  439.         
  440.         // shows a dialog to the user
  441.         new TQuestion(AdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  442.     }
  443.     
  444.     /**
  445.      * Delete a record
  446.      */
  447.     public function Delete($param)
  448.     {
  449.         try
  450.         {
  451.             $key=$param['key']; // get the parameter $key
  452.             TTransaction::open('sqlserver'); // open a transaction with database
  453.             $object = new ViewOcorrenciasCliente($keyFALSE); // instantiates the Active Record
  454.             $object->delete(); // deletes the object from the database
  455.             TTransaction::close(); // close the transaction
  456.             $this->onReload$param ); // reload the listing
  457.             new TMessage('info'AdiantiCoreTranslator::translate('Record deleted')); // success message
  458.         }
  459.         catch (Exception $e// in case of exception
  460.         {
  461.             new TMessage('error'$e->getMessage()); // shows the exception error message
  462.             TTransaction::rollback(); // undo all pending operations
  463.         }
  464.     }
  465.     
  466.     public static function onChange($param)
  467.     {
  468.         if (!empty($param['empreendimento'])){
  469.             TTransaction::open('sqlserver');
  470.             $criteria = new TCriteria;
  471.             $criteria->add(new TFilter('empreendimento_id''='$param['empreendimento']));
  472.             //$criteria->add(new TFilter('aplicacao', 'like', '%gerador%'));
  473.             
  474.             $repository = new TRepository('ViewEmpreendimentos');
  475.             $aplicacaos $repository->load($criteria);
  476.             
  477.             $options = array();
  478.             $options[] = '' ;
  479.             foreach ($aplicacaos as $aplicacao){
  480.                 $options[$aplicacao->id] = $aplicacao->sigla;
  481.             }
  482.             
  483.             //TForm::sendData('form_Vertedouro', $objeto);            
  484.             TCombo::reload('form_search_ViewOcorrenciasCliente''sigla_apl'$options);
  485.         }
  486.     }
  487.     
  488.     /**
  489.      * method show()
  490.      * Shows the page
  491.      */
  492.     public function show()
  493.     {
  494.         // check if the datagrid is already loaded
  495.         if (!$this->loaded AND (!isset($_GET['method']) OR !(in_array($_GET['method'],  array('onReload''onSearch')))) )
  496.         {
  497.             if (func_num_args() > 0)
  498.             {
  499.                 $this->onReloadfunc_get_arg(0) );
  500.             }
  501.             else
  502.             {
  503.                 $this->onReload();
  504.             }
  505.         }
  506.         parent::show();
  507.     }
  508. }
  509. ?>

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)


AE

Buenas.
Conseguiste resolver?
Estou na mesma situação com a versão 5.7 do framework.