2017-10-09 62 views
0

我使用yii2進行開發,但我遇到了一個問題 當我在視圖中使用這些代碼時,模式沒有彈出(當我點擊按鈕時沒有任何反應) 我無法使用Yii2 bootstrapp模式沒有彈出

<?php 
     Modal::begin([ ... 

,因爲我在一個.twig文件,也是網頁加載嵌入PHP成本的速度做這些

   <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"> 
        Button</button> 

<!-- Modal content--> 
    <div class="modal-content"> 
    <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal">&times;</button> 
     <h4 class="modal-title">header</h4> 
    </div> 
    <div class="modal-body"> 
//just html stuff 

    </div> 
    <div class="modal-footer"> 
     <button type="button" class="btn btn-success" data-dismiss="modal" style="width:100%">Close</button> 
    </div> 
    </div> 

</div> 

回答

2

試試這個:

<?php 
Modal::begin([ 
    'header' => '<h2>Show Modal</h2>', 
    'toggleButton' => false, 
    'id' => 'modal-opened', 
    'size' => 'modal-lg' 
]); 

echo 'Modal Opened'; 

Modal::end(); 
?> 

<?= Html::button('Open Modal', ['id' => 'modal-btn', 'class' => 'btn btn-success']) ?> 

<?php 

$this->registerJs(
    <<<JS 
     $('#modal-btn').on('click', function (event) { 
       $('#modal-opened').modal('show'); 
     }); 
    JS 
    ); 
?> 

不要忘記導入模態庫:

use yii\bootstrap\Modal; 
0

好,我發現我的解決方案,所以我會爲任何人有這個分享問題

我在控制器中的動作:

$modalView = $this->renderPartial('ingredients-modal-content.twig'); 
$this->view->params['modalView'] = $modalView; 
//and another action codes like rendering mainview and etc 

,所以我們可以做任何我們想做的與樹枝和我們的觀點,即iniside ingridients模態,content.twig或發送PARAMS給它..

ingridients模態-content.twig:

在我們的main.php
<form action="/cars/add-cars" method="post" class="form-group" id="repairmen"> 
<div class="col-md-4 pull-right" style ="padding:0px; " > 
    <input type="submit" id="repairmen" class="btn btn-success" style="width:100%;" value = "Add"></input> 
</div> 
<div class="col-md-4 pull-right" style ="padding:0px;" > 
    <input type="text" class="form-control" id="repairmen_name" placeholder="ID" style = "text-align:center;" name="product_id" > 
</div> 
<div class="col-md-4 pull-right" style ="padding:0px;" > 
    <input type="text" class="form-control" id="repairmen_name" placeholder="Name" style = "text-align:center;" name="product_name" > 
</div> 
</form> 
<table class="table" style=" 
    direction: rtl; 
    text-align: right !important; 
"> 

<thead> 
<tr> 
    <th>ID</th> 
    <th>Name</th> 
    <th>Price</th> 

</tr> 
</thead> 
<tbody> 
<tr> 
<td>{{item.ID}}</td> 
<td>{{item.Name}}</td> 
<td>{{item.Price}}</td> 
</tr> 
</tbody> 
</table> 
<button type="button" class="btn btn-success" data-dismiss="modal" style="width:100%">submit</button> 

下一個(或其他佈局文件)

<?php 

Modal::begin([ 
    'header' => '<h4>title</h4>', 
    'id'  => 'model', 
    'size' => 'model-lg', 
]); 
    if (isset($this->params['modalView'])){ 
echo $this->params['modalView']; 
    }; Modal::end(); 

?> 

,並在我們的MAINVIEW開放模式將在main.php呈現在<?= $content ?> 在按鈕只是這樣做:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#model"> 
button name</button> 

就是這樣