courses_field.php 3.78 KB
<?php
    use yii\helpers\Html;

?>
<fieldset>

    <div class="skills-title">Пройденные курсы, тренинги</div>



    <div class="field_list">
    <?php $t = 0;
    $label = 0; // add this var
        for($i = 1; $i <= count($model); $i++):
            $row = $i;

            ?>

            <?= Html::beginTag('div', [
            'class' => 'form-group',
            'id'    => isset( $model[ $i ][ 'parent_key' ] ) ? $model[ $i ][ 'parent_key' ] : 0,
        ]) ?>
            <div class="input-blocks">
                <input id="cours-name-<?= ++$label  ?>" type="text" placeholder="" class="form-control custom-input-2" value="<?= isset( $model[ $t ][ 'value' ] ) ? $model[ $t ][ 'value' ] : '' ?>" name="Fields[courses][<?= $row ?>][0][name]"/>
                <label for="cours-name-<?= $label ?>">Название</label>
            </div>
            <div class="input-blocks">
                <input id="cours-year-<?= ++$label  ?>" type="number" class="form-control custom-input-2 custom-input-2-date" value="<?= isset( $model[ ++$t ][ 'value' ] ) ? $model[ $t ][ 'value' ] : '' ?>" name="Fields[courses][<?= $row ?>][1][year]" min="1950" max="<?=date('Y')?>" />
                <label for="cours-year-<?= $label ?>">год</label>
            </div>
            <span data-id="<?= isset( $model[ $i ][ 'parent_key' ] ) ? $model[ $i ][ 'parent_key' ] : 0 ?>" title="<?= Yii::t('app','add') ?>" class="glyphicon glyphicon-trash delete-field-item"></span>
            <?= Html::endTag('div') ?>
            <?php $i = ++$t; ?>
        <?php endfor; ?>
    </div>
    <p class="btn btn-success add_field_<?= $this->context->id ?>"><?= Yii::t('app', 'add_field') ?></p>
</fieldset>
<script>
    var start_i_<?=$this->context->id?> = <?=$i?>;
    var start_label_<?=$this->context->id?> = <?=$label?>; // add this var
    $ (document)
        .ready (
            function ()
            {
                $ ('.add_field_<?=$this->context->id?>')
                    .click (
                        function ()
                        {
                            var block     = $ (this)
                                .siblings('.field_list');
                            var block_id  = $ (this)
                                .parent ('fieldset');
                            var sub_block = '<div class="form-group" >' +
                                            '<div class="input-blocks">'+
                                            '<input id="cours-name-' + ++start_label_<?=$this->context->id?> +'" type="text" placeholder="" class="form-control custom-input-2" value="" name="Fields[courses][' + start_i_<?=$this->context->id?>++ + '][0][name]" />' +
                                            '<label for="cours-name-' + start_label_<?=$this->context->id?> +'">Название</label>'+
                                            '</div>'+
                                            '<div class="input-blocks">'+
                                            '<input id="cours-year-' + ++start_label_<?=$this->context->id?> +'" type="number" class="form-control custom-input-2 custom-input-2-date" value="" name="Fields[courses][' + start_i_<?=$this->context->id?>++ + '][1][year]"  min="1950" max="<?=date('Y')?>" />' +
                                            '<label for="cours-year-' + start_label_<?=$this->context->id?>  +'">год</label>'+
                                            '</div>'+
                                            '<span title="<?= Yii::t('app','add') ?>" class="glyphicon glyphicon-trash delete-field-item"></span>' +
                                            '<div>';
                            block.append (sub_block);
                        }
                    );
            }
        );
</script>