CakePHP数据图标在选择下拉列表中 [英] CakePHP data-icon in select dropdown

查看:103
本文介绍了CakePHP数据图标在选择下拉列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用CakePHP(2.3),有没有办法轻松地添加额外的参数到选择框的选项元素?

Using CakePHP (2.3), is there a way to easily add extra parameters to the option element of a select box?

使用以下代码在模板中创建选择下拉菜单时:

When creating a select dropdown in a template using the following code :

echo $this->Form->input('icon_id', array(
  'label' => 'Icon',
  'empty' => ' ',
  'class' => 'selectbox'
));

输出以下HTML(这是正确的):

The following HTML is output (which is correct) :

<div class="input select">
  <select name="data[Link][icon_id]" class="selectbox" id="LinkIconId">
    <option value=""> </option>
    <option value="16">fa-adjust</option>
    <option value="76">fa-anchor</option>
    <option value="135">fa-archive</option>
    <option value="6">fa-arrow-cicle-o-right</option>
    <option value="10">fa-arrow-circle-o-left</option>
...


$ b $ p我的问题是,我可以通过编程方式向选项标签添加属性,具体来说,我想添加data-icon属性,值与选项文本相同?例如:

My question is, can I programmatically add attributes to the option tag like, and specifically, I'd like to add the data-icon attribute, with the value being the same as the option text? For Example :

    <option value="6" data-icon="fa-arrow-circle-o-right">fa-arrow-cicle-o-right</option>

我有一种感觉,我可能需要写一个帮手来做这个。

I have a feeling I might need to write a helper to do this.

推荐答案

$options = array(
                 1 => 'One', 
                 2 => array('name' => 'Two', 'value' => 2,  'class' => 'extra'), 
                 3 => 'Three',
                 6 => array('name' => 'Six', 'value' => 6, "data-icon"=>"fa-arrow-circle-o-right")
);

echo $this->Form->input('my_option_id',array('type' =>'select', 'options'=>$options));

HTML =>

<select name="data[my_option_id]" id="my_option_id">
  <option value="1">One</option>
  <option value="2" class="extra">Two</option>
  <option value="3">Three</option>
  <option value="6" data-icon="fa-arrow-circle-o-right" >Six</option>
</select>

这篇关于CakePHP数据图标在选择下拉列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆