Lançado Adianti Framework 7.6!
Clique aqui para saber mais
TDataGrid com ordenação via CAST Migrando de um cliente Delphi/Firebird, me deparei com uma coluna PK com VARCHAR e com o conteúdo apenas numérico ('0001,'0002','4550'...'15025','20012', e por aí vai). Na migração já criei um novo campo INT sequencial para PK, mas para o cliente, o que vale são os códigos antigos. e esses são os que aparecem em todas as listagens. Ordená-los então, virou um problema. A TDataGrid tra...
LC
TDataGrid com ordenação via CAST  
Migrando de um cliente Delphi/Firebird, me deparei com uma coluna PK com VARCHAR e com o conteúdo apenas numérico ('0001,'0002','4550'...'15025','20012', e por aí vai). Na migração já criei um novo campo INT sequencial para PK, mas para o cliente, o que vale são os códigos antigos. e esses são os que aparecem em todas as listagens. Ordená-los então, virou um problema.

A TDataGrid trata das colunas com ordem pelo nome do campo. Para obter a ordenação por CAST(<nome_do_campo> AS INT), fiz o seguinte passo a passo:

1.) Criar uma nova classe TDataGridClone.php na 'lib/widget', com extends da TDataGrid original;
2.) Na __construct() da clone lembrar de chamar o parent::__construct();;
3.) Copiar toda a public function createModel da TDataGrid e alterar as linhas abaixo comentadas:

  1. <?php
  2.     /**
  3.      * Creates the DataGrid Structure
  4.      */
  5.     public function createModel$create_header true )
  6.     {
  7.         if (!$this->columns)
  8.         {
  9.             return;
  10.         }
  11.         if ($create_header)
  12.         {
  13.             $this->thead = new TElement('thead');
  14.             $this->thead->{'class'} = 'tdatagrid_head';
  15.             parent::add($this->thead);
  16.             $row = new TElement('tr');
  17.             if ($this->scrollable)
  18.             {
  19.                 $this->thead->{'style'} = 'display:block';
  20.                 if ($this->hasCustomWidth())
  21.                 {
  22.                     $row->{'style'} = 'display: inline-table; width: 100%';
  23.                 }
  24.             }
  25.             $this->thead->add($row);
  26.             $actions_count count($this->actions) + count($this->action_groups);
  27.             if ($actions_count >0)
  28.             {
  29.                 for ($n=0$n $actions_count$n++)
  30.                 {
  31.                     $cell = new TElement('th');
  32.                     $row->add($cell);
  33.                     $cell->add('<span style="width:calc('.$this->actionWidth.' - 2px);display:block"></span>');
  34.                     $cell->{'class'} = 'tdatagrid_action';
  35.                     $cell->{'style'} = 'padding:0';
  36.                     $cell->{'width'} = $this->actionWidth;
  37.                 }
  38.                 $cell->{'class'} = 'tdatagrid_col';
  39.             }
  40.             // add some cells for the data
  41.             if ($this->columns)
  42.             {
  43.                 // iterate the DataGrid columns
  44.                 foreach ($this->columns as $column)
  45.                 {
  46.                     // get the column properties
  47.                     $name  $column->getName();
  48.                     $label '&nbsp;'.$column->getLabel().'&nbsp;';
  49.                     $align $column->getAlign();
  50.                     $width $column->getWidth();
  51.                     $props $column->getProperties();
  52.                     if (isset($_GET['order']))
  53.                     {
  54. // START ALTERATION
  55.                         // if ($_GET['order'] == $name)
  56.                         if (($_GET['order'] == $name) || (strpos($_GET['order'], $name))) // LEONAM - 20-08-2018_12h05m
  57. // END ALTERATION
  58.                         {
  59.                             if (isset($_GET['direction']) AND $_GET['direction'] == 'asc')
  60.                             {
  61.                                 $label .= '<span class="glyphicon glyphicon-chevron-down blue" aria-hidden="true"></span>';
  62.                             }
  63.                             else
  64.                             {
  65.                                 $label .= '<span class="glyphicon glyphicon-chevron-up blue" aria-hidden="true"></span>';
  66.                             }
  67.                         }
  68.                     }
  69.                     // add a cell with the columns label
  70.                     $cell = new TElement('th');
  71.                     $row->add($cell);
  72.                     $cell->add($label);
  73.                     $cell->{'class'} = 'tdatagrid_col';
  74.                     $cell->{'style'} = "text-align:$align;user-select:none";
  75.                     if ($props)
  76.                     {
  77.                         foreach ($props as $prop_name => $prop_value)
  78.                         {
  79.                             $cell->$prop_name $prop_value;
  80.                         }
  81.                     }
  82.                     if ($width)
  83.                     {
  84.                         $cell->{'width'} = (strpos($width'%') !== false || strpos($width'px') !== false) ? $width : ($width 8).'px';
  85.                     }
  86.                     // verify if the column has an attached action
  87.                     if ($column->getAction())
  88.                     {
  89.                         $action $column->getAction();
  90. // START ALTERATION
  91.                         // if (isset($_GET['direction']) AND $_GET['direction'] == 'asc' AND isset($_GET['order']) AND ($_GET['order'] == $name))
  92.                         if (isset($_GET['direction']) AND $_GET['direction'] == 'asc' AND isset($_GET['order']) AND (($_GET['order'] == $name) || (strpos($_GET['order'], $name))))  // LEONAM - 20-08-2018_14h25m
  93. // END ALTERATION
  94.                         {
  95.                             $action->setParameter('direction''desc');
  96.                         }
  97.                         else
  98.                         {
  99.                             $action->setParameter('direction''asc');
  100.                         }
  101.                         $url    $action->serialize();
  102.                         $cell->{'href'}        = $url;
  103.                         $cell->{'style'}      .= ";cursor:pointer;";
  104.                         $cell->{'generator'}   = 'adianti';
  105.                     }
  106.                 }
  107.                 if ($this->scrollable)
  108.                 {
  109.                     $cell = new TElement('td');
  110.                     $cell->{'class'} = 'tdatagrid_col';
  111.                     $row->add($cell);
  112.                     $cell->add('<span style="width:20px;display:block"></span>');
  113.                     $cell->{'style'} = 'padding:0';
  114.                 }
  115.             }
  116.         }
  117.         // add one row to the DataGrid
  118.         $this->tbody = new TElement('tbody');
  119.         $this->tbody->{'class'} = 'tdatagrid_body';
  120.         if ($this->scrollable)
  121.         {
  122.             $this->tbody->{'style'} = "height: {$this->height}px; display: block; overflow-y:scroll; overflow-x:hidden;";
  123.         }
  124.         parent::add($this->tbody);
  125.         $this->modelCreated TRUE;
  126.     }
  127. ?>


Assim você não mexe no core do Adiant. Ah! Lembre que nas listagens ("*List.php") que for precisar usar o order com "CAST" de algum campo, altere a linha "$this->datagrid = new TDataGrid;" para "$this->datagrid = new TDataGridClone;"

No meu caso, funcionou (pelo menos até hoje! Migração é sempre uma incógnita!).

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