Welcome Guest, Not a member yet? Register   Sign In
form_dropdown() - setting properties of <options>
#1

[eluser]smilie[/eluser]
Is there a way to set properties to the <options> of <select> (dropdown)?

I have:

Code:
<select id="sth" name="bla">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

This is done automatically from the foreach loop.
But, I need:

Code:
<option id="sth" value="bla">bla</option>

Why do I need this?
Select is list of all countries. I wanna store the country phone code in ID and the value to be country name. That way, when user changes the country (from drop down) I can update phone country code automatically. Work around is to create another Ajax call to get the code upon .change() but would rather avoid this Smile

Thanks!
#2

[eluser]Jaketoolson[/eluser]
Do you mean to say, each option would have their own unique id?

The options tag supports the standard attributes:
Quote:class
dir
ltr
id
lang
style
title
xml:lang


The CI form_dropdown() function, only supports passing multiple attributes to the <select>, and not the <option>. You can modify this yourself by re-writing this function adding in the functionality to pass attributes to the <option>.

I've done this myself for other form functions where there was no ability to even pass attributes to a form element in the first place... This is especially true with the table library which lacks the same capability throughout, and is missing key structure elements for creating a table!
#3

[eluser]smilie[/eluser]
Well, that's what I have eventually done Smile

In case anyone needs same functionality:

in helpers/form_helper.php (around line 350 in function form_dropdown())

was:
Code:
function form_dropdown($name = '', $options = array(), $selected = array(), $extra = ''){}

// changed to:
function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '',$id=FALSE)

// and was:

else
{
       $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
    $form .= '<option value="'.$key.'" '.$sel.'>'.(string) $val."</option>\n";
}

// Changed to:

else
{
    if($id==TRUE)
    {
        $sel = (in_array($key, $selected) OR in_array($val,$selected)) ? ' selected="selected"' : '';
        $form .= '<option value="'.$val.'" id="'.$key.'" '.$sel.'>'.(string) $val."</option>\n";
    }
    else
    {
        $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
        $form .= '<option value="'.$key.'" '.$sel.'>'.(string) $val."</option>\n";
    }
}

I used this approach not to break functionality of the function for all other dropdowns I am using Smile
#4

[eluser]richiejaeger[/eluser]
Hey,

i know that this thread is a bit old, but i was also looking for a quick solution and i tried something and its working, without editing any helper or core.

Code:
$options = array(
    'small" class="small_class'   =&gt; 'Small Shirt',
    'med" class="med_class'       =&gt; 'Medium Shirt',
    'large" class="large_class'   =&gt; 'Large Shirt',
    'xlarge" class="xlarge_class' =&gt; 'Extra Large Shirt',
);

$shirts_on_sale = array('small', 'large');

echo form_dropdown('shirts', $options, 'large');

// Would produce:

&lt;select name="shirts">
<option value="small" class="small_class">Small Shirt</option>
<option value="med" class="med_class">Medium Shirt</option>
<option value="large" selected="selected" class="large_class">Large Shirt</option>
<option value="xlarge" class="xlarge_class">Extra Large Shirt</option>
</select>

Its working and the form validation still works Smile


Hope this will help somebody.

Greeting from Germany
Richard!
#5

[eluser]Aken[/eluser]
You can also generate the HTML the old fashioned way, if a helper is ever limiting what you're trying to output. Smile
#6

[eluser]richiejaeger[/eluser]
Sure, you could Smile

But the thread is about form_dropdown() ^^
#7

[eluser]Sepehr Lajevardi[/eluser]
Code:
if ( ! function_exists('form_dropdown'))
{
/**
  * A fork of core form_dropdown() to support option attributes.
  *
  * Sample $options array:
  * <code>
  * $options = array(
  *     // Option with attributes:
  *     'channel_islands' => array(
  *       // Option value:
  *      'option_value' => 'Channel Islands',
  *      // Other attributes: (class, id, style, etc.)
  *      'class' => 'London',
  *     ),
  *
  *   // Oldskool options:
  *   'barnet' => 'Barnet',
  *
  *     // ...
  * );
  * </code>
  *
  * @param  string $name     Field name.
  * @param  array  $options  Array of dropdown options.
  * @param  array  $selected Array of selected values.
  * @param  string $extra    Extra string to append to <select>.
  *
  * @return string           HTML representation of the dropdown.
  *
  * @see http://ellislab.com/forums/viewthread/193529/
  */
function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
{
  if ( ! is_array($selected))
  {
   $selected = array($selected);
  }

  // If no selected state was submitted we will attempt to set it automatically
  if (count($selected) === 0)
  {
   // If the form name appears in the $_POST array we have a winner!
   if (isset($_POST[$name]))
   {
    $selected = array($_POST[$name]);
   }
  }

  if ($extra != '') $extra = ' '.$extra;

  $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';

  $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";

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

   // 1. Handle option attributes
   if (is_array($val) AND isset($val['option_value']))
   {
    // Get attributes, update value
    $attrs = $val;
    $val   = $val['option_value'];
    unset($attrs['option_value']);
    // Theme attributes
    $attrs = _parse_form_attributes($attrs, array());
   }

   // 2. Still an array? It's an optgroups
   if (is_array($val) && ! empty($val))
   {
    $form .= '<optgroup label="'.$key.'">'."\n";

    foreach ($val as $optgroup_key => $optgroup_val)
    {
     $attrs = '';

     // Handle optgroup option attributes
     if (is_array($optgroup_val) AND isset($optgroup_val['option_value']))
     {
      // Get attributes, update value
      $attrs        = $optgroup_val;
      $optgroup_val = $optgroup_val['option_value'];
      unset($attrs['option_value']);
      // Theme attributes
      $attrs = _parse_form_attributes($attrs, array());
     }

     $sel   = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
     $form .= '<option value="' . $optgroup_key . '"' . $sel . ' ' . $attrs . '>' . (string) $optgroup_val . "</option>\n";
    }

    $form .= '</optgroup>'."\n";
   }

   // 3. Not an array value
   else
   {
    $sel   = in_array($key, $selected) ? ' selected="selected"' : '';
    $form .= '<option value="' . $key . '"' . $sel . ' ' . $attrs . '>' . (string) $val . "</option>\n";
   }
  }

  // Outro!
  $form .= '</select>';

  return $form;
}
}




Theme © iAndrew 2016 - Forum software by © MyBB