Codeigniter Form Helper - How to add additional parameters to "select" control?

1.4k views Asked by At

I need to modify a site that was written in Codeigniter but I'm no expert.

One thing I'd like to do is modify a select control in a form to use ms-dropdown for a drop-down list including pictures.

However, I can't work out how to make the Codeigniter form helper render parameters other than ID and Value in each option. In this case, to make ms-dropdown work, it would need to also render data-image="..." in each option.

The current code looks like:

$dropdown = array(
'name'=>'MyDropDown', 
'options' => array('Op1'=>'First Option', 'Op2' =>'Second Option')
);

echo form_dropdown($dropdown['name'],$dropdown['options']);

This renders as

<select name="MyDropDown">
<option value='Op1'>First Option</option>
<option value='Op2'>Second Option</option>
</select>

Is there a way for me to make Codeigniter render

<select name="MyDropDown">
<option value='Op1' data-image="filepath1">First Option</option>
<option value='Op2'  data-image="filepath2">Second Option</option>
</select>
3

There are 3 answers

0
Ambulare On

In case it helps anyone, I found a work-around using JQuery.

I made a javascript function that applied the data-image attribute to each option field once the page was ready, then called the msDropdown function afterwards.

function PiccifyShowDropdown(){

var Diagrams = new Array(
"/assets/images/icons/SixtyToHundredPercent.png",
"/assets/images/icons/LessThanThirtyPercent.png",
"/assets/images/icons/ThirtyToSixtyPercent.png",
"/assets/images/icons/SixtyToHundredPercent.png"
);

$("#Show > option").each(

    function()  {
        $(this).attr("data-image",Diagrams[this.index]);
                }

    );

$("#Show").msDropdown({visibleRows:2}); 


}

This seems to have worked, so now I just need someone to solve the same problem as this guy...

0
Kanishka Panamaldeniya On

You can't. You would need to extend CI's Form Helper and modify form_dropdown to accept other attributes like ID's

you will have to extend the helper .

to extend the native Form Helper you'll create a file named application/helpers/MY_form_helper.php, and add or override functions:

if you want to override function form_dropdown

simply write the function the way you want in MY_form_helper.php

here is the base function

if ( ! function_exists('form_dropdown'))
{
    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)
        {
            $key = (string) $key;

            if (is_array($val) && ! empty($val))
            {
                $form .= '<optgroup label="'.$key.'">'."\n";

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

                    $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
                }

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

                $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
            }
        }

        $form .= '</select>';

        return $form;
    }
}

you have to edit this part ,

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

    if (is_array($val) && ! empty($val))
    {
        $form .= '<optgroup label="'.$key.'">'."\n";

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

            $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
        }

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

        $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
    }
}

as you can see , only the option's value attribute is set by the function , you can edit this code and do the thing you want ,

try it , if you could not do it , tell me i ll help you , but first give it a try :)

0
Robin Castlin On

Consider doing something like this:

<script type="text/javascript">
    var filepath = <?=json_encode($dropdown['filepath'])?>;
</script>

$dropdown['filepath'] would use the option value as keys and store the filepath as the value. Then you can simply access filepath[$(this).val()] upon change event.

Example output:

<script type="text/javascript">
    var filepath = { 'Op1' : 'filepath1', 'Op2' : 'filepath2' };

    $('select').bind('change', function() {
        console.log(filepath[$(this).val()]);
    });
</script>

As much as I love using data- attributes, one must not forget other ways to achieve their goals.