Skip to content

Commit 407c108

Browse files
authored
Merge pull request #8054 from ping-yee/231018_generate_table
fix: [Table] field named `data` will produce bugged output
2 parents 6c788de + 4c0eab5 commit 407c108

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

system/View/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ protected function _prepArgs(array $args)
265265
// If there is no $args[0], skip this and treat as an associative array
266266
// This can happen if there is only a single key, for example this is passed to table->generate
267267
// array(array('foo'=>'bar'))
268-
if (isset($args[0]) && count($args) === 1 && is_array($args[0]) && ! isset($args[0]['data'])) {
268+
if (isset($args[0]) && count($args) === 1 && is_array($args[0])) {
269269
$args = $args[0];
270270
}
271271

tests/system/View/TableTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,64 @@ public static function orderedColumnUsecases(): iterable
816816
],
817817
];
818818
}
819+
820+
/**
821+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/8051
822+
*/
823+
public function testGenerateTableWithHeadingContainFieldNamedData(): void
824+
{
825+
$table = new Table();
826+
$table->setHeading([
827+
'codigo' => 'Codigo Orçamento',
828+
'data' => 'Data do Orçamento',
829+
'tipo_desconto' => 'Tipo de Desconto',
830+
'valor_desconto' => 'Valor do Desconto',
831+
])->setSyncRowsWithHeading(true);
832+
833+
$sampleData = [
834+
[
835+
'id' => 1,
836+
'id_cliente' => 1,
837+
'codigo' => 'codigo1',
838+
'data' => '2023-10-16 21:53:25',
839+
'tipo_desconto' => 'NENHUM',
840+
'valor_desconto' => '',
841+
'created_at' => '2023-10-16 21:53:25',
842+
'updated_at' => '2023-10-16 21:53:25',
843+
'deleted_at' => '',
844+
],
845+
[
846+
'id' => 2,
847+
'id_cliente' => 2,
848+
'codigo' => 'codigo2',
849+
'data' => '2023-10-16 21:53:25',
850+
'tipo_desconto' => 'REAL',
851+
'valor_desconto' => 10.00,
852+
'created_at' => '2023-10-16 21:53:25',
853+
'updated_at' => '2023-10-16 21:53:25',
854+
'deleted_at' => '',
855+
],
856+
[
857+
'id' => 3,
858+
'id_cliente' => 3,
859+
'codigo' => 'codigo3',
860+
'data' => '2023-10-16 21:53:25',
861+
'tipo_desconto' => 'PERCENTUAL',
862+
'valor_desconto' => 10.00,
863+
'created_at' => '2023-10-16 21:53:25',
864+
'updated_at' => '2023-10-16 21:53:25',
865+
'deleted_at' => '',
866+
],
867+
];
868+
869+
$generated = $table->generate($sampleData);
870+
871+
$this->assertStringContainsString('<th>Codigo Orçamento</th><th>Data do Orçamento</th><th>Tipo de Desconto</th><th>Valor do Desconto</th>', $generated);
872+
873+
$this->assertStringContainsString('<td>codigo1</td><td>2023-10-16 21:53:25</td><td>NENHUM</td><td></td>', $generated);
874+
$this->assertStringContainsString('<td>codigo2</td><td>2023-10-16 21:53:25</td><td>REAL</td><td>10</td>', $generated);
875+
$this->assertStringContainsString('<td>codigo3</td><td>2023-10-16 21:53:25</td><td>PERCENTUAL</td><td>10</td>', $generated);
876+
}
819877
}
820878

821879
// We need this for the _set_from_db_result() test

0 commit comments

Comments
 (0)