Search code examples
jquerydatepickerjquery-ui-datepicker

Position of buttons in jQuery datepicker


I have searched for quite a bit and can't find an answer to my problem.

I have enabled the buttons panel in my jQuery DatePicker but am now wondering if it is possible to change the position of buttons? I need a "Today" and "Yesterday" button.

I have written the code for a custom button for "Yesterday" but is there a way to change the position of it?

What I need to do is have the buttons in the order of "Yesterday Today Done". Currently they are displayed as can be seen screenshot here.

I have tried writing a code for a custom button for "Today" as well but it still doesn't position properly and on top of that, I can't seem to be able to find a way to hide the default buttons in the buttonPanel.

My code for the datepicker is as follows - JSFiddle:

var options={
        format: 'dd/mm/yyyy',
        container: container,
        todayHighlight: true,
        autoclose: true,
        beforeShow: function (input) { 
            //dpClearButton(input);    
            dpMaxButton(input); 

        },
        showButtonPanel: true,
        beforeShow: function (input) { 
            //dpClearButton(input);    
            dpMaxButton(input); 

        },

        onChangeMonthYear: function (yy, mm, inst) { 

            dpMaxButton(inst.input); 
        }
    };

    function dpMaxButton (input) {
        setTimeout(function () {
            var d = new Date();
            var yesterday = new Date((new Date()).valueOf() - 1000*60*60*24);
            //alert (yesterday);
            var buttonPane = $(input)
            .datepicker("widget")
            .find(".ui-datepicker-buttonpane");
            $("<button>", {
                text: "Yesterday",
                click: function () { 
                     jQuery(input).datepicker('setDate', yesterday);   
                     jQuery(input).datepicker('hide'); }
            }).appendTo(buttonPane).addClass("ui-datepicker-clear ui-state-default ui-priority-primary ui-corner-all");
        }, 1)
    };

    date_input.datepicker(options);

    $.datepicker._gotoToday = function(id) { 
        $(id).datepicker('setDate', new Date()).datepicker('hide').blur(); 
    };

Solution

  • I have updated code:

    http://jsfiddle.net/7Wygg/516/

    First Added css

    .ui-datepicker .ui-datepicker-buttonpane button.ui-datepicker-current{
      float:none !important;
    }
    .pull-left{
      float:left !important;
    }
    

    JS:

    $(function() {
    $(".datepicker").datepicker({
        showButtonPanel: true,
        beforeShow: function (input) { 
                    //dpClearButton(input);    
                    dpMaxButton(input); 
    
                },
          onChangeMonthYear: function (yy, mm, inst) { 
    
                    dpMaxButton(inst.input); 
                }
    });
    function dpMaxButton (input) {
                setTimeout(function () {
                    var d = new Date();
                    var yesterday = new Date((new Date()).valueOf() - 1000*60*60*24);
                    //alert (yesterday);
                    var buttonPane = $(input)
                    .datepicker("widget")
                    .find(".ui-datepicker-buttonpane");
                    $("<button>", {
                        text: "Yesterday",
                        click: function () { 
                             jQuery(input).datepicker('setDate', yesterday);   
                             jQuery(input).datepicker('hide'); }
                    }).appendTo(buttonPane).addClass("ui-datepicker-clear ui-state-default ui-priority-primary ui-corner-all pull-left");
                }, 1)
            }
        $.datepicker._gotoToday = function(id) { 
                $(id).datepicker('setDate', new Date()).datepicker('hide').blur(); 
            };
    });