Skip to content

Commit

Permalink
#724: Second argument of CHtml::listData() now receives anonymous fun…
Browse files Browse the repository at this point in the history
…ction as calculator of the text field value, PHP 5.3+ only
  • Loading branch information
samdark committed Sep 9, 2012
2 parents 230ff6e + f7beda2 commit aa87d96
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG 100644 → 100755
Expand Up @@ -19,6 +19,7 @@ Version 1.1.13 work in progress
- Enh #291: CFormatter::formatDate and formatDateTime now also accept strings in strtotime() format (francis_tm, cebe)
- Enh #486: CHttpSession::$gCProbability and CDbHttpSession::$gCProbability are floats now. Minimal possible $gCProbability value has been changed to the ≈0.00000005% (1/2147483647), was integer 1% before, default value left unchanged (1%) (resurtm)
- Enh #556: CDbColumnSchema::$comment property has been added. It stores comment for the table column, comment retrieving is working for MySQL, PgSQL and Oracle (resurtm)
- Enh #724: Second argument of CHtml::listData() now receives anonymous function as calculator of the text field value, PHP 5.3+ only (resurtm)

This comment has been minimized.

Copy link
@Borales

Borales Sep 10, 2012

Contributor

@resurtm, perhaps, you meant "third argument"?
The same typo is in framework/web/helpers/CHtml.php:1758 in PHPDoc for listData()

This comment has been minimized.

Copy link
@samdark

samdark Sep 10, 2012

Author Member

Good catch. Fixed.

- Enh #949: Added COciSchema::resetSequence (jazahn, samdark)
- Enh #990: Added CArrayDataProvider::$caseSensitiveSort property which allows to control whether sorting should be case sensitive (resurtm)
- Enh #1084: CDateTimeParser: MMM pattern for parsing short month names is now locale aware (resurtm)
Expand Down
58 changes: 48 additions & 10 deletions framework/web/helpers/CHtml.php
Expand Up @@ -1755,10 +1755,19 @@ public static function error($model,$attribute,$htmlOptions=array())
* Note, this method does not HTML-encode the generated data. You may call {@link encodeArray} to
* encode it if needed.
* Please refer to the {@link value} method on how to specify value field, text field and group field.
* You can also pass anonymous function as second argument which calculates text field value (PHP 5.3+ only).
* Your anonymous function should receive one argument, which is the model, the current <option> tag is generated from.
*
* <pre>
* CHtml::listData($posts,'id',function($post) {
* return CHtml::encode($post->title);
* });
* </pre>
*
* @param array $models a list of model objects. This parameter
* can also be an array of associative arrays (e.g. results of {@link CDbCommand::queryAll}).
* @param string $valueField the attribute name for list option values
* @param string $textField the attribute name for list option texts
* @param mixed $textField the attribute name or anonymous function (PHP 5.3+) for list option texts
* @param string $groupField the attribute name for list option group names. If empty, no group will be generated.
* @return array the list data that can be used in {@link dropDownList}, {@link listBox}, etc.
*/
Expand All @@ -1767,21 +1776,50 @@ public static function listData($models,$valueField,$textField,$groupField='')
$listData=array();
if($groupField==='')
{
foreach($models as $model)
if(class_exists('Closure',false) && $textField instanceof Closure)
{
// $text value is calculated in anonymous function, without groups
foreach($models as $model)
{
$value=self::value($model,$valueField);
$text=call_user_func($textField,$model);
$listData[$value]=$text;
}
}
else
{
$value=self::value($model,$valueField);
$text=self::value($model,$textField);
$listData[$value]=$text;
// $text value is from model attribute, without groups
foreach($models as $model)
{
$value=self::value($model,$valueField);
$text=self::value($model,$textField);
$listData[$value]=$text;
}
}
}
else
{
foreach($models as $model)
if(class_exists('Closure',false) && $textField instanceof Closure)
{
$group=self::value($model,$groupField);
$value=self::value($model,$valueField);
$text=self::value($model,$textField);
$listData[$group][$value]=$text;
// $text value is calculated in anonymous function, with groups
foreach($models as $model)
{
$group=self::value($model,$groupField);
$value=self::value($model,$valueField);
$text=call_user_func($textField,$model);
$listData[$group][$value]=$text;
}
}
else
{
// $text value is from model attribute, with groups
foreach($models as $model)
{
$group=self::value($model,$groupField);
$value=self::value($model,$valueField);
$text=self::value($model,$textField);
$listData[$group][$value]=$text;
}
}
}
return $listData;
Expand Down

0 comments on commit aa87d96

Please sign in to comment.