Speaktech.in

Yii2 widget date picker on date selection

In order to trigger a function on date selection on Kartik date picker we need to assign it in pluginevents.

Below codes we have added event to send the selected date value as ajax request to server.

 

echo $form->field($model, 'OrderDate')
->widget(DatePicker::classname(), [
'options' => [
//'value' => Yii::$app->formatter->asDate($startDate, 'dd-m-yyyy')
],
 'type' => DatePicker::TYPE_INPUT,
// 'onChangeDate' =>new \yii\web\JsExpression("function(dateText, inst)
{
alert('hi');
}"),
 //'disabled' => true,
 'pluginOptions' => [
'startDate' => (string) $startDate,
 'autoclose' => true,
'format' => 'dd-M-yyyy',
'todayHighlight' => true,
"endDate" => (string) date('d/m/Y', time() + (1 * 24 * 60 * 60)),
 ],
'pluginEvents' => [
"change" => 'function()
 {
var data_id = $(this).val();
$.ajax({
type: "GET",
url: "'.\yii\helpers\Url::to(['orders/fetchslots']).'",
data: { dt: data_id },
success: function(data)
{$("#orders-orderdeliveryslotid").html( data );
},
});}',
],
 ])->label(false) ;