yii2 : kartik select2 in popup modal - yii2

I used to use kartik in many projects and its working fine even in popup modal , but in this way I do not know why
in controller
$model = new LogFile();
$file_position= $_GET['file_position'];
$canTransferTo = new CourtDepartment();
$canTransferTo = CourtDepartment::find()
->where(['id'=>$file_position])
->one();
$CourtDepartment = CourtDepartment::find()
->where('id in ('. $canTransferTo->canTransferTo.')')
->all();
in my _form.php
<?= $form->field($model, 'move_to')->widget(\kartik\widgets\Select2::classname(), [
'data' => \yii\helpers\ArrayHelper::map($CourtDepartment
, 'id', 'name'),
'options' => ['placeholder' => Yii::t('app', 'Choose Court department')],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
when i click the button
<?= Html::button(Yii::t('app', 'Move File'), ['value' => Url::to(['log-file/create','rippId'=>$model->rippId, 'file_position'=>$model->file_position]),'class' => 'btn btn-primary addNew', 'id'=>'addNew']); ?>
i get this message in console
GET http:[My Site Name]?r=log-file%2Fcreate&rippId=1-10-151&file_position=2 500 (Internal Server Error)
but if i open the link in new tab its works fine

Related

How to reset kartik depdrop to initial state with initial data

i have a depdrop in my project like
<?= $form->field($model, 'neighborhood_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map($query,'id','placeWithCity'),
'options' => ['placeholder' => 'Select Your Neighborhood','id'=>'select_place'],
])->label(false); ?>
And
<?=$form->field($model, 'building_id')->widget(DepDrop::classname(), [
'data' =>ArrayHelper::map(Buildings::find()->all(),'id','buildingWithPlace'),
'options' => ['placeholder' => 'Select The Building','id'=>'select_building'],
'type' => DepDrop::TYPE_SELECT2,
'pluginOptions' => [
'depends' => ['select_place'],
'url'=>Url::to(['property-commercial-rent/buildings']),
'loadingText' => 'Loading buildings ...',
]
])->label(false);?>
The second dropdown is depend on the first one.Initially they both have full list of data without any filtering.When i select a neighborhood in the first dropdown then the second populates with the building names under that neighborhood. Then i have a reset button like and has an action like
$( "#reset-location" ).click(function() {
$(select_place).val('').trigger('change');
$(select_building).val('').trigger('change');
});
And this click resetting the both select.BUt the problem is the building has only the items under the previously select neighborhood.I want the make it all like initial stage.How can i do this
You don't need a reset button. On the neighbourhood field allow clear, so it would be like this
<?= $form->field($model, 'neighborhood_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map($query,'id','placeWithCity'),
'options' => ['placeholder' => 'Select Your Neighborhood','id'=>'select_place'],
'pluginOptions' => [
'allowClear' => true
],
])->label(false); ?>
Then there will be a x in the field to clear it.

Create dependent dropdown in Yii2

These are two active-form fields in Yii2.
<?= $form->field($model, 'navigation_type')->dropdownList(['Module'=>'Module','Screen'=>'Screen']) ?>
<?= $form->field($model, 'showInUrl')->dropdownList([0=>'No',1=>'Yes']) ?>
When I click Screen, the second field should be changed to Yes. When I click Module, it should be changed to No. I have to save only 0 or 1 to the db.
How can I do this?
Well, you need to bind the change event to the first drop-down using javascript/jquery like below. add the script on top of your view and provide the id to both the dropdowns.
$this->registerScript("
$('#navigation_type').on('change',function(){
if($(this).val() == 'Module'){
$('#showInUrl').val(0);
}else{
$('#showInUrl').val(1);
}
});",\yii\web\View::POS_END);
<?= $form->field($model, 'navigation_type')->dropdownList(['Module'=>'Module','Screen'=>'Screen'],['id'=>'navigation_type']) ?>
<?= $form->field($model, 'showInUrl')->dropdownList([0=>'No',1=>'Yes'],['id'=>'showInUrl' ])?>
Apart from above solution, you should look into DepDropDown by kartik which reduces your efforts to
a maximum and you just need to integrate and it works great.
Using Kartik/select Dropdown You Can Code like These :
<div class="navigation-form">
<?= $form->field($model, 'navigation_type')
->widget(kartik\select2\Select2::className(), [
'data' => ['Module'=>'Module','Screen'=>'Screen'],
'options' => ['multiple' => false],
'pluginOptions' => [
'placeholder' => 'Select Module',
],
])
?>
<?= $form->field($model, 'showInUrl')->widget(kartik\select2\Select2::className(),[
'data' => [0=>'No',1=>'Yes'],
'options' => ['multiple' => false],
'pluginOptions' => [
'placeholder' => 'ShoW URL',
],
]) ?>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#navigation-navigation_type').on("change",function(e){
var sel_val = $(this).select2("val");
if(sel_val=='Screen'){
$('[name="navigation[showInUrl]"]').val('Yes').trigger('change');
}
if(sel_val=='Module'){
$('[name="navigation[showInUrl]"]').val('No').trigger('change');
}
});

yii2 - two modal in one active form

I have 2 modals in one active form. Let's say first one is modal-ticket and second one is contract-ticket. First time, I click button to show modal-ticket, and it was fine, but when I click button to show modal-contract, it also call script modal-ticket or otherwise. why?
this is my code.
<?php $form = ActiveForm::begin(); ?>
<div id="BtnSearchTicket">
<?= Html::button('Search Ticket', [
'value' => Url::to('../ticket-timbangan/list'),
'class' => 'btn btn-primary',
'id' => 'BtnModalTicketList',
'data-toggle'=>"modal",
'data-target'=>"#modalTicketList",
]) ?>
</div>
<div id="BtnSearchContract">
<?= Html::button('Search Contract', [
'value' => Url::to('../contract/list'),
'class' => 'btn btn-primary',
'id' => 'BtnModalContractList',
'data-toggle'=>"modal",
'data-target'=>"#modalContractList",
]) ?>
</div>
<?php ActiveForm::end(); ?>
<?php
Modal::begin([
'header' => 'Ticket List',
'id' => 'modalTicketList',
'size' => 'modal-lg',
'class' => 'style=width:auto'
]);
echo "<div id='modalContentTicket'></div>";
Modal::end();
?>
<?php
Modal::begin([
'header' => 'Contract List',
'id' => 'modalContractList',
'size' => 'modal-lg',
'class' => 'style=width:auto'
]);
echo "<div id='modalContentContract'></div>";
Modal::end();
?>
<?php
$script = <<< JS
$('#BtnModalTicketList').click(function(e){
e.preventDefault();
$('#modalTicketList').modal('show')
.find('#modalContentTicket')
.load($(this).attr('value'));
return false;
});
$('#BtnModalContractList').click(function(e){
e.preventDefault();
$('#modalContractList').modal('show')
.find('#modalContentContract')
.load($(this).attr('value'));
return false;
});
JS;
$this->registerJs($script);
?>
this are the error found in console web browser
GET http://localhost/pks/web/ticket-timbangan/get-ticket?id=1 404 (Not Found)
GET http://localhost/pks/web/contract/get-contract?id=2 404 (Not Found)
please help.
Try This: using one modal on your form:
<?php $form = ActiveForm::begin(); ?>
<div id="BtnSearchTicket">
<?= Html::button('Search Ticket', [
'value' => Url::to('../ticket-timbangan/list'),
'class' => 'btn btn-primary showModal',
'id' => 'BtnModalTicketList',
'title' => 'Search Ticket',
]) ?>
</div>
<div id="BtnSearchContract">
<?= Html::button('Search Contract', [
'value' => Url::to('../contract/list'),
'class' => 'btn btn-primary showModal',
'title' => 'Search Contract',
'id' => 'BtnModalContractList',
]) ?>
</div>
<?php ActiveForm::end(); ?>
<?php
Modal::begin([
'headerOptions' => ['id' => 'modalHeader'],
'id' => 'modal',
'size' => 'modal-lg',
'closeButton' => [
'id'=>'close-button',
'class'=>'close',
'data-dismiss' =>'modal',
],
'class' => 'style=width:auto',
'clientOptions' => [
'backdrop' => false, 'keyboard' => true
]
]);
echo "<div id='modalContent'></div>";
Modal::end();
?>
<?php
$script = <<< JS
$(document).on('click', '.showModal', function(){
if ($('#modal').hasClass('in')) {
$('#modal').find('#modalContent')
.load($(this).attr('value'));
document.getElementById('modalHeader').innerHTML = '<h4>' + $(this).attr('title') + '</h4>';
} else {
$('#modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));
document.getElementById('modalHeader').innerHTML = '<h4>' + $(this).attr('title') + '</h4>';
}
});
JS;
$this->registerJs($script);
?>
From viewing your code what calls my attention is how you use the Url::to() function. Not sure why you use ../. I have fiddled with using more than one modal in a form and the code is similar to what your are doing. I suggest you try changing your Url::to() function as follows:
Url::to('/ticket-timbangan/list')
and
Url::to('/contract/list')
Another suggestion would be to try with
Yii::$app->urlManager->createUrl('/ticket-timbangan/list')
and
Yii::$app->urlManager->createUrl('/contract/list')
Let me know if this helps.
First, many thanks to kalu who has helped me 2 days to solve my problem. Two modals which contains grid need to define different classes to prevent execute previous script.
'value' => Url::to('../ticket-timbangan/list') and 'value' => Url::to('../contract/list'), both has two gridview and same select-row class. This class is root cause of the problem, because it's same class and execute both script.
Thanks Kalu, you save my day.

Yii 2 Editable, how to use editable inside form?

$form = ActiveForm::begin();
..
echo Editable::widget([ //this break outter form, because this generate another form
'name'=>'person_name',
'asPopover' => true,
'value' => 'Kartik Visweswaran',
'header' => 'Name',
'size'=>'md',
'options' => ['class'=>'form-control', 'placeholder'=>'Enter person name...']
]);
ActiveForm::end();
So, I tried,
echo Form::widget([
'model'=>$model,
'form'=>$form,
'columns'=>1,
'attributes'=>[
'title'=>[
'label'=>false,
'type' => Editable::INPUT_TEXT,
'widgetClass' => Editable::className(),
'options' => [
'asPopover' => true,
]
],
]
]);
but, it shows input box all the time, not editable text.
how can I use editable widget inside form? without breaking outter form?
You can try this way:
<?= $form->field($model, 'person_name')->Editable::widget([
'name'=>'person_name',
'asPopover' => true,
'value' => 'value',
'header' => 'Name',
'size'=>'md',
'options' => ['class'=>'form-control', 'placeholder'=>'Enter person name...']
]);
?>
Note : Not tested yet.

Yii2- Multiple Bootstrap Modal Items on Same Page

I am creating a page which would hold some buttons for creating different model items loaded via modal.
I'm using this code in a view file: create-all that belongs to the controller: ProductsController
Modal::begin([
'toggleButton' => [
'label' => '<i class="glyphicon glyphicon-plus"></i> Add Product',
'class' => 'btn btn-success'
],
'closeButton' => [
'label' => 'Close',
'class' => 'btn btn-danger btn-sm pull-right',
],
'size' => 'modal-lg',
]);
$class_products = 'app\models\Products';
$productModel = new $class_products();
echo $this->render('/products/create', ['model' => $productModel]);
Modal::end();
Modal::begin([
'toggleButton' => [
'label' => '<i class="glyphicon glyphicon-plus"></i> Add branch',
'class' => 'btn btn-success'
],
'closeButton' => [
'label' => 'Close',
'class' => 'btn btn-danger btn-sm pull-right',
],
'size' => 'modal-lg',
]);
$class_branch = 'app\models\Branch';
$branchModel = new $class_branch();
echo $this->render('/branch/create', ['model' => $branchModel]);
Modal::end();
branch/create code:
<?php
use yii\helpers\Html;
/* #var $this yii\web\View */
/* #var $model app\models\Branch */
$this->title = Yii::t('app', 'Create {modelClass}', [
'modelClass' => 'Branch',
]);
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Branches'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="branch-create">
<?php
//echo Yii::$app->request->getReferrer();
$route = parse_url(Yii::$app->request->getReferrer(), PHP_URL_QUERY);
if ($route == urldecode('r=products%2Fcreate-all'))
print_r ($route);
?>
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>
The buttons work just fine and a modal loads for both of the buttons
Problem
Add Product modal adds a new product successfully, but Add Branch doesn't. It just refreshes the page.
An attempt to solve your issue:
As far as I know, simply rendering view file inside the view, will not post data to the controller, unless you specify the action inside the form which you are rendering.
So inside the _form.php
<?php $form = ActiveForm::begin([
'action'=>'your_path_to_the_url',
]); ?>
You can also make an ajax submission from the form and handle the ajax data using Yii::$app->request->isAjax
This is just an idea. I have not seen your controller code nor your forms. Still a wild guess of the problem. See this will help you or not.