Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

input box datepicker not working in all pages of jQuery DataTables

I have jsp page having jQuery DataTables which contain more then 20 pages I'm using textbox in datatable td tags which shows datepicker i.e. check-in /check-out date.

On the 1st page its works perfectly but on the other pages datepicker class is not apply. Here is following code which I used.

HTML code

<table id="tableBookingByBooker">
    <thead class="btn-default">
        <tr>
            <th>checkInDate</th>
            <th>checkInDate</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <input id="checkInDate${data[0]}" />
            </td>
            <td>
                <input id="Text1" />
            </td>
        </tr>
    </tbody>
</table>

JS Code:

$('input[id^=checkInDate]').each(function (index, element) {
    var checkOutDate = (new Date($('#checkOutDate' + $(this).attr('id').substring(11)).val()));
    checkOutDate.setDate(checkOutDate.getDate() - 1);
    $(this).datepicker({
        dateFormat: 'mm/dd/yy',
        maxDate: checkOutDate,
    });
});

$('input[id^=checkOutDate]').each(function (index, element) {
    var checkInDate = (new Date($('#checkInDate' + $(this).attr('id').substring(12)).val()));
    checkInDate.setDate(checkInDate.getDate() + 1);
    $(this).datepicker({
        dateFormat: 'mm/dd/yy',
        minDate: checkInDate,
    });
});

Input box (checkIn/checkout) Datepicker is successfully working in 1st page of datatable but other pages datepicker is not working.

I tried the pagination event and put js code in the function click but it did not work properly

like image 400
Ravi Bhalsod Avatar asked Apr 30 '15 13:04

Ravi Bhalsod


People also ask

Is there a way to use datepicker without jqueryui?

I found that input.datepicker is a jQueryUI thing. The solution was to include jQueryUI in the page, and it magically worked. BTW it was working fine without jQueryUI for other types of column filters, it's only when I tried to use the date range filters that I noticed any error. Quite subtle. Show activity on this post.

What is the jQuery date picker component?

The jQuery UI library provides a date picker component which gives a comprehensive calendar picker - including features that are not available in Editor's built in datetime calendar input such as the ability to display multiple months at the same time.

How do I use the date picker with editor?

jQuery UI's date picker can be used with Editor through the date field type which will automatically determine if the date picker is available on your page or not - if it is, it will be used and if not the HTML5 date input type will be used.


1 Answers

As Ted pointed out, your best bet is to register an on "page change" event. The problem is that a one-time initialization using jquery select won't have an affect on dynamically loaded content.

There might be other ways, but the one referenced by dataTables looks like this.

https://datatables.net/reference/event/page

//Your Check-in UI DatePicker code.
var checkInInit = function () {}
//Your Check-out UI DatePicker code.
var checkOutInit = function () {}

var table = $('#dataTable').DataTable();
$('#dataTable').on('page.dt', function () {
    checkInInit();
    checkOutInit();
});
like image 199
Maleki Avatar answered Nov 15 '22 00:11

Maleki