如何为Yii dropDownList中的每个选项提供属性值?

时间:2013-08-27 04:43:47

标签: php yii

我有一个与Yii DropDownList相关的问题。我们如何为每个选项提供一些属性值。例如,我需要构建一个如下所示的下拉列表。

<select>
    <option value="a" myAttribute="xyz">Calendar</option>
    <option value="n" myAttribute="PQR">Shopping Cart</option>
    <option value="c" myAttribute="ABC">CD</option>
    <option value="d" myAttribute="HMN">Email</option>   
</select>

values和myAttributes的值来自表。如何通过更改以下内容来实现此目的?

<?php echo $form->dropDownList($model,'media_ids',Media::getAllmedia(),array('?'=>'?')); ?>

在模型媒体中我有这样的功能

public function getAllmedia() {    
        $condition = 'isActive =  "Y"';    
        $model = Media::model()->findAll(array('condition' => $condition));    
        return CHtml::listData($model, 'id', 'media_title');    
    }

编辑我的要求是显示图像以及每个选项,因为我必须为保存图像路径的每个选项提供另一个属性

4 个答案:

答案 0 :(得分:3)

CHtml :: dropDownList()不允许这样做.. 尝试创建这样的其他函数:

媒体类文件:

    static function getAllmediaDDL($idSelected=null) {
                  $condition = 'isActive =  "Y"';
                  $model = Media::model()->findAll(array('condition' => $condition));

                  $combo = CHtml::tag('select', array('id' => 'mySelect'));
                  foreach ($model as $value){
                      $selected = $value->id == $idSelected? true : false;
                      $combo .= CHtml::tag('option', array('value' => $value->id, 'myattribute'=>$value->url_img, 'selected'=>$selected), CHtml::encode($value->media_title), true);
                  }

                  $combo .= CHtml::tag('select');
                  return $combo;

   }

在视野中呼叫:

echo Media::getAllmediaDDL($model->id); // If you want to call in this way, you need to declare it as static function.

答案 1 :(得分:1)

你有listOption details的答案,你必须实现另一个函数,就像你为选项值所做的那样映射每个id,它是图像路径的值:array(id =&gt; array(image_path_attribute = &gt; value),another_id =&gt;(array(),...)例如,假设函数名称为listImagePath(),返回一个选项数组但不要忘记每个值都映射到属性数组不只是一个

<?php echo $form->dropDownList($model,'media_ids',Media::getAllmedia(),array('options'=>Media::listImagePath())); ?>

答案 2 :(得分:1)

这是工作:

  private static SynchronizationContext _context;
  private static readonly ManualResetEvent AwesomiumReady = new ManualResetEvent(false);
  private readonly ManualResetEvent _mainFrameLoaded = new ManualResetEvent(false);
  private static Thread _awesomeThread;

  ...

  public AwesomiumPageDownloader()
  {
     Initialise();
  }

  private static void Initialise()
  {
     if(_awesomeThread != null) return;  //ignore concurrent initialisation problem for now

     _awesomeThread = new Thread(() =>
     {
        WebCore.Started += (s, e) =>
        {
           _context = SynchronizationContext.Current;
           AwesomiumReady.Set();
        };

        WebCore.Run();
     });
     _awesomeThread.Name = "Awesomium Worker";

     _awesomeThread.Start();

     WebCore.Initialize(new WebConfig());

     AwesomiumReady.WaitOne();
  }

  public string Download(string url)
  {
     _mainFrameLoaded.Reset();

     _context.Post(state =>
     {
        using(WebView view = WebCore.CreateWebView(1024, 768))
        {

           view.Source = new Uri(url);

           view.LoadingFrameComplete += (sender, e) =>
           {
              if(!e.IsMainFrame)
              {
                 _mainFrameLoaded.Set();
                 return;
              }
           };

           view.DocumentReady += (sender, args) =>
           {
              _mainFrameLoaded.Set();
           };
        }
     }, null);

     _mainFrameLoaded.WaitOne();

     return null;
  }

在模特中:

<?php 
     echo $form->dropDownList($model,'media_ids',
               'data' => CHtml::listData(Media::model()->findAll(), 'id', 'name'),
                array(
                     'options' =>  CHtml::listData(Media::model()->findAll(), 'id', 'option_array')
                     )
     ); 
?>

答案 3 :(得分:-2)

对不起,我还是初学者,但这是你在找什么?

中的

  <?php echo $form->dropDownListRow($model, 'status',array(0=>"Inactive",1=>"Active")
    ); ?>