【问题标题】:jQuery UI Datepicker -> Display list of events based on monthjQuery UI Datepicker -> 显示基于月份的事件列表
【发布时间】:2017-03-21 02:40:24
【问题描述】:

我正在使用 jQuery UI 日期选择器小部件并设置了一些事件。我想要的是根据用户选择的月份在其下方显示每月事件列表。例如:

然后,如果他们更改为四月,列表将更新为四月的事件。这个小部件可以做到这一点吗?我一直在尝试和研究一段时间。

现在我设置了一些经常性的假期:

var events = {};
for(var i=2017; i < 2027; i++){
events[new Date("01/01/"+i)] = new Event("New Years Day(Closed)", "blue");
events[new Date("02/14/"+i)] = new Event("Valentines Day", "pink");
events[new Date("03/17/"+i)] = new Event("St. Patty's Day", "green");
events[new Date("07/04/"+i)] = new Event("Independence Day(Closed)", "red");
events[new Date("10/31/"+i)] = new Event("Halloween", "orange");
events[new Date("12/25/"+i)] = new Event("Christmas Day(Closed)", "red");

我已经捕获了所有事件:

var eventList = $(".ui-datepicker").find(".ui-datepicker-month").val();
console.log(eventList);

这给了我:

Fri Dec 25 2020 00:00:00 GMT-0800 (Pacific Standard Time)
:
Event
Fri Dec 25 2026 00:00:00 GMT-0800 (Pacific Standard Time)
:
Event
Fri Feb 14 2020 00:00:00 GMT-0800 (Pacific Standard Time)
:
Event
Fri Feb 14 2025 00:00:00 GMT-0800 (Pacific Standard Time)
:
Event
Fri Jan 01 2021 00:00:00 GMT-0800 (Pacific Standard Time)
:
Event
Fri Jul 04 2025 00:00:00 GMT-0700 (Pacific Daylight Time)
:
Event

【问题讨论】:

    标签: javascript jquery jquery-ui datepicker


    【解决方案1】:

    请使用以下代码。

    HTML

    <div class="mt1 border-left flex mr2" style="margin-left: 12px; ">
      <span> Date:</span>
      <input type="text" id="Date" name="date" placeholder="">
    </div>
    

    JS

    $(function() {
        var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    
        function ShowEvents(month) {
            setTimeout(function() {
                $(".ui-datepicker").height(550);
                var Html = "<div style='color:red;'> Events in " + monthNames[month - 1] + " </div>";
                Html += "<br> First Event ";
                Html += "<br> Second Event ";
                Html += "<br> Third Event ";
                $(".ui-datepicker").append(Html);
            }, 5);
        }
    
        $("#Date").datepicker({
            dateFormat: 'yy-mm-dd',
            inline: true,
            onChangeMonthYear: function(year, month, widget) {
                ShowEvents(month);
            }
        });
        $("#Date").focusin(function() {
            if ($(".ui-datepicker").is(':visible') == false) {
                var d = new Date();
                ShowEvents(d.getMonth() + 1);
            }
        });
    });
    

    参考Fiddle

    【讨论】:

    • 如果它在弹出窗口中就可以了。但这也需要我每月更改代码以更新“第一个事件,第二个事件......”
    • 我让它在没有弹出窗口的情况下工作,但它使它无法在日历上突出显示日期。不过感谢您的帖子!
    • 我不明白你要解释什么。您必须编写一个函数,例如当您经过一个月时,该函数将返回该特定月份的事件。您需要遍历所有事件并为此创建 html。并将该 html 绑定到日历 div。这将解决您的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多