Lordfef
(Account)
December 3, 2017, 10:49pm
1
Hi all, i read Yii2 Bootstrap Modal documentation and i see the render method, but i can’t find the right way to use.
I need to render a view into my modal.
For now i find a working different solution:
Modal::begin([
'id'=>'modal_PrivacyPolicy',
'size'=>'modal-lg',
]);
echo "<div id='modalContent'>";
echo \Yii::$app->view->render('@app/views/privacy/_privacy_policy');
echo "</div>";
Modal::end()
but i think there’s a better way using the modal render method.
Any suggestion?
Thank’s
wilsonim
(Wilson Im)
December 4, 2017, 3:28am
2
this is my code, you could refer to it.
button to show modal
<?= Html::button('Price List', [
'value' => Yii::$app->urlManager->createUrl('/some-controller/some-action'),
'class' => 'btn btn-primary',
'id' => 'BtnModalId',
'data-toggle'=> 'modal',
'data-target'=> '#your-modal',
]) ?>
define modal outside active form
<?php ActiveForm::end(); ?>
<?php
Modal::begin([
'header' => 'your-header',
'id' => 'your-modal',
'size' => 'modal-md',
]);
echo "<div id='modalContent'></div>";
Modal::end();
?>
your javascript
$('#BtnModalId').click(function(e){
e.preventDefault();
$('#your-modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));
return false;
});
hope it helps.
Lordfef
(Account)
December 4, 2017, 7:59am
3
Thank’s wilson_xyz i use often this kind of solution, expecially when i need ajax return. But i think there’s a better solution using modal render method. Something like Modal::render(‘myView’) or Modal->render(‘myView’).
this is my code, you could refer to it.
button to show modal
<?= Html::button('Price List', [
'value' => Yii::$app->urlManager->createUrl('/some-controller/some-action'),
'class' => 'btn btn-primary',
'id' => 'BtnModalId',
'data-toggle'=> 'modal',
'data-target'=> '#your-modal',
]) ?>
define modal outside active form
<?php ActiveForm::end(); ?>
<?php
Modal::begin([
'header' => 'your-header',
'id' => 'your-modal',
'size' => 'modal-md',
]);
echo "<div id='modalContent'></div>";
Modal::end();
?>
your javascript
$('#BtnModalId').click(function(e){
e.preventDefault();
$('#your-modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));
return false;
});
hope it helps.
xPeng
(Xunpeng)
December 4, 2017, 3:35pm
4
you can use any method of Modal like this:
$modal = Modal::begin([
'header' => '<h2>Hello world</h2>',
'toggleButton' => ['label' => 'click me','url' => ['/index']],
]);
$modal-> (any method of Modal such as setView()/getView() render()/renderFile());
Modal::end();
Methods of Modal:
http://www.yiiframework.com/doc-2.0/yii-bootstrap-modal.html
Lordfef
(Account)
December 4, 2017, 5:55pm
5
Thank’s xPeng is what exactly i need.
Now that i look the code is quite simple solution, but it doesn’t come in my mind
I read the documentation you link but there’s no expressly example.
Thank’s!
xPeng:
you can use any method of Modal like this:
$modal = Modal::begin([
'header' => '<h2>Hello world</h2>',
'toggleButton' => ['label' => 'click me','url' => ['/index']],
]);
$modal-> (any method of Modal such as setView()/getView() render()/renderFile());
Modal::end();
Methods of Modal:
http://www.yiiframework.com/doc-2.0/yii-bootstrap-modal.html
xPeng
(Xunpeng)
December 4, 2017, 7:42pm
6
Thank’s xPeng is what exactly i need.
Now that i look the code is quite simple solution, but it doesn’t come in my mind
I read the documentation you link but there’s no expressly example.
Thank’s!
Sometimes the Yii isn’t Yii( yes it is , in chinese: 易)。You can always make your own code to “Yiisy” your work. For Example:
make a new Modal:
<?php
namespace app\components;
class Modal extends \yii\bootstrap\Modal{
public static function getContent($file){
echo file_get_contents($file);
}
}
use the new Modal in your sense:
<?php
use app\components\Modal;
Modal::begin([
'header' => '<h2>Hello world</h2>',
'toggleButton' => ['label' => 'click me','url' => ['/index']],
]);
Modal::getContent('yourFile');
Modal::end();
xPeng
(Xunpeng)
December 5, 2017, 1:52pm
7
I would like this one personally:
Modal::begin([
'header' => '<h2>Hello world</h2>',
'toggleButton' => ['label' => 'click me','url' => ['/index']],
]);
echo file_get_contents($file);
Modal::end();