mix8872/yii2-repeater

HTML 重复器(分支的)

安装: 8

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 1

类型:yii2-extension

1.0 2019-10-06 12:13 UTC

This package is auto-updated.

Last update: 2024-09-09 00:17:01 UTC


README

您可以使用此扩展来复制表单内容以及其他内容

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

php composer.phar require mix8872/yii2-repeater

或者在您的 composer.json 文件的 require 部分添加

"mix8872/yii2-repeater": "*"

配置

将模块声明添加到您的 web 配置文件中

<?php

return [
    // ... your config
    'modules' => [
        'repeater'=> [
            'class' => \mix8872\repeater\Repeater::class
        ],
    ],
    'bootstrap' => [        
        'repeater' // add module id to bootstrap for proper aliases and url routes binding
    ]

用法

Single column example

示例视图

<?= \mix8872\repeater\widgets\RepeaterWidget::widget([
    'className' => \app\models\TestRepeater::class,
    'modelView' => '@app/views/site/repeater',
    'models' => [ //list of your models
        new \app\models\TestRepeater(),

    ],
    'btnNewTitle' => 'Add main container',
    'addCallback' => new \yii\web\JsExpression("
        function(data){
           console.log(data);
        }
    "),
    'removeCallback' => new \yii\web\JsExpression("
        function(data){
           console.log(data);
        }
    ")
]);?>

示例模型

<?php 
    class TestRepeater extends Model
    {
        public $title;
        public $text;
    
        public function rules()
        {
            return [
                [['date', 'text'], 'safe']
            ];
        }
    }
?>

模型示例视图

<?php

/** @var \app\models\TestRepeater $model */
/** @var int $id */

?>
<table border="1" width="100%">
    <tbody>
        <tr>
            <td>
                <div class="form-group field-posts-title required has-success">
                    <?= \yii\helpers\Html::activeTextInput($model, "title[$id]")?>
                </div>
        
                <?= CKEditor::widget([
                    'name' => "TestRepeater[text][$id]",
                    'id' => 'cke' . $id,
                    'options' => ['rows' => 3],
                    'preset' => 'basic'
                ])?>
            </td>
        </tr>
        <tr>
            <td>
                <?= \mix8872\repeater\widgets\RepeaterWidget::widget([
                    'className' => \app\models\TestRepeaterTwo::class,
                    'modelView' => '@app/views/site/repeaterTwo',
                    'models' => [
                        new \app\models\TestRepeaterTwo(),
                    ],
                    'additionalData' => [
                        'container' => "[two][$id]",
                        'parentId' => $id
                    ],
                    'btnNewTitle' => 'Add second container',
                    'addCallback' => new \yii\web\JsExpression("
                    function(data){
                       console.log(data);
                    }
                "),
                    'removeCallback' => new \yii\web\JsExpression("
                    function(data){
                       console.log(data);
                    }
                ")
                ]);?>
            </td>
        </tr>
    </tbody>
</table>