jQuery datepicker function override

前端 未结 1 985
孤街浪徒
孤街浪徒 2021-01-07 01:45

I created some custom datepicker using jQuery datepicker, and it works perfect. There is some scenarios when I need some functionality for onSelect event, and want to overri

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-07 02:15

    As this is not possible, I think out workaround: will pass function as param to custom jQuery function. This is code for creating datepickers:

    var onSelectFunction = function(dateValue){
            $('#end_date.datepicker').datepicker("option", "minDate", dateValue);
        }
        options.onSelect = onSelectFunction;
        $('#start_date.datepicker, #end_date.datepicker').attachCustomDatepicker(options);
    

    and this is preview of my custom function:

    $.fn.attachCustomDatepicker = function(options) { 
    
        var defaultOptions = {
            someDefaultCustomParamsHere: ,
            onSelect: null
        }
    
        var onSelectFunction = function(params){ return false; }
    
        if (typeof options == 'object') {
                options = $.extend(defaultOptions, options);
        } else {
                options = defaultOptions;
        }
    
        if(!options.onSelect){
            options.onSelect = onSelectFunction;
        }
    
        $(this).datepicker( "destroy" );
    
        $(this).datepicker({
             onSelect: function (dateValue, inst) {
                options.onSelect(dateValue);
            }
        }
    }
    

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