jQuery UI Datepicker : how to add clickable events on particular dates?

前端 未结 2 2037
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 10:16

I want to highlight the dates on jquery datepicker where there are events attached to it (i\'m not talking about js event, but real life events :D).

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 11:06

    in addition to Andrew Whitaker solution there is another way to do it (actually its a hack but actually its maybe perfect for someone else because maybe the title or date is not always a good identifier)

    Note: please read Andrew Whitaker solution first and see the changes here

    // date picker
    $("div").datepicker({
        // hook handler
        beforeShowDay: function(tdate) {
            var mydata = $(this).data("mydata");
            var enabled = false;
            var classes = "";
            var title = date;
            $.each(mydata, function() {
                if (this.date.valueOf() === tdate.valueOf()){
                    enabled = true;
                    classes = "highlight";
                    title = title + '" data-id ="'+this.id;// my hack to add additional attributes ;)
                }
            });        
            return [enabled,classes,title];
        },
        // event handler
        onSelect: function() {
            var id = $(this).find(".ui-datepicker-current-day").attr("data-id");
            mydata = $(this).data("mydata"),
            selectedData = null;        
            /* search for data id */
            $.each(mydata,function(){
                if (this.id == id){
                    selectedData = this;
                }
            });
            if (selectedData) {
                /* If the event is defined, perform some action here; show a tooltip, navigate to a URL, etc. */
                alert(selectedData);
            }
        }
    }).data("mydata",
        // your data
        [{
            id:1,
            title: "Five K for charity", 
            date: new Date("02/13/2011")
        }, 
        {
            id:2,
            title: "Dinner", 
            date: new Date("02/25/2011")
        }, 
        {
            id:3,
            title: "Meeting with manager", 
            date: new Date("03/01/2011")
        }]);
    

提交回复
热议问题