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:
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!).
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:
- <?php
- /**
- * Creates the DataGrid Structure
- */
- public function createModel( $create_header = true )
- {
- if (!$this->columns)
- {
- return;
- }
- if ($create_header)
- {
- $this->thead = new TElement('thead');
- $this->thead->{'class'} = 'tdatagrid_head';
- parent::add($this->thead);
- $row = new TElement('tr');
- if ($this->scrollable)
- {
- $this->thead->{'style'} = 'display:block';
- if ($this->hasCustomWidth())
- {
- $row->{'style'} = 'display: inline-table; width: 100%';
- }
- }
- $this->thead->add($row);
- $actions_count = count($this->actions) + count($this->action_groups);
- if ($actions_count >0)
- {
- for ($n=0; $n < $actions_count; $n++)
- {
- $cell = new TElement('th');
- $row->add($cell);
- $cell->add('<span style="width:calc('.$this->actionWidth.' - 2px);display:block"></span>');
- $cell->{'class'} = 'tdatagrid_action';
- $cell->{'style'} = 'padding:0';
- $cell->{'width'} = $this->actionWidth;
- }
- $cell->{'class'} = 'tdatagrid_col';
- }
- // add some cells for the data
- if ($this->columns)
- {
- // iterate the DataGrid columns
- foreach ($this->columns as $column)
- {
- // get the column properties
- $name = $column->getName();
- $label = ' '.$column->getLabel().' ';
- $align = $column->getAlign();
- $width = $column->getWidth();
- $props = $column->getProperties();
- if (isset($_GET['order']))
- {
- // START ALTERATION
- // if ($_GET['order'] == $name)
- if (($_GET['order'] == $name) || (strpos($_GET['order'], $name))) // LEONAM - 20-08-2018_12h05m
- // END ALTERATION
- {
- if (isset($_GET['direction']) AND $_GET['direction'] == 'asc')
- {
- $label .= '<span class="glyphicon glyphicon-chevron-down blue" aria-hidden="true"></span>';
- }
- else
- {
- $label .= '<span class="glyphicon glyphicon-chevron-up blue" aria-hidden="true"></span>';
- }
- }
- }
- // add a cell with the columns label
- $cell = new TElement('th');
- $row->add($cell);
- $cell->add($label);
- $cell->{'class'} = 'tdatagrid_col';
- $cell->{'style'} = "text-align:$align;user-select:none";
- if ($props)
- {
- foreach ($props as $prop_name => $prop_value)
- {
- $cell->$prop_name = $prop_value;
- }
- }
- if ($width)
- {
- $cell->{'width'} = (strpos($width, '%') !== false || strpos($width, 'px') !== false) ? $width : ($width + 8).'px';
- }
- // verify if the column has an attached action
- if ($column->getAction())
- {
- $action = $column->getAction();
- // START ALTERATION
- // if (isset($_GET['direction']) AND $_GET['direction'] == 'asc' AND isset($_GET['order']) AND ($_GET['order'] == $name))
- 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
- // END ALTERATION
- {
- $action->setParameter('direction', 'desc');
- }
- else
- {
- $action->setParameter('direction', 'asc');
- }
- $url = $action->serialize();
- $cell->{'href'} = $url;
- $cell->{'style'} .= ";cursor:pointer;";
- $cell->{'generator'} = 'adianti';
- }
- }
- if ($this->scrollable)
- {
- $cell = new TElement('td');
- $cell->{'class'} = 'tdatagrid_col';
- $row->add($cell);
- $cell->add('<span style="width:20px;display:block"></span>');
- $cell->{'style'} = 'padding:0';
- }
- }
- }
- // add one row to the DataGrid
- $this->tbody = new TElement('tbody');
- $this->tbody->{'class'} = 'tdatagrid_body';
- if ($this->scrollable)
- {
- $this->tbody->{'style'} = "height: {$this->height}px; display: block; overflow-y:scroll; overflow-x:hidden;";
- }
- parent::add($this->tbody);
- $this->modelCreated = TRUE;
- }
- ?>
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!).