From c7b02d89eb573cf561ff73ff93d6c34f7e947298 Mon Sep 17 00:00:00 2001 From: Christian Rumpf Date: Tue, 4 Apr 2023 00:33:15 +0200 Subject: [PATCH 01/13] data row columns optional order by heading keys --- system/View/Table.php | 48 +++++++++++++++++++++++++++-- tests/system/View/TableTest.php | 53 +++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 2 deletions(-) diff --git a/system/View/Table.php b/system/View/Table.php index a7ded4fd5cb7..a7a730b667dc 100644 --- a/system/View/Table.php +++ b/system/View/Table.php @@ -83,6 +83,13 @@ class Table */ public $function; + /** + * Order each inserted row by heading keys + * + * @var bool + */ + public bool $rowKeysSyncWithHeadingKeys = false; + /** * Set the template from the table config file if it exists * @@ -162,6 +169,7 @@ public function makeColumns($array = [], $columnLimit = 0) // Turn off the auto-heading feature since it's doubtful we // will want headings from a one-dimensional array $this->autoHeading = false; + $this->rowKeysSyncWithHeadingKeys = false; if ($columnLimit === 0) { return $array; @@ -207,7 +215,42 @@ public function setEmpty($value) */ public function addRow() { - $this->rows[] = $this->_prepArgs(func_get_args()); + $tmpRow = $this->_prepArgs(func_get_args()); + + if ($this->rowKeysSyncWithHeadingKeys && !empty($this->heading)) { + // each key has an index + $keyIndex = array_flip(array_keys($this->heading)); + + // figure out which keys need to be added + $missingKeys = array_diff_key($keyIndex, $tmpRow); + + // Remove all keys which don't exist in $keyIndex + $tmpRow = array_filter($tmpRow, static fn($k) => array_key_exists($k, $keyIndex), ARRAY_FILTER_USE_KEY); + + // add missing keys to row, but use $this->emptyCells + $tmpRow = array_merge($tmpRow, array_map(fn($v) => ['data' => $this->emptyCells], $missingKeys)); + + // order keys by $keyIndex values + uksort($tmpRow, static fn($k1, $k2) => $keyIndex[$k1] <=> $keyIndex[$k2]); + } + $this->rows[] = $tmpRow; + + return $this; + } + + /** + * Set to true if each row column should be synced by keys defined in heading. + * + * If a row has a key which does not exist in heading, it will be filtered out + * If a row does not have a key which exists in heading, the field will stay empty + * + * @param bool $orderByKey + * + * @return Table + */ + public function setSyncRowKeysWithHeadingKeys(bool $orderByKey): Table + { + $this->rowKeysSyncWithHeadingKeys = $orderByKey; return $this; } @@ -436,7 +479,8 @@ protected function _setFromArray($data) } foreach ($data as &$row) { - $this->rows[] = $this->_prepArgs($row); + $this->addRow($row); + //$this->rows[] = $this->_prepArgs($row); } } diff --git a/tests/system/View/TableTest.php b/tests/system/View/TableTest.php index 6e56d2a3fe22..71481ea73717 100644 --- a/tests/system/View/TableTest.php +++ b/tests/system/View/TableTest.php @@ -763,6 +763,59 @@ public function testInvalidCallback() $this->assertStringContainsString('FredBlueSmall', $generated); } + + /** + * @dataProvider orderedColumnUsecases + */ + public function testAddRowAndGenerateWithOrderedColumns(array $heading, array $row, string $expectContainsString): void + { + $this->table->setHeading($heading); + $this->table->setSyncRowKeysWithHeadingKeys(true); + $this->table->addRow($row); + + $generated = $this->table->generate(); + + $this->assertStringContainsString($expectContainsString, $generated); + } + + /** + * @dataProvider orderedColumnUsecases + */ + public function testGenerateDataWithOrderedColumns(array $heading, array $row, string $expectContainsString): void + { + $this->table->setHeading($heading); + $this->table->setSyncRowKeysWithHeadingKeys(true); + + $generated = $this->table->generate([$row]); + + $this->assertStringContainsString($expectContainsString, $generated); + } + + public function orderedColumnUsecases(): array + { + return [ + [ + 'heading' => ['id' => 'ID', 'name' => 'Name', 'age' => 'Age'], + 'row' => ['name' => 'Max', 'age' => 30, 'id' => 5], + 'expectContainsString' => '5Max30' + ], + [ + 'heading' => ['id' => 'ID', 'age' => 'Age', 'name' => 'Name'], + 'row' => ['name' => 'Fred', 'age' => 30, 'id' => 5], + 'expectContainsString' => '530Fred' + ], + [ + 'heading' => ['id' => 'ID', 'name' => 'Name'], + 'row' => ['name' => 'Fred', 'age' => 30, 'id' => 5], + 'expectContainsString' => '5Fred' + ], + [ + 'heading' => ['id' => 'ID', 'age' => 'Age', 'name' => 'Name'], + 'row' => ['name' => 'Fred', 'id' => 5], + 'expectContainsString' => '5Fred' + ] + ]; + } } // We need this for the _set_from_db_result() test From f420ad98ae6aad357fe91fa86fc6a04e2fdf26e7 Mon Sep 17 00:00:00 2001 From: Christian Rumpf Date: Tue, 4 Apr 2023 20:36:19 +0200 Subject: [PATCH 02/13] Docu, CS fixes --- system/View/Table.php | 19 +++------- tests/system/View/TableTest.php | 24 ++++++------ user_guide_src/source/outgoing/table.rst | 26 +++++++++++++ user_guide_src/source/outgoing/table/019.php | 40 ++++++++++++++++++++ 4 files changed, 84 insertions(+), 25 deletions(-) create mode 100644 user_guide_src/source/outgoing/table/019.php diff --git a/system/View/Table.php b/system/View/Table.php index a7a730b667dc..94373f360d0e 100644 --- a/system/View/Table.php +++ b/system/View/Table.php @@ -85,8 +85,6 @@ class Table /** * Order each inserted row by heading keys - * - * @var bool */ public bool $rowKeysSyncWithHeadingKeys = false; @@ -168,7 +166,7 @@ public function makeColumns($array = [], $columnLimit = 0) // Turn off the auto-heading feature since it's doubtful we // will want headings from a one-dimensional array - $this->autoHeading = false; + $this->autoHeading = false; $this->rowKeysSyncWithHeadingKeys = false; if ($columnLimit === 0) { @@ -217,7 +215,7 @@ public function addRow() { $tmpRow = $this->_prepArgs(func_get_args()); - if ($this->rowKeysSyncWithHeadingKeys && !empty($this->heading)) { + if ($this->rowKeysSyncWithHeadingKeys && ! empty($this->heading)) { // each key has an index $keyIndex = array_flip(array_keys($this->heading)); @@ -225,13 +223,13 @@ public function addRow() $missingKeys = array_diff_key($keyIndex, $tmpRow); // Remove all keys which don't exist in $keyIndex - $tmpRow = array_filter($tmpRow, static fn($k) => array_key_exists($k, $keyIndex), ARRAY_FILTER_USE_KEY); + $tmpRow = array_filter($tmpRow, static fn ($k) => array_key_exists($k, $keyIndex), ARRAY_FILTER_USE_KEY); // add missing keys to row, but use $this->emptyCells - $tmpRow = array_merge($tmpRow, array_map(fn($v) => ['data' => $this->emptyCells], $missingKeys)); + $tmpRow = array_merge($tmpRow, array_map(fn ($v) => ['data' => $this->emptyCells], $missingKeys)); // order keys by $keyIndex values - uksort($tmpRow, static fn($k1, $k2) => $keyIndex[$k1] <=> $keyIndex[$k2]); + uksort($tmpRow, static fn ($k1, $k2) => $keyIndex[$k1] <=> $keyIndex[$k2]); } $this->rows[] = $tmpRow; @@ -240,13 +238,9 @@ public function addRow() /** * Set to true if each row column should be synced by keys defined in heading. - * + * * If a row has a key which does not exist in heading, it will be filtered out * If a row does not have a key which exists in heading, the field will stay empty - * - * @param bool $orderByKey - * - * @return Table */ public function setSyncRowKeysWithHeadingKeys(bool $orderByKey): Table { @@ -480,7 +474,6 @@ protected function _setFromArray($data) foreach ($data as &$row) { $this->addRow($row); - //$this->rows[] = $this->_prepArgs($row); } } diff --git a/tests/system/View/TableTest.php b/tests/system/View/TableTest.php index 71481ea73717..da567b63dbd6 100644 --- a/tests/system/View/TableTest.php +++ b/tests/system/View/TableTest.php @@ -794,24 +794,24 @@ public function testGenerateDataWithOrderedColumns(array $heading, array $row, s public function orderedColumnUsecases(): array { return [ - [ - 'heading' => ['id' => 'ID', 'name' => 'Name', 'age' => 'Age'], - 'row' => ['name' => 'Max', 'age' => 30, 'id' => 5], + 'reorder example #1' => [ + 'heading' => ['id' => 'ID', 'name' => 'Name', 'age' => 'Age'], + 'row' => ['name' => 'Max', 'age' => 30, 'id' => 5], 'expectContainsString' => '5Max30' ], - [ - 'heading' => ['id' => 'ID', 'age' => 'Age', 'name' => 'Name'], - 'row' => ['name' => 'Fred', 'age' => 30, 'id' => 5], + 'reorder example #2' => [ + 'heading' => ['id' => 'ID', 'age' => 'Age', 'name' => 'Name'], + 'row' => ['name' => 'Fred', 'age' => 30, 'id' => 5], 'expectContainsString' => '530Fred' ], - [ - 'heading' => ['id' => 'ID', 'name' => 'Name'], - 'row' => ['name' => 'Fred', 'age' => 30, 'id' => 5], + '2 col heading, 3 col data row' => [ + 'heading' => ['id' => 'ID', 'name' => 'Name'], + 'row' => ['name' => 'Fred', 'age' => 30, 'id' => 5], 'expectContainsString' => '5Fred' ], - [ - 'heading' => ['id' => 'ID', 'age' => 'Age', 'name' => 'Name'], - 'row' => ['name' => 'Fred', 'id' => 5], + '3 col heading, 2 col data row' => [ + 'heading' => ['id' => 'ID', 'age' => 'Age', 'name' => 'Name'], + 'row' => ['name' => 'Fred', 'id' => 5], 'expectContainsString' => '5Fred' ] ]; diff --git a/user_guide_src/source/outgoing/table.rst b/user_guide_src/source/outgoing/table.rst index 0ccf8da0904d..8b876ac37e85 100644 --- a/user_guide_src/source/outgoing/table.rst +++ b/user_guide_src/source/outgoing/table.rst @@ -71,6 +71,23 @@ to the Table constructor: .. literalinclude:: table/008.php +Synchronizing row columns with heading +====================================== + +.. versionadded:: 4.4.0 + +``setSyncRowKeysWithHeadingKeys(true)`` enables that each data value +is placed in the same column as defined in ``setHeading()`` if an +associative array was used as parameter. This is especially useful +when dealing with data loaded via REST API where the order is not to +your liking, or if the API returned too much data. + +If a data row contains a ``key`` which does not exist the heading, +the value will be filtered out. Vise versa if a data row does not have key +mentioned in heading, it places an empty cell for that spot. + +.. literalinclude:: table/019.php + *************** Class Reference *************** @@ -188,3 +205,12 @@ Class Reference Example .. literalinclude:: table/018.php + + .. php:method:: setSyncRowKeysWithHeadingKeys(bool $orderByKey) + + :returns: Table instance (method chaining) + :rtype: Table + + Enables each row column to be ordered by heading keys. This gives + more control of how data is displayed in the final table. Make + sure to set this value before calling the first ``addRow()`` method. diff --git a/user_guide_src/source/outgoing/table/019.php b/user_guide_src/source/outgoing/table/019.php new file mode 100644 index 000000000000..2edcac9c0197 --- /dev/null +++ b/user_guide_src/source/outgoing/table/019.php @@ -0,0 +1,40 @@ +setHeading(['name' => 'Name', 'color' => 'Color', 'size' => 'Size']) + ->setSyncRowKeysWithHeadingKeys(true) + ->addRow(['color' => 'Blue', 'name' => 'Fred', 'size' => 'Small']) + ->addRow(['size' => 'Large', 'age' => '24', 'name' => 'Mary']) + ->addRow(['color' => 'Green']); + +echo $table->generate(); +?> + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameColorSize
FredBlueSmall
MaryLarge
Green
\ No newline at end of file From bde7c42e416686d3f647a9adf9231367dcc3b8d1 Mon Sep 17 00:00:00 2001 From: Christian Rumpf Date: Tue, 4 Apr 2023 20:39:54 +0200 Subject: [PATCH 03/13] More CS fixes (trailing comma in array) --- tests/system/View/TableTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/system/View/TableTest.php b/tests/system/View/TableTest.php index da567b63dbd6..5c574f2989a1 100644 --- a/tests/system/View/TableTest.php +++ b/tests/system/View/TableTest.php @@ -797,23 +797,23 @@ public function orderedColumnUsecases(): array 'reorder example #1' => [ 'heading' => ['id' => 'ID', 'name' => 'Name', 'age' => 'Age'], 'row' => ['name' => 'Max', 'age' => 30, 'id' => 5], - 'expectContainsString' => '5Max30' + 'expectContainsString' => '5Max30', ], 'reorder example #2' => [ 'heading' => ['id' => 'ID', 'age' => 'Age', 'name' => 'Name'], 'row' => ['name' => 'Fred', 'age' => 30, 'id' => 5], - 'expectContainsString' => '530Fred' + 'expectContainsString' => '530Fred', ], '2 col heading, 3 col data row' => [ 'heading' => ['id' => 'ID', 'name' => 'Name'], 'row' => ['name' => 'Fred', 'age' => 30, 'id' => 5], - 'expectContainsString' => '5Fred' + 'expectContainsString' => '5Fred', ], '3 col heading, 2 col data row' => [ 'heading' => ['id' => 'ID', 'age' => 'Age', 'name' => 'Name'], 'row' => ['name' => 'Fred', 'id' => 5], - 'expectContainsString' => '5Fred' - ] + 'expectContainsString' => '5Fred', + ], ]; } } From 90dfeee6ed6dee8b4361f284133960fc3a81d016 Mon Sep 17 00:00:00 2001 From: Christian Rumpf Date: Tue, 4 Apr 2023 22:59:20 +0200 Subject: [PATCH 04/13] Docu update with more example --- user_guide_src/source/outgoing/table.rst | 9 +++++++ user_guide_src/source/outgoing/table/020.php | 25 ++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 user_guide_src/source/outgoing/table/020.php diff --git a/user_guide_src/source/outgoing/table.rst b/user_guide_src/source/outgoing/table.rst index 8b876ac37e85..ce61a026697c 100644 --- a/user_guide_src/source/outgoing/table.rst +++ b/user_guide_src/source/outgoing/table.rst @@ -88,6 +88,15 @@ mentioned in heading, it places an empty cell for that spot. .. literalinclude:: table/019.php +.. important:: You must call ``setSyncRowKeysWithHeadingKeys(true)`` and + ``setHeading([...])`` before adding any rows via ``addRow([...])`` where + the rearrangement of columns takes place. + +You get the same result by using the result array is input in ``generate()`` + +.. literalinclude:: table/020.php + + *************** Class Reference *************** diff --git a/user_guide_src/source/outgoing/table/020.php b/user_guide_src/source/outgoing/table/020.php new file mode 100644 index 000000000000..5cfe457f523b --- /dev/null +++ b/user_guide_src/source/outgoing/table/020.php @@ -0,0 +1,25 @@ + 'Blue', + 'name' => 'Fred', + 'size' => 'Small', + ], + [ + 'size' => 'Large', + 'age' => '24', + 'name' => 'Mary', + ], + [ + 'color' => 'Green', + ], +]; + +$table = new \CodeIgniter\View\Table(); + +$table->setHeading(['name' => 'Name', 'color' => 'Color', 'size' => 'Size']) + ->setSyncRowKeysWithHeadingKeys(true); + +echo $table->generate($data); +?> \ No newline at end of file From 012bbf6b319a25e043490a9504c13781c3054797 Mon Sep 17 00:00:00 2001 From: Christian Rumpf Date: Tue, 4 Apr 2023 23:03:10 +0200 Subject: [PATCH 05/13] Fixing CS in docu --- user_guide_src/source/outgoing/table/020.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/user_guide_src/source/outgoing/table/020.php b/user_guide_src/source/outgoing/table/020.php index 5cfe457f523b..fe7c123a475a 100644 --- a/user_guide_src/source/outgoing/table/020.php +++ b/user_guide_src/source/outgoing/table/020.php @@ -7,9 +7,9 @@ 'size' => 'Small', ], [ - 'size' => 'Large', - 'age' => '24', - 'name' => 'Mary', + 'size' => 'Large', + 'age' => '24', + 'name' => 'Mary', ], [ 'color' => 'Green', From d6099ff5637d5b13e1bc49decaba3146da1f084b Mon Sep 17 00:00:00 2001 From: Christian Rumpf Date: Tue, 4 Apr 2023 23:06:40 +0200 Subject: [PATCH 06/13] Fixing CS in docu --- user_guide_src/source/outgoing/table/020.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/user_guide_src/source/outgoing/table/020.php b/user_guide_src/source/outgoing/table/020.php index fe7c123a475a..c34d97ffd3eb 100644 --- a/user_guide_src/source/outgoing/table/020.php +++ b/user_guide_src/source/outgoing/table/020.php @@ -21,5 +21,4 @@ $table->setHeading(['name' => 'Name', 'color' => 'Color', 'size' => 'Size']) ->setSyncRowKeysWithHeadingKeys(true); -echo $table->generate($data); -?> \ No newline at end of file +echo $table->generate($data); \ No newline at end of file From 0e4f7936030fddaaf7e85edec0c4bd93c943ae3b Mon Sep 17 00:00:00 2001 From: Christian Rumpf Date: Tue, 4 Apr 2023 23:09:46 +0200 Subject: [PATCH 07/13] Fixing CS in docu --- user_guide_src/source/outgoing/table/020.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/outgoing/table/020.php b/user_guide_src/source/outgoing/table/020.php index c34d97ffd3eb..afada80c8d3c 100644 --- a/user_guide_src/source/outgoing/table/020.php +++ b/user_guide_src/source/outgoing/table/020.php @@ -21,4 +21,4 @@ $table->setHeading(['name' => 'Name', 'color' => 'Color', 'size' => 'Size']) ->setSyncRowKeysWithHeadingKeys(true); -echo $table->generate($data); \ No newline at end of file +echo $table->generate($data); From 3bfd3b8eafffa3a98927347fc95d5e22ff3a1ae0 Mon Sep 17 00:00:00 2001 From: Christian Rumpf Date: Wed, 5 Apr 2023 22:31:57 +0200 Subject: [PATCH 08/13] Renamed variable and method name --- system/View/Table.php | 10 +++++----- tests/system/View/TableTest.php | 4 ++-- user_guide_src/source/outgoing/table.rst | 10 +++++----- user_guide_src/source/outgoing/table/019.php | 4 ++-- user_guide_src/source/outgoing/table/020.php | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/system/View/Table.php b/system/View/Table.php index 94373f360d0e..14b147b0a0b5 100644 --- a/system/View/Table.php +++ b/system/View/Table.php @@ -86,7 +86,7 @@ class Table /** * Order each inserted row by heading keys */ - public bool $rowKeysSyncWithHeadingKeys = false; + public bool $syncRowsWithHeading = false; /** * Set the template from the table config file if it exists @@ -167,7 +167,7 @@ public function makeColumns($array = [], $columnLimit = 0) // Turn off the auto-heading feature since it's doubtful we // will want headings from a one-dimensional array $this->autoHeading = false; - $this->rowKeysSyncWithHeadingKeys = false; + $this->syncRowsWithHeading = false; if ($columnLimit === 0) { return $array; @@ -215,7 +215,7 @@ public function addRow() { $tmpRow = $this->_prepArgs(func_get_args()); - if ($this->rowKeysSyncWithHeadingKeys && ! empty($this->heading)) { + if ($this->syncRowsWithHeading && ! empty($this->heading)) { // each key has an index $keyIndex = array_flip(array_keys($this->heading)); @@ -242,9 +242,9 @@ public function addRow() * If a row has a key which does not exist in heading, it will be filtered out * If a row does not have a key which exists in heading, the field will stay empty */ - public function setSyncRowKeysWithHeadingKeys(bool $orderByKey): Table + public function setSyncRowsWithHeading(bool $orderByKey): Table { - $this->rowKeysSyncWithHeadingKeys = $orderByKey; + $this->syncRowsWithHeading = $orderByKey; return $this; } diff --git a/tests/system/View/TableTest.php b/tests/system/View/TableTest.php index 5c574f2989a1..b752b6c00ab4 100644 --- a/tests/system/View/TableTest.php +++ b/tests/system/View/TableTest.php @@ -770,7 +770,7 @@ public function testInvalidCallback() public function testAddRowAndGenerateWithOrderedColumns(array $heading, array $row, string $expectContainsString): void { $this->table->setHeading($heading); - $this->table->setSyncRowKeysWithHeadingKeys(true); + $this->table->setSyncRowsWithHeading(true); $this->table->addRow($row); $generated = $this->table->generate(); @@ -784,7 +784,7 @@ public function testAddRowAndGenerateWithOrderedColumns(array $heading, array $r public function testGenerateDataWithOrderedColumns(array $heading, array $row, string $expectContainsString): void { $this->table->setHeading($heading); - $this->table->setSyncRowKeysWithHeadingKeys(true); + $this->table->setSyncRowsWithHeading(true); $generated = $this->table->generate([$row]); diff --git a/user_guide_src/source/outgoing/table.rst b/user_guide_src/source/outgoing/table.rst index ce61a026697c..08477038ebd3 100644 --- a/user_guide_src/source/outgoing/table.rst +++ b/user_guide_src/source/outgoing/table.rst @@ -76,7 +76,7 @@ Synchronizing row columns with heading .. versionadded:: 4.4.0 -``setSyncRowKeysWithHeadingKeys(true)`` enables that each data value +``setSyncRowsWithHeading(true)`` enables that each data value is placed in the same column as defined in ``setHeading()`` if an associative array was used as parameter. This is especially useful when dealing with data loaded via REST API where the order is not to @@ -88,7 +88,7 @@ mentioned in heading, it places an empty cell for that spot. .. literalinclude:: table/019.php -.. important:: You must call ``setSyncRowKeysWithHeadingKeys(true)`` and +.. important:: You must call ``setSyncRowsWithHeading(true)`` and ``setHeading([...])`` before adding any rows via ``addRow([...])`` where the rearrangement of columns takes place. @@ -215,11 +215,11 @@ Class Reference .. literalinclude:: table/018.php - .. php:method:: setSyncRowKeysWithHeadingKeys(bool $orderByKey) + .. php:method:: setSyncRowsWithHeading(bool $orderByKey) :returns: Table instance (method chaining) :rtype: Table - Enables each row column to be ordered by heading keys. This gives - more control of how data is displayed in the final table. Make + Enables each row data key to be ordered by heading keys. This gives + more control of data being displaced in the correct column. Make sure to set this value before calling the first ``addRow()`` method. diff --git a/user_guide_src/source/outgoing/table/019.php b/user_guide_src/source/outgoing/table/019.php index 2edcac9c0197..5478867fddfa 100644 --- a/user_guide_src/source/outgoing/table/019.php +++ b/user_guide_src/source/outgoing/table/019.php @@ -3,7 +3,7 @@ $table = new \CodeIgniter\View\Table(); $table->setHeading(['name' => 'Name', 'color' => 'Color', 'size' => 'Size']) - ->setSyncRowKeysWithHeadingKeys(true) + ->setSyncRowsWithHeading(true) ->addRow(['color' => 'Blue', 'name' => 'Fred', 'size' => 'Small']) ->addRow(['size' => 'Large', 'age' => '24', 'name' => 'Mary']) ->addRow(['color' => 'Green']); @@ -37,4 +37,4 @@ - \ No newline at end of file + diff --git a/user_guide_src/source/outgoing/table/020.php b/user_guide_src/source/outgoing/table/020.php index afada80c8d3c..42bda4d2f3bc 100644 --- a/user_guide_src/source/outgoing/table/020.php +++ b/user_guide_src/source/outgoing/table/020.php @@ -19,6 +19,6 @@ $table = new \CodeIgniter\View\Table(); $table->setHeading(['name' => 'Name', 'color' => 'Color', 'size' => 'Size']) - ->setSyncRowKeysWithHeadingKeys(true); + ->setSyncRowsWithHeading(true); echo $table->generate($data); From 4a9bb60bb575f28a54701338814b86f3d4eb346b Mon Sep 17 00:00:00 2001 From: Christian Rumpf Date: Wed, 5 Apr 2023 22:57:22 +0200 Subject: [PATCH 09/13] CS fixes after change --- system/View/Table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/View/Table.php b/system/View/Table.php index 14b147b0a0b5..76e43e26d393 100644 --- a/system/View/Table.php +++ b/system/View/Table.php @@ -166,7 +166,7 @@ public function makeColumns($array = [], $columnLimit = 0) // Turn off the auto-heading feature since it's doubtful we // will want headings from a one-dimensional array - $this->autoHeading = false; + $this->autoHeading = false; $this->syncRowsWithHeading = false; if ($columnLimit === 0) { From ddb6d3ff9e7249bded661c856db70fd942c6c631 Mon Sep 17 00:00:00 2001 From: Christian Rumpf Date: Thu, 6 Apr 2023 20:06:56 +0200 Subject: [PATCH 10/13] Added to changelog and suggested changes --- system/View/Table.php | 4 +++- tests/system/View/TableTest.php | 8 ++++---- user_guide_src/source/changelogs/v4.4.0.rst | 1 + user_guide_src/source/outgoing/table.rst | 16 +++++++++------- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/system/View/Table.php b/system/View/Table.php index 76e43e26d393..a7623ca616c8 100644 --- a/system/View/Table.php +++ b/system/View/Table.php @@ -241,8 +241,10 @@ public function addRow() * * If a row has a key which does not exist in heading, it will be filtered out * If a row does not have a key which exists in heading, the field will stay empty + * + * @return Table */ - public function setSyncRowsWithHeading(bool $orderByKey): Table + public function setSyncRowsWithHeading(bool $orderByKey) { $this->syncRowsWithHeading = $orderByKey; diff --git a/tests/system/View/TableTest.php b/tests/system/View/TableTest.php index b752b6c00ab4..2895f31df037 100644 --- a/tests/system/View/TableTest.php +++ b/tests/system/View/TableTest.php @@ -767,7 +767,7 @@ public function testInvalidCallback() /** * @dataProvider orderedColumnUsecases */ - public function testAddRowAndGenerateWithOrderedColumns(array $heading, array $row, string $expectContainsString): void + public function testAddRowAndGenerateOrderedColumns(array $heading, array $row, string $expectContainsString): void { $this->table->setHeading($heading); $this->table->setSyncRowsWithHeading(true); @@ -781,7 +781,7 @@ public function testAddRowAndGenerateWithOrderedColumns(array $heading, array $r /** * @dataProvider orderedColumnUsecases */ - public function testGenerateDataWithOrderedColumns(array $heading, array $row, string $expectContainsString): void + public function testGenerateOrderedColumns(array $heading, array $row, string $expectContainsString): void { $this->table->setHeading($heading); $this->table->setSyncRowsWithHeading(true); @@ -791,9 +791,9 @@ public function testGenerateDataWithOrderedColumns(array $heading, array $row, s $this->assertStringContainsString($expectContainsString, $generated); } - public function orderedColumnUsecases(): array + public function orderedColumnUsecases(): iterable { - return [ + yield from [ 'reorder example #1' => [ 'heading' => ['id' => 'ID', 'name' => 'Name', 'age' => 'Age'], 'row' => ['name' => 'Max', 'age' => 30, 'id' => 5], diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst index 503922b418e2..6e38883448ad 100644 --- a/user_guide_src/source/changelogs/v4.4.0.rst +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -87,6 +87,7 @@ Others See :ref:`controller-default-method-fallback` for details. - **Filters:** Now you can use Filter Arguments with :ref:`$filters property `. - **Request:** Added ``IncomingRequest::setValidLocales()`` method to set valid locales. +- **Table:** Addedd ``Table::setSyncRowsWithHeading()`` method to synchronize row columns with heading. See :ref:`table-sync-rows-with-headings` for details. Message Changes *************** diff --git a/user_guide_src/source/outgoing/table.rst b/user_guide_src/source/outgoing/table.rst index 08477038ebd3..bec15f86dbbf 100644 --- a/user_guide_src/source/outgoing/table.rst +++ b/user_guide_src/source/outgoing/table.rst @@ -71,20 +71,22 @@ to the Table constructor: .. literalinclude:: table/008.php -Synchronizing row columns with heading -====================================== +.. _table-sync-rows-with-headings: + +Synchronizing Rows with Headings +================================ .. versionadded:: 4.4.0 -``setSyncRowsWithHeading(true)`` enables that each data value +The ``setSyncRowsWithHeading(true)`` method enables that each data value is placed in the same column as defined in ``setHeading()`` if an associative array was used as parameter. This is especially useful when dealing with data loaded via REST API where the order is not to your liking, or if the API returned too much data. -If a data row contains a ``key`` which does not exist the heading, -the value will be filtered out. Vise versa if a data row does not have key -mentioned in heading, it places an empty cell for that spot. +If a data row contains a key that is not present in the heading, its value is +filtered. Conversely, if a data row does not have a key listed in the heading, +an empty cell will be placed in its place. .. literalinclude:: table/019.php @@ -92,7 +94,7 @@ mentioned in heading, it places an empty cell for that spot. ``setHeading([...])`` before adding any rows via ``addRow([...])`` where the rearrangement of columns takes place. -You get the same result by using the result array is input in ``generate()`` +Using an array as input to ``generate()`` produces the same result: .. literalinclude:: table/020.php From 754391225db375b496a929393f113f2b2714afaf Mon Sep 17 00:00:00 2001 From: Christian Rumpf Date: Thu, 6 Apr 2023 20:26:39 +0200 Subject: [PATCH 11/13] CS fix --- system/View/Table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/View/Table.php b/system/View/Table.php index a7623ca616c8..223d16db71e3 100644 --- a/system/View/Table.php +++ b/system/View/Table.php @@ -241,7 +241,7 @@ public function addRow() * * If a row has a key which does not exist in heading, it will be filtered out * If a row does not have a key which exists in heading, the field will stay empty - * + * * @return Table */ public function setSyncRowsWithHeading(bool $orderByKey) From 5405ba4a662b286cb10829241cc2b679351776fc Mon Sep 17 00:00:00 2001 From: Christian Rumpf Date: Fri, 7 Apr 2023 13:06:23 +0200 Subject: [PATCH 12/13] Fixed typos --- system/View/Table.php | 2 +- user_guide_src/source/changelogs/v4.4.0.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/system/View/Table.php b/system/View/Table.php index 223d16db71e3..fb98248c9e6e 100644 --- a/system/View/Table.php +++ b/system/View/Table.php @@ -242,7 +242,7 @@ public function addRow() * If a row has a key which does not exist in heading, it will be filtered out * If a row does not have a key which exists in heading, the field will stay empty * - * @return Table + * @return $this */ public function setSyncRowsWithHeading(bool $orderByKey) { diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst index 6e38883448ad..b90a9c75fc9f 100644 --- a/user_guide_src/source/changelogs/v4.4.0.rst +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -87,7 +87,7 @@ Others See :ref:`controller-default-method-fallback` for details. - **Filters:** Now you can use Filter Arguments with :ref:`$filters property `. - **Request:** Added ``IncomingRequest::setValidLocales()`` method to set valid locales. -- **Table:** Addedd ``Table::setSyncRowsWithHeading()`` method to synchronize row columns with heading. See :ref:`table-sync-rows-with-headings` for details. +- **Table:** Added ``Table::setSyncRowsWithHeading()`` method to synchronize row columns with heading. See :ref:`table-sync-rows-with-headings` for details. Message Changes *************** From 524e880f5b67a2857ee17c383fdf0143ddadb85e Mon Sep 17 00:00:00 2001 From: Christian Rumpf Date: Sat, 8 Apr 2023 09:09:08 +0200 Subject: [PATCH 13/13] Minor updates --- system/View/Table.php | 2 +- user_guide_src/source/changelogs/v4.4.0.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/system/View/Table.php b/system/View/Table.php index fb98248c9e6e..b66a18c338e6 100644 --- a/system/View/Table.php +++ b/system/View/Table.php @@ -86,7 +86,7 @@ class Table /** * Order each inserted row by heading keys */ - public bool $syncRowsWithHeading = false; + private bool $syncRowsWithHeading = false; /** * Set the template from the table config file if it exists diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst index b90a9c75fc9f..f47fdcfec1bf 100644 --- a/user_guide_src/source/changelogs/v4.4.0.rst +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -87,7 +87,7 @@ Others See :ref:`controller-default-method-fallback` for details. - **Filters:** Now you can use Filter Arguments with :ref:`$filters property `. - **Request:** Added ``IncomingRequest::setValidLocales()`` method to set valid locales. -- **Table:** Added ``Table::setSyncRowsWithHeading()`` method to synchronize row columns with heading. See :ref:`table-sync-rows-with-headings` for details. +- **Table:** Added ``Table::setSyncRowsWithHeading()`` method to synchronize row columns with headings. See :ref:`table-sync-rows-with-headings` for details. Message Changes ***************