I need to disable pjax inside pjax container on some anchor tags like cancel/ back button. Below is my code:
Pjax::begin(['id' => 'pjax-container-pac-form','timeout' => 10000, 'enablePushState' => false]);
$form = ActiveForm::begin([
'options' => [
'id' => 'create-pac-form',
'data-pjax' => true
]
]);
echo Html::a(Yii::t('app','Cancel'), ['/agency'], ['class' => 'btn btn-default', 'id' => 'cancelButton', 'data-pjax' => false]);
ActiveForm::end();
Pjax::end();
I tried to add 'data-pjax' => false on anchor tag but it's not working. Although it's redirecing back to specified url but at first it's trying to hit via ajax and after that it redirects back to the link. I would like to disable ajax here and redirect it back to the url being specified. I can do so by moving the cancel button out of pjax container but I'm looking for some better way of doing it without modifying HTML at all.
Replace 'data-pjax' => false with 'data-pjax' => 0 in anchor tag
Related
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
I have an upload excel to DB form.
It has 1 File input and 2 buttons 'Upload' and 'Delete All'.
Problem Scenario:
I don't want client-side activeform validation on 'Delete All' as File is not required here.
Code:
<?php $form = ActiveForm::begin([ 'enableClientValidation' => false,
'options' =>['action' => Url::toRoute('/site/halltickets')
,'method' => 'POST'
, 'enctype'=>'multipart/form-data']]); ?>
<?= $form->field($model, 'file')
->fileInput()->label("Upload Only Excel File [Allowed Formats: xlsx, xls]") ?>
<center>
<?= Html::submitButton('Upload »', ['class' => 'btn btn-warning']) ?>
<?= Html::a('Delete All »',
Url::toRoute(['site/delete-halltickets']),
['class' => 'btn btn-danger',
'data-confirm' => Yii::t('yii', 'Delete all Hall Ticket Allocations?'),
]);?>
</center>
<?php ActiveForm::end(); ?>
to understand better, below is the image.
It is bad idea to place "Delete All" button on the upload form. Insert this button outside the form. It will more user-friendly.
Additionally you can remove "required" validator from model. Or define it only for the specific scenarios.
yii2 submit button needs to be clicked two times in form
I have a problem where I need to check more than one submit buttons in the controller. It works but I need to click submit buttons two times.
In controller :
switch(\Yii::$app->request->post('submit')) {
case 'submit_1' :
//my code
break;
case 'submit_2' :
//my code
In view
<?= Html::submitButton('NEXT', ['name' => 'submit', 'value' => 'submit_2','class'=>'btn btn-primary pull-right']) ?>
<?= Html::submitButton('PREVIOUS', ['name' => 'submit', 'value' => 'submit_3','id'=>'next_summary', 'class' => 'btn btn-primary pull-right']) ?>
There is an issue with using jquery reserved words as your attribute id or attribute names.
Search for "Be careful when naming form elements such as submit buttons" at
https://github.com/yiisoft/yii2/blob/master/docs/guide/input-forms.md
Search "Additional Notes" at https://api.jquery.com/submit/
Changing your submit names will fix your click twice problem:
<?= Html::submitButton('NEXT', ['name' => 'submit_next', 'value' => 'submit_2','class'=>'btn btn-primary pull-right']) ?>
<?= Html::submitButton('PREVIOUS', ['name' => 'submit_prev', 'value' => 'submit_3','id'=>'next_summary', 'class' => 'btn btn-primary pull-right']) ?>
try to change name of button as array
<?= Html::submitButton('NEXT', ['name' => 'submit[]', 'value' => 'submit_2','class'=>'btn btn-primary pull-right']) ?>
<?= Html::submitButton('PREVIOUS', ['name' => 'submit[]', 'value' => 'submit_3','id'=>'next_summary', 'class' => 'btn btn-primary pull-right']) ?>
and in your controller :
$submittedType = \Yii::$app->request->post('submit');
switch($submittedType[0]) {
//your code
}
To avoid double click disable validate on submit:
$form = ActiveForm::begin([
'validateOnSubmit' => false,
]);
I use redactor for editing comment. There are multiple redactors since there are multiple comments. Most of the time, redactor is loaded, but sometimes the redactor is not loaded and only generate normal text editor.
These comments are loaded after Pjax Request
My code:
<?= \yii\redactor\widgets\Redactor::widget([
'name' => 'comment',
'value' => \yii\helpers\HtmlPurifier::process($comment),
'clientOptions' => [
'imageUpload' => \yii\helpers\Url::to(['/redactor/upload/image']),
],
]) ?>
Problem solved:
You have to put ID :)
<?= \yii\redactor\widgets\Redactor::widget([
'id' => 'edit_redactor_' . $comment_id,
'name' => 'comment',
'value' => \yii\helpers\HtmlPurifier::process($comment),
'clientOptions' => [
'imageUpload' => \yii\helpers\Url::to(['/redactor/upload/image']),
],
]) ?>
I have some filter per-page and gridview.
They placed in pjax.
When i change some pagination and sort and after that use my per-page filter - params from pagination and sort are lost.
I want to save it. How to do that?
My code:
<?php Pjax::begin(['id' => 'pjax']) ?>
<?= Html::dropDownList('per-page', Yii::$app->request->get('per-page') != NULL ? Yii::$app->request->get('per-page') : [10 => 10], [1 => 1, 5 => 5, 10 => 10, 25 => 25, 50 => 50, 100 => 100], [
'onchange' => '
$.pjax.reload({
url: "'.Url::to([Yii::$app->controller->action->id, 'id' => Yii::$app->request->get('id')]).'?StandartSearch%5Bdate%5D=" + $(\'#date\').val() + "&per-page=" + $(this).val(),
container: "#pjax",
});
',
'class' => 'form-control',
'id' => 'per-page'
]) ?>
<?= GridView::widget([
'id' => 'gridview',
'dataProvider' => $dataProvider,
'columns' => [
// columns
],
]); ?>
<?php Pjax::end() ?>
Per-page filter are placed within pjax container. So obviously they will get lost when pjax event is completed.
To make them not to lost their current selection, you can place per page filter in the form outside pjax.
and $formSelector property on pjax which points to that filter form id.
reference: http://www.yiiframework.com/doc-2.0/yii-widgets-pjax.html#$formSelector-detail