【问题标题】:Yii, CJuiDatePicker how to block certain days of the MonthYii,CJuiDatePicker 如何阻止本月的某些日子
【发布时间】:2015-04-13 01:55:22
【问题描述】:

所以我的 Yii _form.php 页面上有这个小部件。

是否可以做一些事情,比如阻止一个月中的某一天?或者可能会阻止每月的所有星期一,不允许用户选择任何星期一。

更新基于 hamed 的回答

<script type="text/javascript">

function disableSpecificDays(date) {
//date is an instance of Date
    var weekDay = date.getDay(); //  Get the weekday as a number (0-6)
    if(weekDay == 1){ //weekDay == 1 means Monday 
        return false;
    }
    else {
        return true;
    }
}

</script>

在视图方面,

<?php $form->widget('zii.widgets.jui.CJuiDatePicker', array(
        'model' => $model,          
        'attribute' => 'date',          
        'value' => $model->date,
        'options' => array(
            'showAnim'=>'fadeIn',
            'showButtonPanel' => true,
            'minDate'=>'0',
            'changeYear' => true,
            'dateFormat' => 'yy-mm-dd',
            'beforeShowDay' => 'disableSpecificDays',
        ),              
    ));
?>

但由于某种原因,它会阻止日期选择器上的所有内容。根本没有什么可以选择的。我在哪一点做错了?请指教。

【问题讨论】:

  • 您正在将手动日期选择器与 yii 小部件结合起来!这是不正确的。尝试删除 $('#Booking_date').datepicker 并在 yii 小部件选项中添加 beforeShowDay: disableSpecificDays(正如我在回答中所说)。
  • 但是,如果我像你那样做,我会得到语法错误。 unexpected ':', expecting ')'

标签: php yii datepicker jquery-ui-datepicker yii-widgets


【解决方案1】:

jqueryUi 日期选择器有beforeShowDay 事件。你可以这样使用这个事件:

$this->widget('zii.widgets.jui.CJuiDatePicker',array(
    ...
   'options'=>array(
       'showAnim'=>'slide',//'slide','fold','slideDown','fadeIn','blind','bounce','clip','drop'
       'showOtherMonths'=>true,// Show Other month in jquery
       'selectOtherMonths'=>true,// Select Other month in jquery,
       'beforeShowDay' => 'disableSpecificDays', //changed ':' to '=>' AND added quote in between function name.
    ),
  'htmlOptions'=>array(
      'style'=>''
  ),
));
?>

现在,您需要在&lt;script&gt; 标签内定义disableSpecificDays 函数:

function disableSpecificDays(date) {
    //date is an instance of Date
    var weekDay = date.getDay(); //  Get the weekday as a number (0-6)
    var monthDay = date.getDate()   //Get the day as a number (1-31)
    if(monthDay == 12 || monthDay == 13 || weekDay == 1) //weekDay == 1 means Monday 
       return false;
    else return true;
}

这将禁用每个月的第 12 天和第 13 天,也会禁用星期一。

这里有两个有用的链接:

【讨论】:

  • 所以我已经添加了适当的东西到地方。但我收到了这个错误,Uncaught TypeError: undefined is not a function。我能知道我做错了哪一部分吗?
【解决方案2】:

我知道这是一个旧条目,但我发现对于 Yii 1,将返回值放在括号 [] 内就可以了。所以JS函数应该是:

<script type="text/javascript">
    //DON'T SHOW SUNDAYS
    function disableSpecificDays(date) {
    //date is an instance of Date
        var weekDay = date.getDay(); //  Get the weekday as a number (0-6)
        var monthDay = date.getDate();
        if(weekDay == 0){
            return [false];
        }
        else {
            return [true];
        }
    }
</script>

【讨论】:

    【解决方案3】:

    这是一个遗留问题,但这是我通过的代码:

    ...
    'options'=>array(
        'beforeShowDay'=> 'js:function(date){ 
            var weekDay = date.getDay(); 
            var monthDay = date.getDate()  
            if(monthDay == 27 || weekDay == 1) { //Disable all Mondays & 27th of the each month
                return [false];
            } else { 
                return [true];
            }',
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      相关资源
      最近更新 更多