How to autofill a DatePicker when other datepicker is selected in yii2 - yii2

I'm using 2amigos Datepicker and trying to fill o_followup datepicker automatically when I'm selecting o_orderdate datepicker. This is same question as Second datepicker is not populating onChange of 1st in yii2. Please help.
Code of o_orderdate datepicker -
<?= $form->field($model, 'o_orderdate')->widget(
DatePicker::className(), [
// inline too, not bad
'inline' => false,
// modify template for custom rendering
//'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
'clientOptions' => [
'autoclose' => true,
'todayHighlight' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
Code of o_followup datepicker
<?= $form->field($model, 'o_followup')->widget(
DatePicker::className(), [
// inline too, not bad
'inline' => false,
// modify template for custom rendering
//'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
'clientOptions' => [
'autoclose' => true,
'todayHighlight' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
Javascript I've tried -
<?php
/* start getting the followupdate */
$script = <<<EOD
$(document).ready(function(){
$("#orders-o_followup").datepicker();
$("#orders-o_orderdate").datepicker({
onSelect: function(){
var fecha = $(this).datepicker('getDate');
$("#orders-o_followup").datepicker("setDate", new Date(fecha.getTime()));
$("#orders-o_followup").datepicker("setDate", "+365d");
}
});
});
EOD;
$this->registerJs($script);
/*end getting the followupdate */
?>
Nothing happens at all when I'm selecting the o_orderdate datepicker. Please help.
Partially Working
<?php
/* start getting the followupdate */
$script = <<<EOD
$("#orders-o_orderdate").change(function(){
$("#orders-o_followup").datepicker();
var fecha = $(this).datepicker('getDate');
$("#orders-o_followup").datepicker("setDate", new Date(fecha.getTime()));
$("#orders-o_followup").datepicker("setDate", "+365d");
});
EOD;
$this->registerJs($script);
/*end getting the followupdate */
?>
This code is populating date in o_followup datepicker. But if I change the o_orderdate again it doesn't respond. It remains stuck the initial changed value. and secondly the dateformat is "mm/dd/yyyy". But I want "yyyy-mm-dd". Please help.

try this:
$("#orders-o_followup").change(function(){
// do do do ;)
});
on change do what do u want dear )

Related

Yii2: Is it possible to disable breadcrumbs in some case (mobile for example)

As the title said, I want in some case to hide or disable the breadcrumbs in my Yii2 app.
Is there any general solution or should I put code in any of my view?
I eventually find this solution :
First add a param in the app config/params.php : 'ariane'
return [
'adminEmail' => 'admin#example.com',
'bsVersion' => '3.x',
'ariane' => false,
];
then add code to empty the links array conditionally in the layout main.php:
<?php
if(isset(Yii::$app->params['ariane']) && Yii::$app->params['ariane'] == false){
$this->params['breadcrumbs'] = [];
}
?>
Why not to put breadcrumb in layout and check it for condition only once.
I mean this way in layout file:
<?php if ($condition) {
Breadcrumbs::widget([
'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
]);
} ?>
<?= $content ?> //view file content

Yii2 - Why ajax change page in Listview with Pjax not working properly

I used listview with pjax block and this is my view page:
<div class="main-video">
<div>نمایش</div>
<div id="data">
<?php
$model = new ActiveDataProvider([
'query' => FileInfos::find()->where(['type' => 2])->orderBy(['id'=>SORT_DESC]),
'pagination' => [
'pageSize' => 15,
],
]);
Pjax::begin([
'id'=> 'id-pjax',
'enablePushState' => false, // to disable push state
'enableReplaceState' => false, // to disable replace state,
'timeout'=> 999999999
]);
if(isset($model))
echo ListView::widget([
'dataProvider' => $model,
'itemView' => '/partials/_item',
'viewParams' => ['typeName' => 'video'],
]);
Pjax::end();
?>
</div>
<button id="btn-show">نمایش</button>
</div>
And this is my action controller code:
public function actionVideo()
{
return $this->render('video');
}
After click on next page, it's reload on entire page.
in network tab of inspect element, I see two request calls:
1 - video?page=2&per-page=15&_pjax=%23id-pjax
2 - video?page=2&per-page=15
First request no have any response and second request have the rendered view page which reloads to that.
Whats wrong in my code?
How can I fix this issue for have ajax request call for changing page?
This article helped me: https://riptutorial.com/yii2/example/16529/how-to-use-pjax,
So it solved by changing pjax begin block with this code:
Pjax::begin([
'id'=> 'id-pjax-'.uniqid(),
'enablePushState' => false, // to disable push state
'enableReplaceState' => false, // to disable replace state,
'timeout'=> 999999999,
'clientOptions' => ['method' => 'POST']
]);

yii2 pjax inside other pjax

i have form with two pjax. (one inside two) when clicked link in second PJAX send query to server but in field "X-PJAX-Container" write ID first container.
Pjax::begin([
'id' => 'register-form'
]);
...
Pjax::begin([
'id' => 'que'
]);
echo yii\helpers\Html::a('update', ['/portal/que/gr-usl/');
Pjax::end();
...
Pjax::end();
location after click update : /portal/que/gr-usl?_pjax=%23register-form
request header
X-PJAX:true
X-PJAX-Container:#register-form
X-Requested-With:XMLHttpRequest
if set data-pjax for link #que request header not changed.
for all link in inside pjax disabled data-pjax an href change to onclick with function
$.pjax({url: $(el).attr('link'), container: '#pjax-grusl'});
links has code
echo yii\helpers\Html::a('update', null,[
'data-pjax' => 'false',
'link' => \yii\helpers\Url::to(['/portal/que/gr-usl/',
'ids' => $ids,
'filter' => $filter,
'gr' => $gr
]),
'onclick' => 'selGr(this)'
]);
but its should work from box :( i don't understand why click by link don't find first pjax container from dom tree.

How to change one attribute display in yii2 DetailView widget?

I am using yii\widgets\DetailView to display some single object informations like this:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'username',
'email:email',
[
'attribute'=>'status',
'value' => $model->status,
],
'created_at:date',
'updated_at:date',
],
]) ?>
I want to display status in different color. So basically I would like to either add class of .red to the wrapping <td> tag, or even better to wrap the status with <span class="red">{value}</span>
GridView has contentOptions which is really easy to use. But with DetailView I have tried with options and template, but I couldn't make it work. Maybe I did something wrong, or I need to use some other attribute.
So this is not working for me:
[
'attribute'=>'status',
'value' => $model->status,
'template' => '<tr><th>{label}</th><td><span class="red">{value}</span></td></tr>'
],
Can someone explain how to achieve this ?
Thanks in advance.
You should simply try :
[
'attribute'=>'status',
'value' => '<span class="red">'.$model->status.'</span>',
'format' => 'raw',
],
Read more.

yii2 inserting a value, when 'other' option is choosen, in dropdown list

I have a dropdown list in my form, and I would like to enable the user to an option of 'other', which would enable him to insert a value in to a text box, instead of a value from the dropdownlist.
Is that possible?
<?= $form->field($model, 'institution')->dropDownList(ArrayHelper::map(Institution::find()->all(), 'iId', 'name'), ['class'=>'form-control inline-block']); ?>
The same thing I would like to do with a radioList.
<?= $form->field($model, 'selfDescription')->radioList(array('good'=>'good','very good'=>'very good','best'=>'best')); ?>
thanks.
First. Add "other" option to dropDownList. And handle it in your frontend with JS
$form
->field($model, 'institution')
->dropDownList(
ArrayHelper::map(Institution::find()->all(),
'iId',
'name'
) + ['other' => 'Other'],
[
'class'=>'form-control inline-block',
// next code disables text field when any but "other" option is selected
// works for jQuery 1.6+
'onchange' => new JsExpression('
$("#textFieldSelector").prop("disabled", $(this).find("option:selected").val() != "other");'),
]
);
Then you need to set properly your validators. Just add
['textField', 'required', 'when' => function($model) {
return $model->institution === 'other';
}]