CodeIgniter:如何在选项form_dropdown中添加属性

时间:2015-05-11 09:43:38

标签: php html codeigniter

在codeigniter中的代码

for line in file:    
   for char in line:

它生成像这样的HTML代码

$options = array(
    0  => '--Gender--',
    1  => 'Male',
    2  => 'Female',
    3  => 'Don't know',
);
echo form_dropdown('gender', $options, 0, 'class="form-control"');

我希望将性别选项禁用 如何添加属性disabled =" disabled"选项性别?喜欢这个代码

<div class="form-group">
    <select name="jenis_kelamin" class="form-control">
        <option value="0" selected="selected">--Gender--</option>
        <option value="1">Male</option>
        <option value="2">Female</option>
        <option value="3">Don't know</option>
    </select>
</div>

4 个答案:

答案 0 :(得分:0)

尝试生成类似 -

的选项
$options = array(
    '0" disabled'  => '--Gender--',
    1  => 'Male',
    2  => 'Female',
    3  => 'Don't know',
);

或者在这种情况下,您也可以手动生成它。

答案 1 :(得分:0)

在此目录中创建MY_form_helper.phpCI_ROOT/application/helpers/MY_form_helper.php并将此代码保存到其中:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Author: RezaAmya@gmail.com
 * Website: www.amya.ir
 */

// --------------------------------------------------------------------

if ( ! function_exists('form_dropdown'))
{
    /**
     * Drop-down Menu
     *
     * @param   mixed   $data
     * @param   mixed   $options
     * @param   mixed   $selected
     * @param   mixed   $extra
     * @param   mixed   $attr
     * @return  string
     */

    function form_dropdown($data = '', $options = array(), $selected = array(), $extra = '', $attr = 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) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';

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

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

            if (is_array($val) && $key == 'optgroup')
            {
                if (empty($val))
                {
                    continue;
                }

                $form .= '<optgroup label="'.$val['lable']."\">\n";

                foreach ($val as $optgroup_key => $optgroup_val)
                {
                    $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
                    $form .= '<option value="'.html_escape($optgroup_key).'"'.$sel
                        .$attr_html.'>'
                        .(string) $optgroup_val."</option>\n";
                }

                $form .= "</optgroup>\n";
            }
            else
            {
                //manage options attributes
                if (array_key_exists($key,$attr)) {
                    if (is_array($attr[$key]))
                    {
                        foreach ($attr[$key] as $attr_name => $attr_value)
                        {
                            $attr_html .= ' '.html_escape($attr_name) .'="'.(string)$attr_value.'"'.' ';
                        }
                    }
                    else
                    {
                        $attr_html = $attr[$key];
                    }
                }

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

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


/* Sample of usage in your code */
        $temp = 'type';

        $options = array(
            'val1'=>'item1',
            'val2'=>'item2'
        );

        $attr = array(
            'val1'=>'attr_name1="attr_value1" attr_name2="attr_value2"',
            'special'=> array(
                'attr_3'=>'attr_value3',
                'attr_4'=>'attr_value4',
                'example_attr'=>'www.amya.ir'
            )
        );

        $extra = array(
            'id'=> $temp,
            'class'=> "form-control show_hide"
        );

        echo form_dropdown($temp, $options, set_value($temp, 'val2'), $extra, $attr);

它应该工作
玩得很开心

答案 2 :(得分:0)

我遇到了同样的问题。你的问题帮助我开发了解决问题的方法

我的解决方案是使用jquery并按文件加载的标签和值选择元素。

<script type="text/javascript">

  $(document).ready(function(){

    $('option[value$="0"]').prop("disabled", true);
  });

</script>

答案 3 :(得分:0)

application/helper目录中创建MY_helper.php文件并在application/autoload.php内加载新帮助程序,然后添加此自定义form_dropdown()函数:

    function my_form_dropdown($data = '', $options = array(), $selected = array(), $disabled = array(), $hidden= array(), $extra = '')
{
    $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
        }

        if (isset($data['disabled']))
        {
            $disabled = $data['disabled'];
            unset($data['disabled']); // select tags don't use an disabled attribute
        }

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

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

    // 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) > 1 && 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"' : '';
                $dis = in_array($optgroup_key, $disabled) ? ' disabled' : '';
                $hid = in_array($optgroup_key, $hidden) ? ' hidden' : '';
                $form .= '<option value="'.html_escape($optgroup_key).'"'.$sel.$dis.$hid.'>'
                    .(string) $optgroup_val."</option>\n";
            }

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

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

该函数与核心下拉函数的工作方式相同,但使用新的两个参与者(disabled)(hidden),其中u可以设置一组禁用和隐藏选项。

my_form_dropdown()函数的用法:

$data = ['name' => 'countries', class='form-control'];
$options = [
 '' => '-Select Country-',
 'burundi' => 'Burundi',
 'comoros' => 'Comoros',
 'congo' => 'D.R Congo'
];
 echo my_form_dropdown($data, $options, '', '', '');