Yii2 bootstrapp模式没有弹出

时间:2017-10-09 10:09:07

标签: twitter-bootstrap yii2 twig

我正在使用yii2进行开发,但我遇到了一个问题 当我在视图中使用这些代码时,模态不会弹出(当我点击按钮时没有任何反应) 我不能使用

  <?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 个答案:

答案 0 :(得分:2)

试试这个:

 use yii\bootstrap\Modal;

不要忘记导入模态库:

.7mb.mp3

答案 1 :(得分:0)

我找到了我的解决方案,所以我会为有这个问题的人分享它

我在控制器中的行动:

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

所以我们可以用树枝做我们想要的任何事情,我们认为iniside ingridients-modal-content.twig或者向它发送params和..

in ingridients-modal-content.twig:

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

接下来在我们的main.php(或其他布局文件)中

<?php

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

?>

并在我们的主视图中打开模态,将在<?= $content ?>的main.php中呈现 在按钮中只需执行此操作:

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

它是