【问题标题】:Half day in jQuery ui DatepickerjQuery ui Datepicker 中的半天
【发布时间】:2017-04-29 17:19:34
【问题描述】:

我目前正在使用 jQuery UI Datepicker 脚本来显示租赁的可用日期。我使用推荐的方式来为脚本提供一系列禁用日期,但我的问题是,租赁结束的日期也应该可以作为新租赁的开始日期。客户在第一天和最后一天要求一个红色三角形,以表明它应该可用。我怎样才能用这个插件来实现呢?

$(function() {
    <?php

    $date_range = array();
    $select = mysql_query("SELECT * FROM reservation WHERE cid='$chalet[id]' and status='1'");
    while ($r = mysql_fetch_array($select)) {
        $date_range1 = createDateRangeArray(date("Y-m-d", $r[arrive]), date("Y-m-d", $r[depart]));

        // How can i use those 2 arrays to detect the right triangle to display and make sure the day is selectable
        $first[] = reset($date_range1);
        $last[] = end($date_range1);

        $date_range = array_merge($date_range, $date_range1);
    }

    foreach ($date_range as $key => $values) {
        $dates .= '"' . $values . '", ';
    }
    if ($dates) {
        echo 'var disableddates = [' . substr($dates, 0, -2) . "];\n";
    } else {
        echo "var disableddates = [];\n";
    }

    ?>
    function DisableSpecificDates(date) {
        var string = jQuery.datepicker.formatDate('dd-mm-yy', date);
        return [disableddates.indexOf(string) == -1];
    }
    $( "#calendrier" ).datepicker({
        beforeShowDay: DisableSpecificDates
    });
});

以上代码呈现如下

:

我需要它看起来像这样:

【问题讨论】:

    标签: javascript php jquery jquery-ui datepicker


    【解决方案1】:

    希望你能明白这个想法,如果有任何错误,请原谅,因为它是直接从大脑写的,没有经过任何测试。这会将 .arrival / .departure 类添加到给定日期。

    PHP:

    $arrivals = $departures = $disabledDates = array();
    while ($r = mysql_fetch_array($select)) {
        $arrivals[] = $r['arrive'];
        $departures[] = $r['departure'];
        $disabledDates[] = ....
    

    JS

    var arrivals = <?=json_encode($arrivals)?>;
    var departures = <?=json_encode($departures)?>;
    var disabledDates = <?=json_encode($disabledDates)?>;
    
    
    $( "#calendrier" ).datepicker({
    
        beforeShowDay: function(date){
            var myDate = jQuery.datepicker.formatDate('YYYY-mm-dd', date);
    
            if($.inArray(myDate, disabledDates)) 
                return [false];
            else if($.inArray(myDate, arrivals)) 
                return [true, 'arrival', 'Arrival day'];
            else if($.inArray(myDate, departures)) 
                return [true, 'departure', 'Departure day'];
            else
                return [true];
        }
    }
    

    【讨论】:

    • 这是一个很好的开始。刚刚尝试了您的脚本纠正了 JS 错误并对其进行了测试,现在每个月的每一天都被阻止了,他会尝试解决它,但是如果您看到错误,请告诉我。
    • 很高兴我能帮上忙 ;)
    • @WebiumMedia 你能发布你的工作作为答案吗?
    猜你喜欢
    • 1970-01-01
    • 2011-07-05
    • 2010-10-27
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2013-08-31
    相关资源
    最近更新 更多