Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a jQuery UI Datepicker button inline with the input?

In an MVC 5 project, with the default bootstrap layout, I am using the following code to set all inputs of a certain class to jQuery UI Datepicker widgets:

$(".jqueryui-marker-datepicker").datepicker({
    dateFormat: "yy-mm-dd",
    changeYear: true,
    showOn: "button"
}).next("button").button({
    icons: { primary: "ui-icon-calendar" },
    label: "Select a date",
    text: false
});

Here is the HTML that is rendered by Razor and jQuery UI after the above call executes, minus some aria and validation data attributes:

<div class="form-group">
    <label class="control-label col-md-2" for="Time">Date</label>
    <div class="col-md-10">
        <input class="form-control jqueryui-marker-datepicker hasDatepicker valid" id="Time" name="Time" type="text" value="2015-05-02">
        <button type="button" class="ui-datepicker-trigger ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only" role="button" title="Select a date">
            <span class="ui-button-icon-primary ui-icon ui-icon-calendar"></span><span class="ui-button-text">Select a date</span>
        </button>
        <span class="text-danger field-validation-valid" data-valmsg-for="Time" data-valmsg-replace="true"></span>
    </div>
</div>

The problem with this is that datepicker button appears below the date input. This is because the bootstrap .form-control class makes gives the input a display: block. If I edit this in Chrome's console to inline-block the button appears immediately to the right of the input, exactly where I want it.

Now I could add a new css rule as follows:

.form-control.jqueryui-marker-datepicker {
  display: inline-block;
}

but I'm just not sure if this is the neatest way to do this, with the least impact on all the layout magic that bootstrap is doing.

like image 835
ProfK Avatar asked May 01 '15 14:05

ProfK


People also ask

How datepicker is implemented in jQuery?

$(function() { $("input#date_due"). datepicker(); }); The selector you want is all elements with the tag name "input" and the id attribute set to "date_due".

How do I maxDate datepicker?

Try this: $("#datepicker"). datepicker({ minDate: -0, maxDate: new Date(2013, 1, 18) }); If you want use hard coded date, use the new Date(2013, 1, 18) pattern.

How do I change the pop up position of the jQuery datepicker control?

To change the position of the jQuery UI Datepicker just modify . ui-datepicker in the css file. The size of the Datepicker can also be changed in this way, just adjust the font size.

How can change date format in jQuery UI datepicker?

inside the jQuery script code just paste the code. $( ". selector" ). datepicker({ dateFormat: 'yy-mm-dd' });


2 Answers

The bootstrap metaphor for displaying buttons next to inputs is to use an input-group like this:

<div class="input-group">
  <input type="date" class="form-control" placeholder="Date" />
  <span class="input-group-btn">
    <button class="btn btn-default" type="button">
        <span class="glyphicon glyphicon-calendar" ></span>
    </button>
  </span>
</div>

You can tweak your script to format the output to add these classes and html structure by wrapping the contents like this:

$(".jqueryui-marker-datepicker")
  .wrap('<div class="input-group">')
  .datepicker({
    dateFormat: "yy-mm-dd",
    changeYear: true,
    showOn: "button"
  })
  .next("button").button({
    icons: { primary: "ui-icon-calendar" },
    label: "Select a date",
    text: false
  })
  .addClass("btn btn-default")
  .wrap('<span class="input-group-btn">')
  .find('.ui-button-text')
  .css({
    'visibility': 'hidden',
    'display': 'inline'
  });
<link href="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet"/>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script>

<input type="text" class="jqueryui-marker-datepicker form-control">

Alternative w/ Bootstrap

That being said, this is a pretty convoluted way to do this. I'd add a native bootstrap glyphicon instead of trying to force the button to display correctly. It's also pretty uncommon to mix jQuery-UI and Bootstrap as there is a lot of overlap in functionality and they don't always play nice together. I would recommend looking into just extending bootstrap with a datepicker plugin.

Here's an example using bootstrap-datepicker

$('.datepicker').datepicker({});

// 
$('.datepicer-icon').on('click', '.btn', function(e) {
  $(e.delegateTarget).find('.datepicker').focus();
});
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.1/css/datepicker.css" rel="stylesheet"/>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.1/js/bootstrap-datepicker.js"></script>

<div class="container" >

  <h3>With Input Group</h3>
  <div class="input-group datepicer-icon">
    <input type="text" class="form-control datepicker" placeholder="Date" />
    <span class="input-group-btn">
      <button class="btn btn-default" type="button">
        <span class="glyphicon glyphicon-calendar" ></span>
      </button>
    </span>
  </div>

  <h3>With Feedback</h3>
  <div class="form-group has-feedback">
    <input type="text" class="form-control datepicker" placeholder="Date" />
    <i class="glyphicon glyphicon-calendar form-control-feedback"></i>
  </div>

</div>
like image 127
KyleMit Avatar answered Sep 30 '22 21:09

KyleMit


Try this way :

<form class="form-inline">
        <div class="form-group">
            <label class="control-label col-md-2" for="Time">Date</label>
            <div class="col-md-10">
                <input class="form-control jqueryui-marker-datepicker hasDatepicker valid" id="Time" name="Time" type="text" value="2015-05-02">
                <button type="button" class="ui-datepicker-trigger ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only" role="button" title="Select a date">
                    <span class="ui-button-icon-primary ui-icon ui-icon-calendar"></span><span class="ui-button-text">Select a date</span>
                </button>
                <span class="text-danger field-validation-valid" data-valmsg-for="Time" data-valmsg-replace="true"></span>
            </div>
        </div>
</form>
like image 44
Joe Mike Avatar answered Sep 30 '22 20:09

Joe Mike