我如何在jQuery datepicker上制作简单的新闻日历

时间:2011-07-07 08:07:22

标签: php jquery

我有代码:

$(function() {
    $('#datepicker').datepicker({
        beforeShowDay: daysToMarkAndLink
    });
});

function daysToMarkAndLink(date) {
    .....(if daysToMarkAndLink matches){
        ......(link goes here and also highlight);  
         }
    return [false];
}

<?
$sql = mysql_query("SELECT * FROM news");
while ($row = mysql_fetch_array($sql))
{
    print $row["time_added"];
}
?>

我想做类似的事情,突出我在数据库中的唯一日子,并在我的新闻页面上链接它们,按date_added过滤。

我想要一个关于jQuery datepicker的非常简单的新闻日历。

感谢

1 个答案:

答案 0 :(得分:1)

要实现这一点,你需要混合使用beforeShowDay和onSelect,如下所示:

      <input type="textbox" id="mydate"></input>
      <script>
        //<![CDATA[
        var dateArray = new Array();
        // generate these from mysql. Replace the date by your date field
        dateArray.push({date: new Date('2011/7/5'), link: 'http://mylink1.com'});
        dateArray.push({date: new Date('2011/7/6'), link: 'http://mylink2.com'});
        dateArray.push({date: new Date('2011/7/10'), link: 'http://mylink3.com'});
        dateArray.push({date: new Date('2011/7/15'), link: 'http://mylink4.com'});

        function formatDate(date)  // we use function to convert date to a string we can debug
        {
          var mon = date.getMonth(); // 0-based
          var day = date.getDate(); // 1-based
          var yyy = date.getFullYear();
          return (mon+1)+'/'+day+'/'+yyy;
        }

        $(function(){

          $("#mydate").datepicker({
            beforeShowDay: function(date) {
              var bFound = false;
              var sDate = formatDate(date);
              for (var i = 0; i < dateArray.length; i++) {
                var compDate = formatDate(dateArray[i].date);
                if (sDate == compDate) {
                  bFound = true;
                  break;
                }
              }
              return [bFound,'',''];
            },
            onSelect: function(dateText, inst) {
              var sDate = formatDate(new Date(dateText));
              for (var i = 0; i < dateArray.length; i++) {
                var compDate = formatDate(dateArray[i].date);
                if (sDate == compDate) {
                  document.location.href = dateArray[i].link;
                  break;
                }
              }
            }
          });
        });
        //]]>
      </script>