如何创建yii2多个字段? [关闭]

Have a problem with yii2 How i can handle multiple fields? My view must be like this link http://bootsnipp.com/snippets/featured/multiple-fields Please can someone provide an example

I think you should using javascript to create fields and php form (not using yii).

Example:

The Html:

<div class="input">
    <div id="container"></div>
    <span class='add-input' href="#" name="name" onclick='addFields()'>Add</span>
</div>

The JS function:

function addFields(){
    var container_parent = document.getElementById('container');

    var div = document.createElement("div");
    div.name = "div_element";
    container_parent.appendChild(div);

    var new_field = document.createElement("input");
    new_field.name = 'new_name[]';
    div.appendChild(new_field);
}

Here I set name 'new_name[]' to when receive the post data, we using foreach ($_POST['new_name'] as value) to get all data.

If create select element:

function addFields(){
    var container_parent = document.getElementById('container');

    var div = document.createElement("div");
    div.name = "div_element";
    container_parent.appendChild(div);

    var new_field = document.createElement("select");
    new_field.name = 'new_name[]';
    div.appendChild(new_field);
    new_field.innerHTML = '<?php echo $option_items; ?>';
}

Here I add options value created by yii form:

$option_items =  $form->dropDownList($model, 'name', $allItems, array('prompt'=>'')); //get dropdownlist by yii
//remove data, just hold option value
$pos = strpos($option_items, "<option");
$rpos = strrpos($option_items, "</option>");
$option_items = substr($option_items, 0, $rpos+9);
$option_items = substr($option_items, $pos, strlen($option_items) - $pos);
$option_items = str_replace("
", "", $option_items);

Or you can add your options value at:

new_field.innerHTML = '<?php echo $option_items; ?>';

You can modify to it cooler, good luck, sorry if my English is bad :)