jQuery Datepicker “After Update” Event or equivalent

前端 未结 4 1524
我在风中等你
我在风中等你 2020-12-01 19:24

I am using a standard jQuery Datepicker and I need to modify the text in each TD based on some daily status information. I realize I can wire up the \"beforeShowDay\" event

相关标签:
4条回答
  • 2020-12-01 19:33

    if you need a "afterShow"-event before jquery-ui version 1.9 you can overwrite the datepicker._updateDatepicker function.

    for example:

    $(function() {
        $.datepicker._updateDatepicker_original = $.datepicker._updateDatepicker;
        $.datepicker._updateDatepicker = function(inst) {
            $.datepicker._updateDatepicker_original(inst);
            var afterShow = this._get(inst, 'afterShow');
            if (afterShow)
                afterShow.apply((inst.input ? inst.input[0] : null));  // trigger custom callback
        }
    });
    

    Now the datepicker raise an "afterShow" event after every update from the datepicker element.

    I know it isn't the best way to solve this problem, but it's better than change the original jquery-ui code.

    0 讨论(0)
  • 2020-12-01 19:33

    I know its another ugly hack but I just add a timeout, like this;

    $('.selector').datepicker({
       beforeShowDay: function(date) { 
           setTimeout(function() {
                //your code  
           }, 50); /* TD content is already rendered */ 
       },
    });
    
    0 讨论(0)
  • 2020-12-01 19:50

    You can do this:

    $('.selector').datepicker({
       beforeShowDay: function(date) { 
           /*some function to do before display*/ 
       },
       onChangeMonthYear: function(year, month, inst) { 
           /*some function to do on month or year change*/ 
       }
    });
    

    See DOCs

    0 讨论(0)
  • 2020-12-01 19:51

    Looks like afterShow() and onAfterChangeMonthYear() are already raised an enhancements, but there doesn't seem to be any work on them yet.

    You could (for now) implement it yourself…

    Download the latest uncompressed source v1.8.13 - see http://blog.jqueryui.com/2011/05/jquery-ui-1-8-13/ and search for _updateDatepicker: function(inst) { then add a new function call. Here is how I laid out the new function:

    _updateDatepicker: function(inst) {
        // existing jQueryUI code cut for brevity
        this._afterShow(inst);
    },
    _afterShow: function(inst) {
        var afterShow = this._get(inst, 'afterShow');
        if (afterShow)
            afterShow.apply((inst.input ? inst.input[0] : null),
                    [(inst.input ? inst.input.val() : ''), inst, inst.dpDiv.find('td:has(a)')]);
    },
    

    I just coded the function to have the same signature as beforeShow() but then added a 3rd parameter - an array of <td> elements.

    You can then add your own callback to the datepicker() in the usual way.

    <script type="text/javascript">
        $(function() {
            $('#dp').datepicker({
                afterShow: function (input, inst, td) {
                   console.log(td); // needs a browser with a console (Chrome / Firefox+Firebug for example)
                }
            });
        });
    </script>
    <input type="text" id="dp"/>
    

    Note: I've not done a great deal of testing, but it seems to be called after the datepicker is rendered. It may need some more work to fit in with your requirements if it's not exactly what you are after. Hope it helps nonetheless :-)

    0 讨论(0)
提交回复
热议问题