6

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

6
  • 1
    My guess would be that you need to instantiate the date-pickers on page change (assuming they are getting unique ID's) Commented Apr 30, 2015 at 14:27
  • 1
    PUT CHECK-IN /CHECK-OUT CODE ABOVE DATA TABLE JS CODE....ITS WORK PROPERLY Commented May 1, 2015 at 9:40
  • Did you get this solved then? Want to accept an answer or close the question if it was something else entirely? Commented May 1, 2015 at 18:21
  • 1
    yes @maleki as per my question you answer is perfect here i suggest that any code which going to apply on data-table must be put before datatable js code. Thank you. Commented May 4, 2015 at 8:28
  • @RaviPrajapati I'm glad I could be of help. I think it might have been a language barrier issue that gave me a different impression. Normally if your issue is solved by someone or yourself, you click the check-mark next to an answer to indicate your acceptance of an answer. This helps others coming across the question know where to look first. Commented May 5, 2015 at 6:12

3 Answers 3

1

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();
});
Sign up to request clarification or add additional context in comments.

1 Comment

on('page.dt') didn't work for me but on('draw.dt') did, stackoverflow.com/questions/32133849/…
0

Yes function call successfully but the applied code checkIn/checkOut date picker is not working i have given unique name of each checkInDate and checkOut Date Elements. Here i applied following code...

// Check-in UI DatePicker code.
    var checkInInit = function () {
        alert('call');
        $('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,
            });
        });

    }
    // Check-out UI DatePicker code.
    var checkOutInit = function () {
        alert('call');
        $('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,
        });
    });
    }
    $('#tableBookingByBooker').on('page.dt', function () {
        checkInInit();
        checkOutInit();
    });

1 Comment

Here function called but datepiker not applied on text box. i think issue in function call. alert com before datatable page is appear... Is there any solution that function called after datatable page is appeared ?
0

Just Use below code :

          $(document).ready(function () {
            $("#tableId").DataTable($(".date").datepicker());                 
           }); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.