• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add data attributes on <options> using form_dropdown()

#1
How can I add data attributes in the <option> tags using the form_dropdown() helper ? To generate something like this, for example:
PHP Code:
// the part that I'm interested is the [b]data-code[/b] attribute
<select class="form-control" name="country">
     <
option value="a" data-code="code1">itemOne</option>
     <
option value="b" data-code="code2">itemTwo</option>
     <
option value="c" data-code="code3">itemThree</option>
</
select
Reply

#2
Hello ,

This can be done in codegniter :

form_dropdown('project_interested_in', array("" => "Location Interested In") + $arrProjectNameCity, "", ' id="project_interested_in" class="form-control" ')
Reply

#3
If you use CI form_dropdown then you can pass it an array of options.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#4
(08-20-2015, 04:17 AM)InsiteFX Wrote: If you use CI form_dropdown then you can pass it an array of options.

I'm using CI form_dropdown, but from what I see in the documentation it gives only an example of how to pass the options as a key => value array, like this

PHP Code:
<option value="$keydata-attr="???">$val</option

What about the data attributes??
Reply

#5
You use the $extra

PHP Code:
$extra 'id="1" class="class"'
You may need to use a comma between them not sure.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#6
The form_dropdown() function doesn't have anyway to add attributes to the option elements. $extra is added to the select element.

If you pass a two-dimensional array to $options, it creates an optgroup element with the interior key/value pairs used for option elements inside the optgroup.

The best you could probably do if you still want to use form_dropdown() would be to create a new version of the function in MY_form_helper.php which takes an additional parameter with an array for the option attributes. In that case, I would recommend setting it up to require the same keys used in the $options array with the value being an array or string that can be passed to _attributes_to_string() (like the $extra parameter).

Here is an example. Most of the code here is just copied from the form_helper, and, other than the added parameter on the first line, I've marked the changed lines with comments indicating the changes. Note that although it supports adding attributes to option elements within optgroup elements, it doesn't support adding attributes to the optgroup elements themselves.
PHP Code:
function form_dropdown($data ''$options = array(), $selected = array(), $extra ''$optionsExtra = array())
{
 
   $defaults = array();

 
   if (is_array($data))
 
   {
 
       if (isset($data['selected']))
 
       {
 
           $selected $data['selected'];
 
           unset($data['selected']); // select tags don't have a selected attribute
 
       }

 
       if (isset($data['options']))
 
       {
 
           $options $data['options'];
 
           unset($data['options']); // select tags don't use an options attribute
 
       }
 
   }
 
   else
    
{
 
       $defaults = array('name' => $data);
 
   }

 
   is_array($selected) OR $selected = array($selected);
 
   is_array($options) OR $options = array($options);

 
   // If no selected state was submitted we will attempt to set it automatically
 
   if (empty($selected))
 
   {
 
       if (is_array($data))
 
       {
 
           if (isset($data['name'], $_POST[$data['name']]))
 
           {
 
               $selected = array($_POST[$data['name']]);
 
           }
 
       }
 
       elseif (isset($_POST[$data]))
 
       {
 
           $selected = array($_POST[$data]);
 
       }
 
   }

 
   $extra _attributes_to_string($extra);

 
   $multiple = (count($selected) > && stripos($extra'multiple') === FALSE) ? ' multiple="multiple"' '';

 
   $form '<select '.rtrim(_parse_form_attributes($data$defaults)).$extra.$multiple.">\n";

 
   foreach ($options as $key => $val)
 
   {
 
       $key = (string) $key;

 
       if (is_array($val))
 
       {
 
           if (empty($val))
 
           {
 
               continue;
 
           }

 
           $form .= '<optgroup label="'.$key."\">\n";

 
           foreach ($val as $optgroup_key => $optgroup_val)
 
           {
 
               $sel in_array($optgroup_key$selected) ? ' selected="selected"' '';

 
               // Changed to include $optionsExtra
 
               $form .= '<option value="'.html_escape($optgroup_key).'"'.$sel
                    
.(isset($optionsExtra[$key][$optgroup_key]) ? _parse_form_attributes($optionsExtra[$key][$optgroup_key]) : '')
 
                   .'>'.(string) $optgroup_val."</option>\n";
 
           }
 
           $form .= "</optgroup>\n";
 
       
 
       else 
        
{
 
           // Changed to include $optionsExtra
 
           $form .= '<option value="'.html_escape($key).'"'
 
               .(in_array($key$selected) ? ' selected="selected"' '')
 
               .(isset($optionsExtra[$key]) ? _parse_form_attributes($optionsExtra[$key]) : '')
 
               .'>'.(string) $val."</option>\n";
 
       }
 
   }

 
   return $form."</select>\n";

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.