Yii Framework Forum: CHtml::listData Modified - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CHtml::listData Modified Allows attributes concactenation Rate Topic: ****- 2 Votes

#1 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,448
  • Joined: 04-October 10

Posted 14 January 2011 - 04:21 PM

Hi there,

I saw a couple of times at the forum people asking how to concatenate two attributes for the CHtml::listData method and I am currently developing a customized CHtml extension to render HTML elements properly for my new CMS that has that function modified. I post it here, so you guys can make use of it for your own needs.

I think that could be a good section in the forum for developers to share their functions (pieces of code too small to write a wiki or a extension). Things that maybe useful for others to 'plug' onto their Yii resource libraries. Anyway, whatever the team does I am sure they will do good.

Here is this little and useful function (at least for me).


/**
* 
* Modified CHtml listData
* @param array $models
* @param string $valueField 
* @param array|string $textFields 'attribute' or array('attribute1','attribute2')
* @param string $groupField
*/
public function listData( $models, $valueField, $textFields, $groupField = '' ){
		
		$listData=array();
		
		foreach($models as $model)
		{
			$value=CHtml::value($model,$valueField);
			if(is_array($textFields)){
				$text = array();
				foreach($textFields as $attr){
					$text[]= CHtml::value($model,$attr);
				}
				$text = implode(' ', $text);
			}
			else
				$text= CHtml::value($model,$textFields);
			
			if($groupField===''){
				$listData[$value]=$text;
			}
			else{
				$group=CHtml::value($model,$groupField);
				$listData[$group][$value]=$text;	
			}
				
		}
		return $listData;
	}

¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
2

#2 User is offline   chandraamit29 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 20-June 13

Posted 20 June 2013 - 12:24 AM

Thank you so much for providing this function. I was stuck this issue last 5 to 6 hour. I was not getting any proper solution. But when i applied these function, i got a proper result which i wanted.
0

#3 User is offline   Cristopher Mijares 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 13-November 13

Posted 17 December 2013 - 11:02 AM

Antonio!!!!
Encarecidamente muchas gracias por compartir tan acertada solución!!!
0

#4 User is offline   William Vaquerano 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 24-November 13

Posted 05 January 2014 - 01:17 AM

Antonio,

You solve this in such a simple manner! Thank you!
0

#5 User is offline   rajesh chaurasia 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 12-January 13
  • Location:Mohali

Posted 13 January 2014 - 01:41 AM

hi friends you can use like this

$criteria = new CDbCriteria;


$models = modelname::model()->findAll($criteria);

$list = CHtml::listData($models, 'id', 'title');

return $list;
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users