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 ]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误