kv4nt/yii2repeater

为yii2添加和删除可重复输入元素组的接口

安装次数: 260

依赖者: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 0

公开问题: 0

类型:yii2-extension

v1.0.1 2021-02-16 08:04 UTC

This package is auto-updated.

Last update: 2024-09-16 15:36:22 UTC


README

类似于 http://briandetering.net/repeater

##如何使用

使用composer安装:composer require kv4nt/yii2repeater

将以下代码放入您的表单中

<?php echo Repeater::widget([
        'modelView' => '@app/modules/admin/views/***/repeater_view_file',
        'appendAction' => \yii\helpers\Url::to(['add-item-action']),
        'removeAction' => \yii\helpers\Url::to(['remove-item-action']),
        'form' => $form,
        'models' => $models, //The existing related model | example: $model->items
    ]) ?>

在您期望的控制器中,您需要放入以下代码

public function actions()
    {
        return [
            'add-item-action' => [
                'class' => 'kv4nt\yii2repeater\actions\AppendAction',
                'model' => 'app\models\Item',
                'contentPath' => '@app/modules/admin/views/***/repeater_view_file', //related to current controller
            ],
            'remove-item-action' => [
                'class' => 'kv4nt\yii2repeater\actions\DeleteAction',
                'model' => 'app\models\Item',
            ]
        ];
    }

这就是上面提到的repeater_view_file.php示例

<div class="repeater-content">
    <div class="form-group">
        <?= Html::label('Some label', Html::getInputId($model, "[$id]title")) ?>
        <?= Html::activeTextInput($model, "[$id]title", ['class' => 'form-control']) ?>
    </div>
    <div class="form-group">
        <?= Html::label('Other label', Html::getInputId($model, "[$id]other_attribute")) ?>
        <?= Html::activeTextInput($model, "[$id]other_attribute", ['class' => 'form-control']) ?>
    </div>
</div>