Yii2 : I Can't type anything if select2 in modal - yii2

I'm using Kartik Select2 Widget ... but the problem is I can't type anything in the search.
I used this code:
`<?= Html::button('حجز موعد ', ['value'=>URL::to('index?r=event/create'), 'class' => 'btn btn-warning btn-block btn-flat','id'=>'creates']) ?>
<?php
Modal::begin([
'options' => [
'id' => 'kartik-modal',
'tabindex' => false
],
]);
modal::end();
?>`
JS Code:
`$(function(){
$(document).on('click','#creates',function(){
var date = new Date().toJSON().slice(0,10);
$.get('?r=event/create',{'date':date},function(data){
$('#modal').modal('show')
.find('#kartik-modal')
.html(data);
});
});
});`
my problem is : the text disabled
If not in modal it works fine !

Related

Update form after ajax save

I want form data updated after ajax save. Cause if item was new (id - empty), it tries to create new one each time. Also there are back-end generated fields which are appears after save.
<?php $form = ActiveForm::begin([
'method' => 'post',
'action' => ['category/save', 'id' => $category ? $category->id : ''],
'enableClientValidation' => true,
// 'enableAjaxValidation' => false,
'validateOnChange' => false,
'validateOnBlur' => false,
'validateOnSubmit' => true,
'options' => [
'id' => 'customer-update',
'class' => 'ajax-submit',
],
'fieldConfig' => [
'template' => '<div class="row-content-col1">{label}</div><div class="row-content-col2">{input}{error}</div>'
]
]); ?>
.......
<?php echo $form->field($category, 'url')->textInput(['class' => 'ip', 'readonly' => true]); ?>
......
<?php $form->end(); ?>
Form field produce such html:
<div class="row-content-col1"><label class="control-label" for="category-url">Url</label></div><div class="row-content-col2"><input type="text" id="category-url" class="ip" name="Category[url]" readonly><div class="help-block"></div></div>
</div>
And than on controller i return this (tried different variations):
{"error":false,"message":"Category 'asdfzsdf sdf' saved","data":{"name":"asdfzsdf sdf","url":"asdfzsdf-sdf","project_id":1,"id":21}}
What is valid response for ajax form? Or is there other way to handle this all ?
Pjax is really useful for your challenge, Just add your form inside of Pjax widget. add form action to new path(such: site/control-data).
In your action method do what you want, but send response like that :
return $this->renderAjax('form path',$model);
It's the general of what you must do.
But maybe you have problem with jquery or pjax or need some more data, but all questions have an answer,
See Pjax for ActiveForm

Yii2 Pagination with Tabs

I have 5 tabs on one page. All the tabs have different content, but on one of them i need to have pagination. When click on pagination the page is refreshing and the current opened tab is closed and show by default first tab ... I want when click on pagination, the current tab to be open and the refresh only part with data information.
here is my code:
<?php
Pjax::begin([
'id' => 'w0',
'enablePushState' => true, // I would like the browser to change link
'timeout' => 10000 // Timeout needed
]);
$spec = Specifications::find()->where('active = 1')->orderBy(['sort' => SORT_ASC]);
$count = $spec->count();
$pagination = new Pagination(['totalCount' => $count, 'defaultPageSize' => 20]);
$models = $spec->offset($pagination->offset)
->limit($pagination->limit)
->all();
echo LinkPager::widget([
'pagination' => $pagination,
'hideOnSinglePage' => true,
'prevPageLabel' => 'Предишна',
'nextPageLabel' => 'Следваща'
]);
if ($spec) { ?>
<div class="form-group">
<label>Спецификации</label></br>
<?php
foreach ($models as $singleSpec) {
echo $singleSpec->id." ".$singleSpec->title;
}
?>
</div>
<?php } ?>
<?php Pjax::end() ?>
remove 'id'=>'w0' from Pjax, it is refreshing your page

Select2 and DepDrop in dynamic form in yii2

I'm trying to use Select2 and depdrop within a dynamic form. For the first row it's working only. But on the next row I'm getting following error.
When I'm using select2 without depfrop it works fine.
Code of form (of select2 and depdrop field)
<div class="col-xs-3 col-sm-3 col-lg-3">
<?= $form->field($modelsProductsales, "[{$i}]productname")->label(false)->widget(Select2::classname(), [
'data' => ArrayHelper::map(Productbatch::find()->orderBy('productname')->all(),'productname','productname'),
'language' => 'en',
'options' => ['placeholder' => 'Select Product','id' => 'prodname'],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, 'batchno')->label(false)->widget(DepDrop::classname(), [
//'options'=>['id'=>'subcat-id'],
'pluginOptions'=>[
'depends'=>['prodname'],
'placeholder'=>'Batch No',
'url'=>Url::to(['/invoice/bills/subcat'])
]
]); ?>
Code of subcat action
public function actionSubcat() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$out = Productbatch::getBatchNo($cat_id);
echo Json::encode($out);
// the getSubCatList function will query the database based on the
// cat_id and return an array like below:
// [
// ['id'=>'<sub-cat-id-1>', 'name'=>'<sub-cat-name1>'],
// ['id'=>'<sub-cat_id_2>', 'name'=>'<sub-cat-name2>']
// ]
//echo Json::encode(['output'=>$out, 'selected'=>'']);
return;
}
}
Please let me know if anymore input from my end is required.
check your codes to find JavaScript codes for the first element that works correctly.you should be add JavaScript codes for all same elements that produces by insert button.
I suggest you to extended another dynamic form from \wbraganca\dynamicform\DynamicFormWidget for your own.Then override registerAssets function and add JavaScripts for other insert handler.
It has problem is yii2-dynamic-form.js file of dynamic form. Same solution work for Select2 and DepDrop both.
solution on following link works for me.
https://github.com/wbraganca/yii2-dynamicform/issues/76#top[Updated Code][1]

How to open a new window in YII framework 2.0

I want to click on a link inside Grid View, which should open a new window in a new tab.
I don't want CHtml::Link answers, since it is YII 1.1, I am using YII 2.0.
THE BELOW CODE IS INSIDE GRID VIEW.
['attribute'=>'EMPLOYEEID',
'label'=>'EMPLOYEEID',
'value'=> Html::a('E_ID', '?r=tb-run-engine/index', ['title' => 'Go']),
],
I didnt get any value for EMPLOYEEID instead am getting [notset] as value in Grid view.and am not getting hyperlink also.Am new to yii 2.0 can any one help me to figure out this problem??
To open link in new tab/window you have to set attribute target="_blank" for this link:
some text
So in Yii2 with Html helper in view file you can write:
<?= Html::a("some text","some_url",['target'=>'_blank']) ?>
And in yii2 grid you can show raw column:
[
'attribute'=>'name', //your model attribute
'format'=>'raw',
'value'=>function ($model, $index, $widget){
return Html::a(
$model->name, //link text
['page/update','id'=>$model->id], //link url to some route
[ // link options
'title'=>'Go!',
'target'=>'_blank'
]
);
}
],
add this to your Html:a options ['target' => '_blank', 'data-pjax' => 0] or turn off pjax in grid
here is my grid view:
<?= GridView::widget([
'dataProvider' => TbRunEngineSearch::$dataprovider_static,
'filterModel' => $searchModel,
'id'=>'searchgrid',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute'=>'run_id',
'label'=>'field level details', //your model attribute
'format'=>'raw',
'value'=>function ($model, $index, $widget){
return Html::a(
$model->run_id, //link text
['page/update','id'=>$model->run_id], //link url to some route
[ // link options
'title'=>'Go!',
'target'=>'_blank'
]
);
}
],
['attribute'=>'run_id',
'value'=>'product_name',
'label'=>'Product Name'],
['attribute'=>'run_id',
'value'=>'module_name',
'label'=>'Module Name'],
['attribute'=>'run_id',
'value'=>'operation_name',
'label'=>'Operation Name'],
['attribute'=>'initiated_at',
'value'=>'initiated_at',
'label'=>'Start Time'],
['attribute'=>'finished_at',
'value'=>'finished_at',
'label'=>'End time'],
['attribute'=>'run_id',
'value'=>'pass_percent',
'label'=>'Pass %'],
['attribute'=>'run_id',
'value'=>'fail_percent',
'label'=>'Fail %'],
['attribute'=>'run_id',
'value'=>'operations_num',
'label'=>'Operations #'],
['attribute'=>'build_num_primary',
'value'=>'build_num_primary',
'label'=>'Build # Pri/Sec'],
'run_status',
'source',
['attribute'=>'env_primary',
'value'=>'env_primary',
'label'=>'ENV # Pri/Sec '],
['attribute'=>'instance_primary',
'value'=>'instance_primary',
'label'=>'INSTANCE # Pri/Sec '],
],
]);
?>

Yii2: Method Not Allowed (#405) while logout user

I am loging out user through following code. This is my view code behind logout button:
<li>
<a href="<?= Url::to(['site/logout'])?>">
<i class="fa fa-sign-out"></i> Log out
</a>
</li>
My controller code is:
public function actionLogout()
{
Yii::$app->user->logout();
$model = new LoginForm();
$this->layout = 'index';
return $this->render('login', ['model' => $model]);
}
In the logout it shows me:
Method Not Allowed. This url can only handle the following request
methods: POST.
What is it?
Seems like you have VerbFilter attached to logout action in your SiteController:
/**
* #inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
That means this action can requested only with POST method, and you are requesting with GET, that's why exception #405 is thrown.
Either remove this from VerbFilter or add data-method attribute to request with POST:
...
Update: Another reason of this problem can be missing dependency for yii\web\YiiAsset. Make sure it's included in AppAsset:
public $depends = [
'yii\web\YiiAsset',
...
];
YiiAsset provides data-method attribute feature which gives possibility to link act as a form with action post by writing less code. Without asset obviously link will act as regular link and standard GET request will be sent.
You can also use a custom template
'items' => [
[
'label' => 'Logout',
'url' => ['/user/security/logout'],
'template' => '{label}',
],
]
u can change the view code and echo instead of
<li>
<a href="<?= Url::to(['site/logout'])?>">
<i class="fa fa-sign-out"></i> Log out
</a>
</li>
this one:
<?= Html::a('<i class="fa fa-sign-out"></i>',
['/site/logout'],
['class'=>'btn btn-default btn-flat']), //optional* -if you need to add style
['data' => ['method' => 'post',]])
?>
You must only replace 'logout' => ['post'], with 'logout' => ['get']. In this way your error will be solved.
This way works only with Yii Framework version 2.
See more at: http://tutorials.scrisoft.com/solve-this-error-method-not-allowed-this-url-can-only-handle-the-following-request-methods-post/#sthash.fQmwYPJH.dpuf
If you are using Nav::widget to generate menus, the logout item should have linkOptions specified:
[
'label' => '<i class="fa fa-sign-out"></i>Logout',
'url' => ['/logout'],
'linkOptions' => ['data-method' => 'post'],
],
Following works too assuming you might extra class and data-method attribute.
<?=
Html::a(
'Logout (' . Yii::$app->user->identity->username . ')',
['/site/logout'],
['class' => 'ui inverted button', 'data-method' => 'post']
);
?>
this code is working for AdminLTE template.
['label' => 'Sign out (' . Yii::$app->user->identity->username . ')','url' => ['/site/logout'],'template' => '{label}',],