yii2 modal loading content but header modal can not see it - yii2

I can not understand why I do not show well the modal can you help?
Modal
<?php
Modal::begin([
'header' => '<h2>Accedi</h2>',
'id' => 'modalSmall',
'size' => 'modal-sm',
//keeps from closing modal with esc key or by clicking out of the modal.
// user must click cancel or X to close
'clientOptions' => ['backdrop' => 'static', 'keyboard' => true],
'footer' => 'fdd'
]);
echo "<div id='modalContent'></div>";
Modal::end();
?>
JS
$(function() {
$('#modalButton a').click(function(e) {
e.preventDefault();
$('#modalSmall').modal('show')
.find('.modal-content')
.load($(this).attr('href'));
});
});
what I see
there is not the close button

<?php
Modal::begin([
'header' => '',
'headerOptions' => ['id' => 'modalHeader'],
'id' => 'modalSmall',
'size' => 'modal-medium', //medium
'clientOptions' => ['backdrop' => 'static', 'keyboard' => false]
]);
echo "<div id='modalContent'>
<div class='col-lg'>
<img src='".Yii::$app->request->baseUrl."/loading.gif' width='280' height='210' alt='loading...'> // put loading.gif in backend/web/ directory
</div>
</di>";
Modal::end();
?>
$(function() {
$('#modalButton').click(function(e) {
e.preventDefault();
$('#modalSmall').modal('show')
.find('.modal-content')
.load($(this).attr('value'));
document.getElementById('modalHeader').innerHTML = '<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button><h3>' + $(this).attr('title') + '</h3>';
});
});
How to call?
use yii\helpers\Url;
<p>
<?= Html::a(Yii::t('app', 'Add Something'), null, ['class' => 'btn btn-success', 'id'=>'modalButton',
'value'=>Url::to(['/controller/action']),
'title'=>'Show in Modal header'
])
?>
</p>

Related

Modal Form not work in Yii2 with AdminLTE

I built a web app using Yii2 with adminLTE theme. i'm trying to make modal form in my web app, I've tried in Yii2 without adminLTE it work fine like this:
I did the same way in Yii2 with adminLTE it failed, when I clicked the button, it didn't do anything, and I tried to do inspectElement and I got this:
This is my view in index.php:
<?php
use yii\helpers\Html;
use kartik\detail\DetailView;
use kartik\grid\GridView;
use yii\helpers\Url;
use yii\bootstrap\Modal;
/* #var $this yii\web\View */
/* #var $modelTrip backend\models\TripsSchedule */
\yii\web\YiiAsset::register($this);
?>
<p>
<?= Html::button('Add Schedule', ['value' => Url::to('/intra/admin/bus-passengers/create'), 'class' => 'btn btn-success', 'id' => 'modalButton']) ?>
</p>
<?php
Modal::begin([
'header' => '<h4>Add Passenger</h4>',
'id' => 'modal',
'size' => 'modal-lg',
]);
echo "<div id='modalContent'><div>";
Modal::end();
?>
<div>
<?=
DetailView::widget([
'model' => $modelTrip,
'id' => $modelTrip->tripScheduleId,
'responsive' => true,
'enableEditMode' => false,
'condensed' => true,
'hover' => true,
'mode' => DetailView::MODE_VIEW,
'mainTemplate' => '{detail}',
'attributes' => [
[
'attribute' => 'departureTime',
'label' => 'Departure Time',
'value' => function ($form, $widget) {
$model = $widget->model;
return date('H:i', strtotime($model->departureTime)) . ' WIB';
},
],
],
])
?>
</div>
This is code in controller:
public function actionCreate()
{
$model = new BusPassengers();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->busPassengerId]);
}
return $this->renderAjax('create', [
'model' => $model,
]);
}
You can see, it didn't render anything. Do I need add another additional code because I'm using adminLTE?
Update
Here is the js:
$(function () {
$('#modalButton').click(function () {
$('#modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));
});
});
Update
Here is the view source and console screenshot
This line is not needed if you already have a layout file, so please remove this
\yii\web\YiiAsset::register($this);
also please make sure jquery file loaded before your js code else it will not work since bootstrap modal is depend on jQuery (please check your console).
Or at the end, your can add js code as Yii way:
<?php
$script = <<< JS
$('#modalButton').click(function () {
$('#modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));
});
JS;
$this->registerJs($script);
?>
Let me know if it work or please post screenshot of viewSource and console.
Thank you
Update
Try replace these code block and it will work:
1. JS
<script type="text/javascript">
function showModal() {
$('#passenger_modal').modal('show');
$('#passenger_modal_content').load("<?=Yii::getAlias('#web')?>/intra/admin/bus-passengers/create");
}
</script>
2. BUTTON
<p>
<?= Html::button('Add Schedule', ['class' => 'btn btn-success', 'onClick'=>'showModal()']) ?>
</p>
3. MODAL BLOCK
<?php
Modal::begin([
'header' => '<h4>Add Passenger</h4>',
'id' => 'passenger_modal',
'size' => 'modal-lg',
]);
echo '<div id="passenger_modal_content"><div>';
Modal::end();
?>
I just change the id's of blocks but that is not matter at all.
this updated version worked for me problem was my layout footer was hidden and being shown with modal body with this question solution
Yii2 and adminlte : 2.4.13
<?php
Modal::begin([
'header' => '<h4><i class="fa fa-book" style="padding-right:5px"></i>' .
$model->name
. '</h3>',
'id' => 'modal',
'size' => 'modal-lg',
]);
?>
<?= "<div id='modalContent'>" ?>
<?php
$form = ActiveForm::begin(); ?>
<div class="box box-info">
<div class="box-body">
<div class="form-group">
<?= $form->field($chapter, 'name')->textInput() ?>
</div>
<div class="form-group">
<?= $form->field($chapter, 'book_id')->hiddenInput(['value' => $model->id])->label(false); ?>
</div>
<div class="form-group">
<?= $form->field($chapter, 'description')->textarea(['rows' => '6']) ?>
</div>
</div>
<div class="box-footer">
<?= Html::a('Cancel', ['index'], ['class' => 'btn btn-default']) ?>
<?= Html::submitButton('Save', ['class' => 'btn btn-info pull-right']) ?>
</div>
</div>
<?php ActiveForm::end();
Modal::end();
?>
" ?>
<script>
$(function() {
$('#modalButton').click(function() {
$('#modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));
});
});
</script>

Yii2 modal height issue

I've been using bootstrap modal on some form. I have learned from this tutorial to create modal. For my previous project I didn't have any issue with the modal. But for my current project the modal body is not fully covering the modal content. Here's the screenshot
I solved this problem for now by referring this. I'm not able to figure out what could be causing the height issue.
My view file code
<div class="collection-type-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::button('Create Collection', ['value' => Url::to('index.php?r=settings/collectiontype/create'), 'class' => 'btn btn-success', 'id' => 'modalButton']) ?>
</p>
<?php // Modal for create
Modal::begin([
'header'=>'Collection Type',
'id'=>'modal',
'size'=>'modal-lg',
]);
echo "<div id='modalContent'></div>";
Modal::end();
?> <!-- end modal -->
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'type',
'days',
'created_by',
'modified_by',
// 'status',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
My controller create action code
public function actionCreate() {
$model = new CollectionType();
$model->created_by = Yii::$app->user->identity->username;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
if (Yii::$app->request->isAjax) {
return $this->renderAjax('create', ['model' => $model]);
} else {
return $this->render('create', ['model' => $model]);
}
}
}
And my js code
$(function(){
$('#modalButton').click(function(){
$('#modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));
});
$('#modal').on('show.bs.modal', function () {
$('.modal-lg .modal-content').css('height',$( window ).height()*0.55);
});
});
As I said I've solved the problem by using the above js script but I don't think this is the best solution as I would have to set the height manually like this for every modal. What could be causing this problem? Has anyone faced this issue and solve?
This looks like a problem with floats. Modify the view generated inside the modal:
Wrap the content of this modal between <div class="clearfix"> and </div>.
OR
Add <div class="clearfix"></div> at the end of this view.

How to reload pjax div in modal window in yii2

I have used modal for viewing comments list, with modal having an update button and a textbox.
When a user enters some text in the text box and click update button i want the comments list div to be reloaded without the whole page being reloaded or the modal being closed or hided.
I have already read
How to update widget with pjax in modal window in yii2
que. but still not getting solution.
My code is
Registered Js and view file
$this->registerJs("
$(function() {
$('#replayMeg').on('beforeSubmit', function(event, jqXHR, settings) {
var comment = $('#".Html::getInputId($modelReply, 'hdr_comments')."').val();
var ticketId = $model->hdt_id;
$.ajax({
url: '".Url::to(['helpdesk-replies/create'])."',
type: 'post',
data: { replyMsg : comment, ticketId : ticketId},
beforeSend: function() {
if($('#".Html::getInputId($modelReply, 'hdr_comments')."').val() == '')
{
$('.field-helpdeskreplies-hdr_comments').addClass('form-group field-helpdeskreplies-hdr_comments required has-error');
$('.help-block').html('Reply cannot be blank.');
return false;
}
},
success: function(data) {
//$.pjax.reload({container:'#update_replyBox'});
}
});
return false;
})
});
");
<?php yii\widgets\Pjax::begin(['id' => 'update_replyBox']); ?>
<div class="modal-body">
<div class="box-tools pull-right">
<?= $actionBtn; ?>
</div>
</div>
<div class="panel panel-info">
<div class="panel-heading colorchangeheader left-align">
<i class="fa fa-users fa-fw"></i> <?= $model->getUserName($cData['hdr_replay_user_id']); ?> <span class="mobilebreakpoint"><i class="fa fa-clock-o fa-fw"></i><?= Yii::$app->formatter->asDate($cData['created_at']); ?> # <?= Yii::$app->formatter->asTime($cData['created_at']); ?></span>
</div>
<div id="rp8" class="panel-body">
<?= $cData['hdr_comments'] ?>
</div>
</div>
<?php $form = ActiveForm::begin(['id' => 'replayMeg']); ?>
<div class="box-body">
<div class='row'>
<div class='col-sm-12'>
<?=
$form->field($modelReply, 'hdr_comments')->widget(RemainingCharacters::className(), [
'type' => RemainingCharacters::INPUT_TEXTAREA,
'name' => 'message_body',
'id' => 'message_body-textarea',
'text' => Yii::t('app', '{n} Characters Remaining'),
'label' => [
'tag' => 'p',
'id' => 'my-counter',
'class' => 'counter text-aqua',
'invalidClass' => 'error'
],
'options' => [
'rows' => '3',
'class' => 'form-control replyTextArea',
'maxlength' => 200,
'placeholder' => Yii::t('app', 'Write something'),
]
]);
?>
</div>
</div>
<!--/div>
<div class="box-footer"-->
<?= Html::button('<i class="fa fa-reply"></i> '.Yii::t('comm', 'Reply'), ['class' => 'btn btn-info btn-create submit', 'type' => 'submit']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php yii\widgets\Pjax::end() ?>
Please try to set backdrop false in the modal
'clientOptions' => ['backdrop' => false]
or
Set data-pjax true in your active form
<?php $form = ActiveForm::begin(['options' => ['data-pjax' => true ]]); ?>
For more details http://www.yiiframework.com/wiki/772/pjax-on-activeform-and-gridview-yii2/
For more options on Modal Refer

How to use Pjax with a Custom Button in Gridview

This is my gridview
<?php Pjax::begin(['id' => 'vouchers']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
['header'=>'Cancel Voucher',
'value'=> function($data)
{
return Html::a(Yii::t('app', ' {modelClass}', [
'modelClass' =>"Cancel",
]), ['generalentries/cancel_voucher','voucher_no'=>$data->voucher_no],
['class' => 'btn btn-xs btn-danger',
'onclick'=>"return confirm('Are you Sure you want to CANCEL this Voucher?');"]
);
},
'format' => 'raw'
],
],
]); ?><?php Pjax::end() ?>
and this is my cancel_voucher action
public function actionCancel_voucher( $voucher_no )
{
$entries = TabGLEntry::find()->where(['voucher_no' => $voucher_no])->all();
foreach($entries as $entry)
{
$entry->update_note = FALSE;
$entry->is_deleted = 1;
$entry->save();
}
$this->redirect( Yii::$app->request->referrer );
}
How can i update the grid view using pjax when i click on the Cancel Button?
I tried giving button an id and then on the onClick event of the button i tried to update the gridview using $.pjax.reload({container:"#vouchers"}); But it didn't work
EDIT
This is how i tried
<?php
\yii\widgets\Pjax::begin([
'id' => 'countries',
'timeout' => false,
// 'enablePushState' => false,
'clientOptions' => ['method' => 'POST']])
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
['header'=>'Cancel Voucher',
'value'=> function($data)
{
return Html::a(Yii::t('app', ' {modelClass}', [
'modelClass' =>"Cancel",
]), ['generalentries/cancel_voucher','voucher_no'=>$data->voucher_no],
['class' => 'btn btn-xs btn-danger','id' =>'cancel_voucher',
'onclick'=>"return confirm('Are you Sure you want to CANCEL this Voucher?');"]
);
},
'format' => 'raw'
],
<?php
$this->registerJs(
'jQuery(document).ready(function($){
$(document).ready(function () {
$("#cancel_voucher").on("click", function(){
alert("hai");
$.pjax.reload({container:"#countries"});
});
});
});'
);
?>
Issue
You have to return content, which will be showing in Pajax block instead of $this->redirect( Yii::$app->request->referrer );
public function actionCancel_voucher( $voucher_no )
{
$entries = TabGLEntry::find()->where(['voucher_no' => $voucher_no])->all();
foreach($entries as $entry)
{
$entry->update_note = FALSE;
$entry->is_deleted = 1;
$entry->save();
}
if (Yii::$app->getRequest()->getIsPjax()) {
//render part of view with grid
return $this->renderAjax($url, $params);
}
else { $this->redirect( Yii::$app->request->referrer ); }
}
For correct pajax redirect you have to setup X-Pjax-Url header.
if (Yii::$app->getRequest()->getIsPjax()) {
$response = Yii::$app->getResponse();
$destination = Url::to($url);
$response->getHeaders()->set('X-Pjax-Url', $destination);
$response->getHeaders()->set('Location', $destination);
return null;
}

Yii2 Pjax on ActiveForm and GridView [ Got it Working]

I have a gridview
<?php Pjax::begin(['id' => 'members']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
// 'filterModel' => $searchModel,
'columns' => [
['header'=>'Remove Member',
'value'=> function($data)
{
return Html::a(Yii::t('app', ' {modelClass}', [
'modelClass' =>'',
]), ['members/stl_remove','id'=>$data->id],
['class' => 'btn btn-link fa fa-times fa-2x pop']
);
},
'format' => 'raw'
],
['class' => 'yii\grid\ActionColumn','template'=>'{update}'],
],
]); ?>
<?php Pjax::end() ?>
when i click on the Remove members link a modal popsup (delete.php)
<?php Pjax::begin(['id' => 'delete_members']) ?>
<?php $form = ActiveForm::begin(['options' => ['data-pjax' => true ]]); ?>
<div class="row">
<div class="col-md-5">
<?php echo $form->field($model, 'remarks')->dropDownList(['death' => 'Death',
'marriage' => 'Marriage', 'house_shift' => 'House Shift'],['prompt'=>'Select'])
->label("Reason For Removal"); ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Remove') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php yii\widgets\Pjax::end() ?>
Now these are my controller code
[code]
public function actionStl_remove($id,$ajax=0)
{
$model = $this->findModel($id);
$model_delete = new Members();
$model->scenario = "remove";
if ($model_delete->load(Yii::$app->request->post()))
{
$model->remarks = $model_delete->remarks;
$model->status = 1;
$model->save();
// $model = new Members();
// return $this->redirect(['index']);
}
else
{
return $this->renderAjax('delete', [
'model' => $model_delete,
'id' => $id,
]);
}
}
I am not able to get data loaded into gridview. I click on the link modal popsup then i submit after that i go on to the index page and the page reloads
This is how i got it working. In delete.php i made following changes.
<?php $form = ActiveForm::begin([
'options' => ['data-pjax' => true,
'id'=> 'dynamic-form111',
// 'validationUrl' => 'validation-rul'
]]); ?>
<div class="row">
<div class="col-md-5">
<?php echo $form->field($model, 'remarks')->dropDownList(['death' => 'Death',
'marriage' => 'Marriage', 'house_shift' => 'House Shift'],['prompt'=>'Select'])
->label("Reason For Removal"); ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Remove') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
basically i gave an id to the from dynamic-form111.
Then in index.php along with gridview in pjax i added the below code
<?php
$this->registerJs(
'jQuery(document).ready(function($){
$(document).ready(function () {
$("body").on("beforeSubmit", "form#dynamic-form111", function () {
var form = $(this);
// return false if form still have some validation errors
if (form.find(".has-error").length)
{
return false;
}
// submit form
$.ajax({
url : form.attr("action"),
type : "post",
data : form.serialize(),
success: function (response)
{
$("#ajaxModal").modal("toggle");
$.pjax.reload({container:"#countries"}); //for pjax update
},
error : function ()
{
console.log("internal server error");
}
});
return false;
});
});
});'
);
?>
which via ajax check if delete.php has been submitted successfully if yes then reloads gridview using pjax and closes modal.