Add HTML5 data attribute to dropDownList

Hi,

I have the following situation. I am trying to create a dropdown List which gets its data from the database. All that works very well. I’m now trying to add an additional HTML5 data attribute to each <option> Tag which should include an additional id. How can I achieve that?


echo $form->dropDownList(

			$model,

			'search_id[]',

			PHtml::listData(Posts::model()->getPosts(),'id', 'title'),

                        array(

			     'class' => 'form-control',

			     'prompt' =>'Please Select',

                        );

Ok, I am big a step further in answering my own question.




$posts = Posts::model()->getPosts();


echo $form->dropDownList(

			$model,

			'search_id[]',

			PHtml::listData($posts,'id', 'title'),

                        array(

			     'class' => 'form-control',

			     'prompt' =>'Please Select',

                             'options'=>  array(

                                                'example' => array('data-mypost' => 2

                                          ),

                        );

That means that the option field with the value=‘example’ will now get the data attribute data-mypost=“2”. Wonderful, but not fully what I wanted yet, since the option fields are dynamic the value is dynamic. I therefore thought to try the following:




   'options'=>  array(

                      PHtml::listData($posts,'id'), => array('data-mypost' => 2

                ),



But again, this is not working. A print_r on PHtml::listData($posts,‘id’) shows me all the id’s, but it does not seem to work on top. Any idea?

UPDATE:

a print_r actually shows me


Array

(

    [122] => 

    [123] => 

    [142] => 

    [137] =>

which makes sense because list data returns an array. So I thought I could use




   'options'=>  array(

                      key(PHtmlPHtml::listData($posts,'id')), => array('data-mypost' => 2

                ),



This now enters a data attribute only for the first one… well any hints from anyone?