douza9835
2015-03-06 16:41
浏览 48

jQuery Datepicker只有一组特定的日期

I need help with the jQuery datepicker. I have an array of dates. I need all of the dates disabled except for the dates in this array. I have code working where it does the exact opposite. I create the array of dates in PHP (I know their date features better) and then bring them into the javascript. I am using laravel framework with blade templating. If someone could modify what I have to work or have a better solution, that would be great!

Enabled dates: - Every Wednesday is available unless it is Wednesday then use the following Wednesday - Every OTHER Saturday is available starting with 03/07/2015

Here is my code:

HTML

                    <div class="form-group">
                        <div class="col-md-6">
                            {{ Form::text('orientation', '', array('class' => 'form-control', 'id' => 'orientation',
                            'placeholder' => 'Orientation Date', 'required' => 'required')) }}
                        </div>
                    </div>

Javascript

$(function() {
var array = {{$data['availableSat']}}

$('#orientation').datepicker({
    beforeShowDay: function(date){
        var string = jQuery.datepicker.formatDate('yy-mm-dd', date);

        alert(array.indexOf(string));
        return [ array.indexOf(string) == -1 ]
    }
});
});

PHP

public function getMoreInfo()
{
    //Gather Orientation Dates
    $time = time();
    $dayName = date("D", strtotime("now"));

    //First get the available Wednesdays
    if ($dayName === "Wed")
    {
        $wed1 = date('m/d/Y', strtotime('+1 week Wednesday'));
        $wed2 = date('m/d/Y', strtotime('+2 week Wednesday'));
        $wed3 = date('m/d/Y', strtotime('+3 week Wednesday'));

        $sqlWed1 = date('Y-m-d', strtotime('+1 week Wednesday'));
        $sqlWed2 = date('Y-m-d', strtotime('+2 week Wednesday'));
        $sqlWed3 = date('Y-m-d', strtotime('+3 week Wednesday'));
    }
    else
    {
        $wed1 = date('m/d/Y', strtotime('next Wednesday'));
        $wed2 = date('m/d/Y', strtotime('+1 week Wednesday'));
        $wed3 = date('m/d/Y', strtotime('+2 week Wednesday'));

        $sqlWed1 = date('Y-m-d', strtotime('next Wednesday'));
        $sqlWed2 = date('Y-m-d', strtotime('+1 week Wednesday'));
        $sqlWed3 = date('Y-m-d', strtotime('+2 week Wednesday'));
    }

    $data['availableWed'][] = $sqlWed1;
    $data['availableWed'][] = $sqlWed2;
    $data['availableWed'][] = $sqlWed3;

    //Get the available Saturdays -> Every other sat can be derived by using week numbers and checking for odd or even
    $week = date("W"); //php function for current week number
    //Evaluate the sat for a few weeks out
    for ($i=0; $i<4; $i++)
    {
        $lastDigitWeek = substr($week,-1);
        //If the week is odd then there is orientation
        if ($lastDigitWeek == '1' || $lastDigitWeek == '3' || $lastDigitWeek == '5' || $lastDigitWeek == '7' || $lastDigitWeek == '9')
        {
            $time2 = strtotime('Saturday', $time);
            $time3 = gmdate("Y-m-d", $time2);
            $Saturdays[$i] = "N/A";
            //Disabled Saturdays
            $disableDates[] = $time3;
        }
        else
        {
            if ($dayName === "Sun")
            {
                $time2 = strtotime('+1 week Saturday', $time);
                $time3 = gmdate("m/d/Y", $time2);
                $time4 = gmdate("Y-m-d", $time2);
            }
            else
            {
                $time2 = strtotime('Saturday', $time);
                $time3 = gmdate("m/d/Y", $time2);
                $time4 = gmdate("Y-m-d", $time2);
            }
            //Available Saturdays
            $Saturdays[$i] = $time3;
            $saturdaysSQL[] = $time4;
        }

        $week = $week + 1;
        $time = $time + 604800;
    }
    $data['availableSat'] = json_encode($saturdaysSQL);
    $data['availableWed'] = json_encode($data['availableWed']);
    return View::Make('apps.newHire.menuMoreInfo')->with('data', $data);
}

Produces: Opposite of what I need

Thanks Guys,

Anthony

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjie9630 2015-03-06 17:26
    已采纳

    As stated by bobdye

    return [ array.indexOf(string) !== -1 ]

    打赏 评论

相关推荐 更多相似问题