Bibash Adhikari
Bibash Adhikari

Reputation: 173

How to use datepicker in yii2?

I have used normal input field in Yii 2 to take a date. I haven't used model for input field. How can I use datepicker in that input field?

My code for input field is:

<div class="col-lg-4">
    <label>From</label> &nbsp;<input type="text" id="datepicker" class="form-   control" name="frominput" />
</div>

And now my JS code is:

<?php
    $this->registerJs("
        $( '#datepicker' ).datepicker();
    ");
?>

This doesn't show datepicker. How can I do this?

Upvotes: 0

Views: 1564

Answers (3)

Saleem Khan
Saleem Khan

Reputation: 150

You can use yii 2 form and model

field($model, 'from_date')->widget(\yii\jui\DatePicker::classname(), [ //'language' => 'ru', //'dateFormat' => 'yyyy-MM-dd', ]) ?>

Upvotes: 1

jithin
jithin

Reputation: 920

Register your script like below in your form

use yii\web\View;

 <?php
    $script = <<< JS
    $('#datepicker').datepicker();
    JS;
    $this->registerJs($script, View::POS_END);
 ?>

Upvotes: 0

Bharatsing Parmar
Bharatsing Parmar

Reputation: 2455

First you need to register jquery file and jquery.ui file

$this->registerJsFile(
  '@web/js/jquery.ui.min.js',
  ['depends' => [\yii\web\JqueryAsset::className()]]
);

Upvotes: 0

Related Questions