Using datePicker to disable dates already selected

Discussion in 'jQuery' started by xna4life, Dec 11, 2012.

  1. #1
    hey guys..So I am using datepicker http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/datePickerStartEnd.htmlto select start and end dates.I have the controller coded so that if the user attempts to select a date from the calendar (which is already stored for them-from a previous selection) it will not add it to the DB. I also have the weekends blocked out so they cant be used (done using
    http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/datePickerCustomCellRender.html)

    E.g say the user has previously selected fri 14th and added it to the DB, if they try to make a new record selecting from thur 13th to mon 17th- only thur 13, and mon 17th will be added to the DB. As fri 14th was previously added and 15 and 16th are weekends:Done using the following code:
    [HttpPost] public ActionResult listHolidays(Holiday holiday, int? PersonId, string HolidayDate, string endDate, string AlreadyExists)
    { DateTime startDates = Convert.ToDateTime(HolidayDate);
    DateTime endDates = Convert.ToDateTime(endDate);

    while (startDates <= endDates)
    {
    if (startDates.DayOfWeek != DayOfWeek.Saturday && startDates.DayOfWeek != DayOfWeek.Sunday)
    {
    ViewBag.CantDuplicateHolidays = String.IsNullOrEmpty(AlreadyExists) ? "date" : "";
    var dates = from d in db.Holidays
    where d.HolidayDate == startDates && d.PersonId == PersonId
    select d;


    // <= 0..so if it does not already exist
    if (dates.Count() <= 0)
    {
    Holiday holiday1 = new Holiday();
    holiday1.PersonId = PersonId.Value;
    holiday1.HolidayDate = startDates;


    db.Holidays.AddObject(holiday1);
    db.SaveChanges();
    //say start date is 10. AddDays(1) will make it 11 then return it to startDates in 'startDates' = startdates,
    //but doesnt chage the value of startdates = 'startdates'
    }


    }


    startDates = startDates.AddDays(1);
    }


    return RedirectToAction("Index");
    }
    ---------------
    my view
    Date.format = 'dd/m/yyy';
    $("#HolidayDate").addClass('date-pick');
    $("#endDate").addClass('date-pick');
    //$('.date-pick').datePicker//({dateFormat: 'dd-mm-yy'}).val();


    // clickInput: true


    $(function () {
    //3 methods below dont allow user to select weekends
    $('.date-pick').datePicker(
    {
    createButton: false,
    renderCallback: function ($td, thisDate, month, year) {
    if (thisDate.isWeekend()) {
    $td.addClass('weekend');
    $td.addClass('disabled');
    }


    }
    }
    )


    .bind('click',
    function () {
    $(this).dpDisplay();
    this.blur();
    return false;
    }
    )


    .bind('dateSelected',
    function (e, selectedDate, $td) {
    console.log('You selected ' + selectedDate);
    }
    );


    // HolidayDate is start date
    $('#HolidayDate').bind('dpClosed',
    function (e, selectedDates) {
    var d = selectedDates[0];
    if (d) {
    d = new Date(d);
    $('#endDate').dpSetStartDate(d.addDays(0).asString());
    }
    }
    );


    //end date is end date
    $('#endDate').bind('dpClosed',
    function (e, selectedDates) {
    var d = selectedDates[0];
    if (d) {
    d = new Date(d);
    $('#HolidayDate').dpSetEndDate(d.addDays(0).asString());
    }
    }
    );
    });


    can any suggest how to disable a date on the calendar if it is already in the DB
     
    xna4life, Dec 11, 2012 IP