diff --git a/DynamicRelations.php b/DynamicRelations.php index 399a55a..a9e0147 100644 --- a/DynamicRelations.php +++ b/DynamicRelations.php @@ -13,6 +13,7 @@ class DynamicRelations extends Widget public $collection; public $collectionType; public $viewPath; + public $params; public function init(){ parent::init(); @@ -32,13 +33,14 @@ public function run(){ } $key = "dynamic-relations-$type"; $hash = crc32($key); - Yii::$app->session->set('dynamic-relations-'.$hash, [ 'path'=>$this->viewPath, 'cls'=>$type ]); + Yii::$app->session->set('dynamic-relations-'.$hash, [ 'path'=>$this->viewPath, 'cls'=>$type , 'params' => $this->params]); return $this->render('template', [ 'title' => $this->title, 'collection' => $this->collection, 'viewPath' => $this->viewPath, 'ajaxAddRoute' => Url::toRoute(['dynamicrelations/load/template', 'hash'=>$hash]), + 'params' => $this->params, ]); } @@ -55,10 +57,11 @@ public function uniqueOptions($field, $uniq) public static function relate($model, $attr, $request, $name, $clsname) { - if($request[$name]) + if(array_key_exists($name,$request)) { - if($new = $request[$name]['new']) + if(array_key_exists('new',$request[$name])) { + $new = $request[$name]['new']; foreach( $new as $useless=>$newattrs) { $newmodel = new $clsname; diff --git a/README.md b/README.md index a7b6959..69ec5b4 100755 --- a/README.md +++ b/README.md @@ -193,6 +193,7 @@ use synatree\dynamicrelations\DynamicRelations; 'title' => 'Business Hours', 'collection' => $model->hours, 'viewPath' => '@app/views/business-hours/_inline.php', + 'params' => ['firstParam' => $firstParam , 'anotherParam' => $anotherParam], // this next line is only needed if there is a chance that the collection above will be empty. This gives the script a prototype to work with. 'collectionType' => new \app\models\BusinessHours, diff --git a/assets/js/dynamic-relations.js b/assets/js/dynamic-relations.js index 06672b4..0e83266 100644 --- a/assets/js/dynamic-relations.js +++ b/assets/js/dynamic-relations.js @@ -31,11 +31,11 @@ jQuery(document).ready(function () { jQuery('.add-dynamic-relation').on('click', function(event){ event.preventDefault(); var me = this; - view = jQuery(me).closest('[data-related-view]').attr('data-related-view') + "&t=" + ( new Date().getTime() ); + view = jQuery(me).next('[data-related-view]').attr('data-related-view') + "&t=" + ( new Date().getTime() ); jQuery.get(view, function(result){ $result = jQuery(result); - li = jQuery(me).closest('li').clone().empty(); ul = jQuery(me).closest('ul'); - ul.append( li ); + jQuery(me).next('ul.list-group').append('
  • '); + li = jQuery(me).next('ul.list-group').children().last(); li.append( $result.filter("#root") ); $result.filter('script').each(function(k,scriptNode){ if(!scriptNode.src || !scriptLoaded( scriptNode.src ) ) diff --git a/controllers/LoadController.php b/controllers/LoadController.php index 568c8ce..b54c78c 100644 --- a/controllers/LoadController.php +++ b/controllers/LoadController.php @@ -13,6 +13,7 @@ public function actionTemplate($hash) { echo $this->render( $args['path'], [ 'model' => new $args['cls'], + 'params' => $args['params'], ]); } diff --git a/views/template.php b/views/template.php index cc08473..03f7a15 100644 --- a/views/template.php +++ b/views/template.php @@ -1,28 +1,48 @@ registerCSS( + "h2.panel-title a{text-decoration:none; color:inherit;font-size:26px} + h2.panel-title a i{font-size:14px;}" + ); + $this->registerJs( + "$('h2.panel-title a').on('click',function (){ + $(this).children('i').toggleClass('glyphicon-chevron-down glyphicon-chevron-up'); + });" + , View::POS_READY + ,"template" + ); ?> - - - +
    +
    +

    + + + +

    +
    +
    +
    + + + +
      + +
    • + +
      + renderFile( $viewPath, [ 'model' => $model, 'params' => $params, ]); ?> +
      +
    • + +
    +
    +
    +
    \ No newline at end of file