如何在 jquery datepicker 中动态禁用特定日期?
Posted
技术标签:
【中文标题】如何在 jquery datepicker 中动态禁用特定日期?【英文标题】:how to dynamically disable specific date in jquery datepicker? 【发布时间】:2018-03-16 03:02:53 【问题描述】:我正在尝试动态禁用数据库中已经存在的日期(jquery datepicker)。我已经完成了静态方法,因为我能够实现目标但在动态方法中没有取得成功
<script language="javascript">
$(document).ready(function ()
var getfromdb = @html.Raw(@ViewBag.bookdate);
var result = '\'' + getfromdb.toString().split(',').join('\',\'') + '\'';
var disableddates = [result];
var newdisableddates = ['17/10/2017','18/10/2017','19/10/2017','22/10/2017','23/10/2017','24/10/2017','25/10/2017']; // Static Approach
$("#txtFromdate").datepicker(
minDate: 0,
beforeShowDay: DisableSpecificDates,
dateFormat: 'dd/mm/yy'
);
$("#txtTodate").datepicker(
minDate: 0,
beforeShowDay: DisableSpecificDates,
dateFormat: 'dd/mm/yy'
);
function DisableSpecificDates(date)
var string = jQuery.datepicker.formatDate('dd/mm/yy', date);
return [disableddates.indexOf(string) == -1];
);
这是我的控制器代码:
List<string> getbookdate = new List<string>();
getbookdate = BookDate(id);
ViewBag.bookdate = JsonConvert.SerializeObject(getbookdate.ToList());
public List<string> BookDate(int? id)
DateTime dt = System.DateTime.Now.Date;
var getbookdate = (from x in entity.BookingMasters
join
y in entity.BookingDetails on x.BookingId equals y.BookingId
where x.ProductId == id && y.FromDate > dt && y.ToDate > dt
select new BookingModel ProductId = x.ProductId.Value, FromDate = y.FromDate.Value, ToDate = y.ToDate.Value ).ToList();
List<string> allDates = new List<string>();
foreach (var r in getbookdate)
for (DateTime date = r.FromDate; date <= r.ToDate; date = date.AddDays(1))
allDates.Add(Convert.ToString(date.ToShortDateString()));
return allDates;
【问题讨论】:
【参考方案1】:禁用不适用于日期选择器。试试我的plnkr。
https://embed.plnkr.co/h4RaeIKJVfj9IjyHREqZ/
它在点击时获取日期选择器并解析给定的日期和数据集,如果找到匹配项,它会删除其上的点击事件。并在每个渲染上重复,请参阅我的 plnkr 链接以获取 html。希望对你有帮助
<!--Disable doesn't work well with date picker. Try this. Hope it helps-->
<!--Working example https://embed.plnkr.co/h4RaeIKJVfj9IjyHREqZ/ -->
<!--Code-->
<!--HTML PART-->
<!DOCTYPE html>
<html>
<head>
<script data-require="jquery@3.1.1" data-semver="3.1.1" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script data-require="tether@*" data-semver="1.4.0" src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<link data-require="bootstrap@4.0.5" data-semver="4.0.5" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" />
<link data-require="jqueryui@*" data-semver="1.12.1" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />
<script data-require="jqueryui@*" data-semver="1.12.1" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
</head>
<body class="container">
<h1>Hello User</h1>
<div class="box">
<div class="box-content">
<div class="row">
<div class="col-md-4">
<label>Date:</label>
<input type="text" id="date-picker-control" />
</div>
</div>
</div>
</div>
<script>
var element = $('#date-picker-control');
element.datepicker();
element.off('click').on('click', function()
// Setup DatePicker
console.log('Update Date Picker');
// If user changes month or year, update date picker
// based on the date list you have
$("#ui-datepicker-div")
.find("div.ui-datepicker-header")
.find('a.ui-datepicker-next, a.ui-datepicker-prev')
.on('click', function()
element.click();
);
// Get all available dates in current displayed dates
var availableDates = $("#ui-datepicker-div")
.find("table.ui-datepicker-calendar")
.find('td[data-handler="selectDay"]');
availableDates.filter(function(i)
var targetDateElement = $(this);
//Get date parts
var year = +targetDateElement.data("year");
var month = targetDateElement.data("month") + 1; //JS fix
var day = +targetDateElement.find('a.ui-state-default').text();
var builtDate = month + '/' + day + '/' + year;
// you can substitute your json data
var targetDates = [
"10/2/2017", "10/6/2017", "10/8/2017"
];
$.each(targetDates, function(i, targetDate)
// If builtDate match targetdate then turn click event oFF on that date
if (targetDate == builtDate)
targetDateElement.css(
'border': '1px solid gray',
'background': 'darkgray',
'font-weight': 'bold',
'color': 'gray'
).off('click');
);
);
);
</script>
</body>
</html>
【讨论】:
你查看我的 plnkr 链接了吗?它有效..你能告诉发生了什么【参考方案2】:您最初的想法是正确的方向,但我认为所有字符串转换可能都发生了一些奇怪的事情。我只是通过日期并与日期进行比较,而不是所有的字符串: 我已经写了这个快速测试...
控制器:
//Replace with however you get dates
ViewBag.DatesList = new List<DateTime>()
new DateTime(2018, 01, 01),
new DateTime(2018, 01, 02),
new DateTime(2018, 01, 03)
;
return View();
查看:
<script language="javascript">
$(document).ready(function ()
var disableddates = [];
@foreach (DateTime date in ViewBag.DatesList)
//note JS month is zero-based for no good reason at all...
@:disableddates.push(new Date(@date.Year, @date.Month-1, @date.Day))
$("#txtFromdate").datepicker(
minDate: 0,
beforeShowDay: DisableSpecificDates,
dateFormat: 'dd/mm/yy'
);
$("#txtTodate").datepicker(
minDate: 0,
beforeShowDay: DisableSpecificDates,
dateFormat: 'dd/mm/yy'
);
function DisableSpecificDates(date)
//filter array to find any elements which have the date in.
var filtereddates = disableddates.filter(function (d)
return d.getTime() === date.getTime();
);
//return array with TRUE as the first (only) element if there are NO
//matching dates - so a matching date in the array will tell calling
//code to disable that day.
return [filtereddates.length == 0];
);
</script>
这似乎对我有用。
【讨论】:
忘记添加 - 这也可以解决本地化问题(您的原始代码假定 ToShortDateString() 将始终以您在 JS 中测试的格式返回日期,但情况可能并非总是如此。从MSDN : "ToShortDateString 方法返回的字符串是文化敏感的。它反映了当前文化的 DateTimeFormatInfo 对象定义的模式。"以上是关于如何在 jquery datepicker 中动态禁用特定日期?的主要内容,如果未能解决你的问题,请参考以下文章
jQuery UI Datepicker - 如何动态禁用特定日期
如何根据 datepicker 选择的日期动态设置 jquery.timepicker minTime?
如何使用 jQuery Datepicker 动态应用 setDate 和(minDate 和 maxDate)日期范围?