-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kv4nt
committed
Feb 4, 2019
1 parent
eb986c9
commit 448b0e8
Showing
11 changed files
with
287 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/nbproject/private/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |