How to give select tag an attribute in cake php?

こ雲淡風輕ζ 提交于 2019-12-24 07:10:46

问题


How to give select tag an attribute in cake php ?

$options = array('0' => 'News', '1' => 'Movies');
    echo $this->Form->select('selectValue', $options, 0, array('id' => 'select') )

So that I can give a attribute data-url to every options

<select id="select" name="data[Video][gender]" >
  <option value="0" data-url = "/news" >News</option>
  <option value="1" data-url = "/movies" >Movies</option>
</select>

回答1:


you can't :P or at least there's no easy way to do it...

You'll need to extend the FormHelper and override the select() method.

Something like:

class MyFormHelper extends FormHelper{
    public function select($fieldName, $options = array(), $attributes = array()) {
        //something like the original method but this time the $options 
        //is more than a simple key=>value array, so you'll need
        //to change the code to include the data-url
    }
}

and then call your helper using MyFormHelper, something like

//In the view
$options = array(
                 array('data-url'=>'/news','text'=>'News'),
                 array('data-url'=>'/movies','text'=>'Movies'),
);

$this->MyForm->select('field',array('options'=>$options))

That's the main idea. Hope this helps



来源:https://stackoverflow.com/questions/10881586/how-to-give-select-tag-an-attribute-in-cake-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!