Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
kv4nt committed Feb 4, 2019
1 parent eb986c9 commit 448b0e8
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/nbproject/private/
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Che Guevara
Copyright (c) 2016 relbraun

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# yii2-repeater

Repeater like http://briandetering.net/repeater

##How to use

install using composer:
`composer require kv4nt/yii2repeater`

put this code in your form:
```
<?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
]) ?>
```
in your desired controller ypu have to put tis code:
```
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',
]
];
}
```
And this is an example of the repeater_view_file.php reminded above:
```
<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>
```
68 changes: 68 additions & 0 deletions Repeater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace kv4nt\yii2repeater;

use Yii;
use yii\base\Model;
use yii\base\Widget;
use yii\db\ActiveRecord;
use yii\helpers\Json;
use yii\widgets\ActiveForm;

class Repeater extends Widget
{

/**
* @var ActiveRecord[]
*/
public $models;

/**
* @var string The view path to render repeater
*/
public $modelView;

/**
* @var string The new item action url to run in ajax
*/
public $appendAction;

/**
* @var string The remove item action url to run in ajax
*/
public $removeAction;

/**
* @var ActiveForm Optional, if you want to use the $form variable.
*/
public $form;

/**
* @var array Key - value params to append to the view file
*/
public $additionalData = [];

public function init()
{
parent::init(); // TODO: Change the autogenerated stub
}

public function run()
{
$view = $this->getView();
$this->view->registerJs('var deleteQuestionMessage="' . Yii::t('yii2-repeater', 'Do you really want to delete this item?') . '";', yii\web\View::POS_HEAD);
RepeaterAsset::register($view);
$data = Json::encode(['append' => $this->appendAction, 'remove' => $this->removeAction]);
echo "<div class='ab-repeater'>";
echo "<div class='list-area'>";
foreach ($this->models as $k => $model)
{
$content = $this->render($this->modelView, array_merge(['model' => $model, 'form' => $this->form, 'id' => $model->id], $this->additionalData));
echo $this->render('repeater', ['content' => $content, 'model' => $model, 'id' => $model->id]);
}
echo "</div><div class='row'><div class='col-xs-12 m-t-10 m-l-5'><a class='btn btn-primary new-repeater' href='javascript:;'>" . Yii::t('yii2-repeater', 'Add new item') . "</a> <a href='javascript:;' class='btn btn-danger recover-btn'>" . Yii::t('yii2-repeater', 'Recover') . "</a></div></div></div>";
$js = "new window.repeater($data)";
$this->view->registerJs($js);
}

}
23 changes: 23 additions & 0 deletions RepeaterAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace kv4nt\yii2repeater;

use yii\web\AssetBundle;

class RepeaterAsset extends AssetBundle
{

public $sourcePath = __DIR__;
public $basePath = '@app/web';
public $js = [
'js/js.js',
];
public $css = [
'css/style.css',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];

}
24 changes: 24 additions & 0 deletions actions/AppendAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace kv4nt\yii2repeater\actions;

use yii\base\Action;

class AppendAction extends Action
{

/**
* @var string full name of Model class
*/
public $model;
public $contentPath;

public function run()
{
$this->controller->viewPath = dirname(__DIR__) . '/views';
$id = \Yii::$app->request->post('id');
$model = new $this->model();
return $this->controller->renderPartial('repeater', ['model' => $model, 'contentPath' => $this->contentPath, 'id' => $id]);
}

}
34 changes: 34 additions & 0 deletions actions/DeleteAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace kv4nt\yii2repeater\actions;

use Yii;
use yii\base\Action;
use yii\db\ActiveRecord;
use yii\web\Response;

class DeleteAction extends Action
{

/**
* @var string full name of Model class
*/
public $model;

public function run()
{
$id = \Yii::$app->request->post('id');
$model = $this->model;
/** @var ActiveRecord $model */
$model = $model::findOne($id);
$response = 0;
if ($model)
{
$response = $model->delete();
}

Yii::$app->response->format = Response::FORMAT_JSON;
return ['status' => $response];
}

}
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "kv4nt/yii2repeater",
"description": "An interface to add and remove a repeatable group of input elements for yii2",
"license": "MIT",
"homepage": "https://bitbucket.org/sagabeta/repeater",
"type": "yii2-extension",
"require": {
"php": "^5.4.3 || ^7.0",
"yiisoft/yii2-bootstrap": "*"
},
"autoload": {
"psr-4": {
"kv4nt\\yii2repeater\\": ""
}
},
"minimum-stability" : "stable",
"license": "MIT"
}
Empty file added css/style.css
Empty file.
49 changes: 49 additions & 0 deletions js/js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Created by Arielb on 11/24/2016.
*/
jQuery(function($){

var Repeater = function(url){
var appendUrl = url.append,
deleteUrl = url.remove;
var self = this;
this.id = 1;
this.archive = [];
var $wrap = $('.ab-repeater .list-area'),
$recover = $('.ab-repeater .recover-btn');
this.recover = function(){
$wrap.append(this.archive.pop());
if(this.archive.length === 0){
$recover.prop('disabled', true);
}
};
$(document).on('click', '.repeater-item .remove', function(){

if(confirm(deleteQuestionMessage)){
self.archive.push($(this).parents('.repeater-item').clone());
var $item = $(this).parents('.repeater-item');
var data ={id:$item.data('id')};
$.post(deleteUrl,data, function(data){
$item.remove();
$recover.prop('disabled', false);
});
}
});

$('.new-repeater').click(function(){
var data ={id:self.id};
data[yii.getCsrfParam()]=yii.getCsrfToken();
data.additionalData = $('.repeater-item').find('input,select,textarea').serialize();
data.id = self.id;
$.post(appendUrl,data, function(data){
$wrap.append($(data));
});
self.id++;
});
$recover.click(function(){
self.recover();
})
};

window.repeater = Repeater;
});
20 changes: 20 additions & 0 deletions views/repeater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/* @var $this \yii\web\View */
/* @var $form \yii\widgets\ActiveForm */
?>

<div class="repeater-item" data-id="<?= $id ?>">
<div class="row">

<?php
if (isset($contentPath))
{
$content = $this->render($contentPath, ['model' => $model, 'id' => $id]);
}
?>
<?= $content ?>
<div class="col-xs-1">
<a class="remove btn btn-danger" href="javascript:;" title="Удалить">X</a>
</div>
</div>
</div>

0 comments on commit 448b0e8

Please sign in to comment.