Cakephp选项标签属性

时间:2014-03-22 19:34:23

标签: cakephp cakephp-2.x

在视图中:

echo $this->Form->input('Ingredient');

上面填充了HTML输出的多个选择列表:

<select name="data[Ingredient][Ingredient][]" option="hh" multiple="multiple" id="IngredientIngredient">
<option value="1" selected="selected">Tomato</option>
<option value="2">Spaghetti </option>
<option value="3" selected="selected">Salt</option>
</select>

我需要知道的是如何向生成的<option>标记添加属性?

3 个答案:

答案 0 :(得分:5)

使用控制器传递选定的值:

if ($this->request->is('post') {
    // save form
} else {
    $this->request->data['Incredient']['Incredient'] = $ids;
}

请参阅here

要添加其他属性(如类),您只需要将其设置为更深的数组,然后就可以传递它们:

$options = array(
    1 => 'One', 
    2 => array('name' => 'Two', 'value' => 2,  'class' => 'extra'), 
    3 => 'Three');

echo $this->Form->input('test', array('type' => 'select', 'options' => $options));

结果:

<div class="input select">
    <label for="ModelTest">Test</label>
    <select name="data[Model][test]" id="ModelTest">
        <option value="1">One</option>
        <option value="2" class="extra">Two</option>
        <option value="3">Three</option>
    </select>
</div>

请参阅this

答案 1 :(得分:0)

我的示例自定义标记选项在select(by CakePHP 2 + VueJs 2)中很容易。

<?php
            $options = array(
                array(
                    'value' => false,
                    'v-for' => 'obj in data_options_input_id_vuejs',
                    'name' => '{{obj.text}}',
                    'v-bind:value' => 'obj.value',
                )
            );
            echo $this->Form->input('input_id', array(
                'type' => 'select',
                'required' => true,
                'class' => 'input-block-level',
                'options' => $options,
                'v-model:' => 'productos_stocks_padre_combo_select',
            ));
?>

答案 2 :(得分:0)

Add extra data to option

大家好,在我的情况下,我想在我的选择选项中添加code =“code-value”,这段代码对我有用。 如果您想为您的选项添加额外数据,请尝试以下代码:

    //In controller 

    $chapters = $this->Course->Chapter->find('all', array('fields' => array('name', 'code', 'id')));

            // Call the noop function $this->noop() on every element of chapters
            $chapters = Hash::map($chapters, "{n}.Chapter", array($this, 'noop'));

// Callback noop function
    function noop($option) {

            $option['value'] = $option['id'];
            unset($option['id']);
            return $option;
        }

您可以看到有关Hash的文档:map function here。祝你好运。