Yii Framework Forum: Bootstrap Modal render function - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Bootstrap Modal render function How to use?

#1 User is offline   Federico Benedetti 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 165
  • Joined: 11-June 14
  • Location:Italy - Pesaro

Posted 03 December 2017 - 05:49 PM

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
0

#2 User is offline   wilson_xyz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 18-June 16

Posted 03 December 2017 - 10:28 PM

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.
0

#3 User is offline   Federico Benedetti 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 165
  • Joined: 11-June 14
  • Location:Italy - Pesaro

Posted 04 December 2017 - 02:59 AM

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').

View Postwilson_xyz, on 03 December 2017 - 10:28 PM, said:

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.

0

#4 User is offline   xPeng 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 04-August 15

Posted 04 December 2017 - 10:35 AM

View PostFederico Benedetti, on 04 December 2017 - 02:59 AM, said:

Something like Modal::render('myView') or Modal->render('myView').

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.yiiframew...trap-modal.html
1

#5 User is offline   Federico Benedetti 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 165
  • Joined: 11-June 14
  • Location:Italy - Pesaro

Posted 04 December 2017 - 12:55 PM

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!

View PostxPeng, on 04 December 2017 - 10:35 AM, said:

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.yiiframew...trap-modal.html

0

#6 User is offline   xPeng 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 04-August 15

Posted 04 December 2017 - 02:42 PM

View PostFederico Benedetti, on 04 December 2017 - 12:55 PM, said:

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:
1. make a new Modal:
<?php

namespace app\components;

class Modal extends \yii\bootstrap\Modal{
    
    public static function getContent($file){
        
        echo file_get_contents($file);
        
    }
}



2. 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(); 

0

#7 User is offline   xPeng 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 04-August 15

Posted 05 December 2017 - 08:52 AM

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(); 

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users