2amigos / yii2-date-picker-widget

Bootstrap DatePicker Widget for Yii2
https://2amigos.us
Other
130 stars 77 forks source link

Inline - No way to tell the input value changes #31

Closed michal-novacek closed 8 years ago

michal-novacek commented 8 years ago

Hi guys,

I am passing inline => true and trying to read the value of the input as it changes. Since it is changed by jQuery('#$id').val(e.format()); there is no 'change' or similar event fired.

I had to hack around it like this:

    $('.datepicker-wrapper').on('click', function(e) { // datepicker widget does .val which does not trigger events
        if (!($(e.target).is('.day'))) { // passing '.day' as second arg to .on does not work either because the whole thing gets redrawn
            return;
        }
        setTimeout(function() { // and timeout because click happens faster than the .val
            doThingWithValue($('#datepicker-input').val());
        }, 32);
    });

I think the best solution (besides using ActiveForm, I know) would be to let me pass my own clientEvents['changeDate'] and skip this if I do pass it:

if ($this->inline) {
    $this->clientEvents['changeDate'] = "function (e){ jQuery('#$id').val(e.format());}";
}

Let me know if you want a pull request.

Mike

tonydspaniard commented 8 years ago

I do not see anything wrong by you having to write the javascript code to get the value as its changed/clicked on the inline way. In fact, is exactly how I would probably do it.

michal-novacek commented 8 years ago

The issue is that the input value changes without triggering a change event so it requires a really hacky approach (listening for the click that causes the change + a timeout). But since you don't see anything wrong with that, we don't have anything to discuss further :)