【问题标题】:How to disable Weekends in this calendar UI如何在此日历 UI 中禁用周末
【发布时间】:2014-07-14 18:06:29
【问题描述】:

我有这个从 David Walsh 实施修改的示例代码,我的目标是禁用周六和周日周末的按钮。我在实施它时遇到了一些困难。我希望有人可以帮助我解决我的问题。`

<?php
/*
* Calendar from David Walsh's implementation: http://davidwalsh.name/php-calendar
*/

if(isset($_REQUEST["date"])){

    // Get the variable
    $chosenDate = explode("/",$_REQUEST["date"]);
    $month = $chosenDate[0];
    $year = $chosenDate[1];

    // Check browser
    if(isset($_SERVER['HTTP_USER_AGENT'])){
        $agent = $_SERVER['HTTP_USER_AGENT'];
    }

    /* 
    * Draws the calendar 
    */

    // Draw table 
    $calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

    // Table headings 
    $headings = array('S','M','T','W','Th','F','S');
    $calendar.= '<tr><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

    // Variables to be used
    $running_day = date('w',mktime(0,0,0,$month,1,$year));
    $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
    $days_in_this_week = 1;
    $day_counter = 0;
    $current_date = explode("/",date('m/j/Y')); //Gets the current date 
    $dates_array = array();

    // Row for week one 
    $calendar.= '<tr class="calendar-row">';

    // Print "blank" days until the first of the current week 
    for($x = 0; $x < $running_day; $x++):
        $calendar.= '<td class="calendar-day-np"> </td>';
        $days_in_this_week++;
    endfor;

    // Keep going with days...
    for($list_day = 1; $list_day <= $days_in_month; $list_day++):

        // This checks current day so that past days will never be picked
        if($list_day < $current_date[1] && $month == $current_date[0] && $year == $current_date[2])
            $calendar.= '<td class="calendar-day2">';
        else{
            if(stristr($agent,"firefox")) $value = 'this.textContent';
            else $value = 'innerText';
            $calendar.= '<td class="calendar-day" onclick="redirect_to_sample('.$value.')">';
        }

        // Add in the day number
        if($list_day < $current_date[1] && $month == $current_date[0] && $year == $current_date[2])
            $calendar.= '<div class="day-number2">'.$list_day.'</div>';
        else if($list_day == $current_date[1] && $month == $current_date[0] && $year == $current_date[2])
            $calendar.= '<div class="day-number"><u>'.$list_day.'</u></div>';
        else 
            $calendar.= '<div class="day-number">'.$list_day.'</div>';      

        // For further SQLs put here
        $calendar.= str_repeat('<p></p>',2);

        $calendar.= '</td>';

        if($running_day == 6):
            $calendar.= '</tr>';
            if(($day_counter+1) != $days_in_month):
                $calendar.= '<tr class="calendar-row">';
            endif;
            $running_day = -1;
            $days_in_this_week = 0;
        endif;
        $days_in_this_week++; $running_day++; $day_counter++;
    endfor;

    // Finish the rest of the days in the week 
    if($days_in_this_week < 8 && $days_in_this_week != 1):
        for($x = 1; $x <= (8 - $days_in_this_week); $x++):
            $calendar.= '<td class="calendar-day-np"> </td>';
        endfor;
    endif;

    // Final row 
    $calendar.= '</tr>';

    // End the table 
    $calendar.= '</table>';

    // All done, return result 
    echo $calendar;
}


   ?>`

将禁用 sat 和 sun 的类是 <td class="calendar-day2">

【问题讨论】:

  • 我已经想通了:只需将 (|| ($running_day==0 || $running_day==6)) 添加到 if(($list_day

标签: javascript php css ajax


【解决方案1】:

看起来$running_day 正在跟踪星期几。 0 = 周六和 6 = 周日,因此您可以利用这一点。

尝试以下方法:

// This checks current day so that past days will never be picked
if($list_day < $current_date[1] && $month == $current_date[0] && $year == $current_date[2])
      $calendar.= '<td class="calendar-day2">';}
elseif($running_day == 0 || $running_day == 6) //Check for weekend
{
     $calendar.= '<td class="calendar-day weekend">';    } 
else{
        if(stristr($agent,"firefox")) $value = 'this.textContent';
        else $value = 'innerText';
        $calendar.= '<td class="calendar-day" onclick="redirect_to_sample('.$value.')">';
}

我已经检查了周末,创建了一个不同的 td 标记和一个额外的周末 css 类,这样你就可以为周末添加一些额外的样式。

请注意,我还没有对此进行测试,并且正在猜测。

【讨论】:

  • 这是一个可能的解决方案,但为了使事情更简单,只需将 (|| ($running_day==0 || $running_day==6)) 添加到 if(($list_day
  • @tomjerry,这是我最初的想法,但您可能想要以不同于过去日期的方式处理周末。例如。除了样式之外,您还可以添加一个标题属性来提供一些反馈title='Please select a weekday'
【解决方案2】:
$("#datepicker").datepicker({ beforeShowDay: $.datepicker.noWeekends });

【讨论】:

  • 我该如何实现呢?所有禁用的日期都在这个类中
  • 这个问题没有提到datepicker jquery 插件。这是一个 php 的实现。在回答之前,您应该更仔细地阅读问题。
  • 我已经想通了:只需将 (|| ($running_day==0 || $running_day==6)) 添加到 if(($list_day
猜你喜欢
相关资源
最近更新 更多
热门标签