dthhlf1777 2012-06-06 16:50
浏览 74
已采纳

添加属性以选择Joomla中的选项

I'm trying to add extra HTML attributes to some select options in a dropdown list in Joomla 2.5, and want to use the built-in HTML helpers rather than write the HTML by myself. The current output is:

<select>
    <option value="Red">Red</option>
</select>

but I would like it to be something like:

<select>
    <option value="Red" data-img="red.jpg">Red</option>
</select>

so that I can access the data-img attribute using Javascript when the selected option changes.

I'm creating the select options like so:

$select = JHtml::_('select.option', "Red", "Red");

and then passing them to JHTML to create the generic list HTML:

$html = JHTML::_('select.genericlist', ...);

I've looked through the documentation and tried passing various different parameter to the functions, but it's very confusing in terms of all the options (option.attr, attr etc) that the functions use, and Google has turned up nothing either.

Can anyone tell me what extra parameters I need to pass to the functions to get it properly add the extra attributes to the <option> elements?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dpql57753 2012-06-12 18:15
    关注

    I was struggling on this exact scenario today, needing to add some extra data with the select's options. After a thorough analyze of the joomla/libraries/joomla/html/html/select.php file, I succeeded to do this with a few downside...

    First, in my case, the data used for the select is coming from the database, and need some preparation for this scenario :

    $db =& JFactory::getDBO();
    $sql = 'SELECT  nom AS value , nom AS text, prix FROM #__reservation_nourritures order by `ordering` asc';
    $db->setQuery($sql);
    $nourritures = $db->loadObjectList();
    foreach ($nourritures as $nourriture){
        //the data to use MUST be in an array form for the extra html attributes...
        $nourriture->data = array('data'=>$nourriture->prix);
    }
    

    Once the data is ready, you can pass it to the JHTML function to build the select :

    echo JHTML::_('select.genericlist',$nourriture,'myId',array('class'=>'nourritures','option.attr'=>'data'));
    

    In short, the 'option.attr' must be used to insert attributes into the options. Note : The select.genericlist function MUST have only 3 arguments for this to work. From what I understand from the function, attributes only get merged into the options if you pass exactly 3 arguments to the function, otherwise it just ignores it. So if you want, as exemple, to define a preselected option with the additional parameters, you are out of luck. Here is the part regarding this in the function :

    if (is_array($attribs) && func_num_args() == 3)
    {
        // Assume we have an options array
        $options = array_merge($options, $attribs);
    }
    

    To my understanding, this is a bug and/or a bad behavior. I'll fill in a bugg in the joomla tracker when I get time.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端