Yii2 bootstrapp模式没有弹出
问题描述:
我使用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">×</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>
答
试试这个:
<?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;
答
好,我发现我的解决方案,所以我会为任何人有这个分享问题
我在控制器中的动作:
$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>
就是这样