【问题标题】:Highlight a day with an icon or color in DateTimePicker. (MVC and Bootstrap)在 DateTimePicker 中用图标或颜色突出显示一天。 (MVC 和引导程序)
【发布时间】:2018-08-09 08:15:46
【问题描述】:

我有一张如下图所示的表格。

我希望日期列中的值用圆圈中的圆圈突出显示 DateTimePicker(或特定颜色)。

有可能吗?最好的方式 ?有什么例子吗?

谢谢

附注我使用 MVC 和 Bootstrap 3

【问题讨论】:

  • 听起来可以使用beforeShowDay。您可以将控制器操作中的日期作为日期数组传递,并使用inArray 检查并突出显示这些日期。你能显示当前的日期选择器和控制器方法代码吗?
  • 目前还没有代码,我想知道是否可能以及如何解决。谢谢你的重播..
  • This post 和 this 可以帮助您入门。您可以随时编辑提供您已经尝试完成的日期突出显示的帖子。
  • 太棒了!!非常感谢。

标签: twitter-bootstrap-3 asp.net-mvc-5


【解决方案1】:

复制到Controller中

public ActionResult GetArrayofDates()
            {
                DateTime[] d = new DateTime[]
                {
                    new DateTime(2019,9,27),
                    new DateTime(2019,9,25),
                    new DateTime(2015,7,27),
                    new DateTime(2019,5,5)
                };

                return View(d);
            }

这就是风景

@{
    ViewBag.Title = "GetArrayofDates";
}
<link href="~/Content/themes/base/jquery-ui.min.css" rel="stylesheet" />
<style>
    .highlight {
        background-color: #ff0000 !important;
        color: #ffffff !important;

    }
    .nothighlight{
        background-color:#fff7f7;
        color:#000000;
    }
</style>
<h2>GetArrayofDates</h2>
@{

    for (int i = 0; i < Model.Length; i++)
    {
        <h3>@Model[i]</h3>
    }
}
<div id="calandar">

</div>
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script src="~/Scripts/jquery-ui-1.12.1.js"></script>
<script>



    var dates = []

@foreach (var item in Model)
{ 

    //@: allow you to write javascritp/hhtml code inside c# code which you specific using @ which allow write c# inside javascrpit/html
    @:dates.push('@item.ToString("yyyy-M-dd")');
}
    console.log(dates);
    console.log(dates["0"])

    $("#calandar").datepicker({
        todayHighlight: true,
        changeYear: true,
        changeMonth: true,
        minDate: new Date(2010,1,1),
        beforeShowDay: function (date) {

            var calender_date = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + ('0' + date.getDate()).slice(-2);
            console.log(calender_date)
            var search_index = $.inArray(calender_date, dates);

            if (search_index > -1) {
                return [true,'highlight','Employee Worked on this day.' ];
            } else {
                return [true, 'nothighlight', 'Employee did not Work on this day.'];
            }

        }
    });        








</script>

这个完整的工作示例确保匹配从服务器发送的日期格式并像我一样放入数组中,一切都会好起来的。 并下载 Jquery UI,并使用 UI CSS,我想你知道

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 2011-09-15
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多