1 Answer, 1 is accepted
Hello Clint,
In order to achieve the desired behavior, I would suggest configuring a template in the content option of the "month" view of the DatePicker component:
angular.module("MyModule", ["kendo.directives"])
.controller("MyCtrl", function ($scope) {
$scope.datePickerOptions = {
value: new Date(),
month: {
content: (data) => `<span class="${data.value == 9 ? 'color' : ''}">${data.value}</span>`
},
};
})
As you can see from the above code snippet, I am setting the ".color" class for the span element conditionally, only for a specific date value (in this case "9").
After that, you could select the td parent element of the corresponding span from the template, using the CSS :has() pseudo-class selector, and set the desired background color for the table cell:
<style>
.k-calendar-td:has(.color){
background-color: red;
}
</style>
Below you can find a small Dojo example with AngularJS, demonstrating the described approach:
Let me know if this is what you are looking for.
Regards,
Zornitsa
Progress Telerik
Just need to work out a way of using a list of dates from a database procedure, which represents dates with different colours and apply that to the template via an AngularJS procedure within the controller.
Thanks again for your suggestion.
Hi Clint,
You can access the full Date() obejct through data.date. Then you can use javaScript methods such as getMonth() and getFullYear() to customize the appearance only of the needed dates. Below is an example:
month: {
content: (data) => `<span class="${(data.date.getDate() == 9 && data.date.getMonth() == 2 && data.date.getFullYear() == 2024) ? 'color' : ''}">${data.value}</span>`
},
Here is the modified Dojo example: - https://dojo.telerik.com/@NeliKondova/EHiDiZOl
Regards,
Neli
Neli help, I am stuck on trying to call a function with your example.
Where you have:
content: (data) => `<span class="${data.value == 9 ? 'color' : ''}">${data.value}</span>`
I would like to call a controller function so I can use more complex logic to determine the CSS background colour. I was thinking of doing something like this:
$scope.datePickerOptions = {
month: {
content: (data) => `<span class="${ $scope.dateBackground(data) }">${ data.value }</span>"`
},
};
$scope.dateBackground = function (data) {
var test = data.date;
return "colorRed";
}
How can I call a function within the content template, please?
<!DOCTYPE html>
<html>
<head>
<title>AngularJS example</title>
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/themes/6.7.0/default/default-ocean-blue.css">
<script src="https://kendo.cdn.telerik.com/2023.2.829/js/jquery.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2023.2.829/js/angular.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2023.2.829/js/kendo.all.min.js"></script>
</head>
<body>
<div id="example" ng-app="MyModule">
<div ng-controller="MyCtrl">
<input kendo-date-picker k-options="datePickerOptions"/>
</div>
</div>
<script>
angular.module("MyModule", ["kendo.directives"])
.controller("MyCtrl", function ($scope) {
$scope.datePickerOptions = {
value: new Date(),
month: {
content: (data) => {{dateBackground(data)}}
},
};
$scope.dateBackground = function(data) {
return "<span class='color'>" + data.value + "</span>";
}
})
</script>
<style>
.k-calendar-td:has(.color){
background-color: red;
}
</style>
</body>
</html>
but when I try that, I get this error:
result:7 Uncaught ReferenceError: dateBackground is not defined at content (result:7:36) at _e (kendo.all.js:322401:19) at content (kendo.all.js:322401:19) at ee (kendo.all.js:322401:19) at Object.content (kendo.all.js:322401:19) at init.navigate (kendo.all.js:322401:19) at init.setOptions (kendo.all.js:322401:19) at N._setOptions (kendo.all.js:322401:19) at N._calendar (kendo.all.js:322401:19) at N.open (kendo.all.js:322401:19)
So I'm not sure what to do here. Is what I am doing possible?
2016.3.914+Official
Which was installed with Bowser. So how would be update that if we need to, for any solution to work?
I think I may have found a solution that works. The code is below:
<!DOCTYPE html>
<html>
<head>
<title>AngularJS example</title>
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/themes/6.7.0/default/default-ocean-blue.css">
<script src="https://kendo.cdn.telerik.com/2023.2.829/js/jquery.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2023.2.829/js/angular.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2023.2.829/js/kendo.all.min.js"></script>
</head>
<body>
<div id="example" ng-app="MyModule">
<div ng-controller="MyCtrl">
<input kendo-date-picker k-options="datePickerOptions"/>
</div>
</div>
<script>
angular.module("MyModule", ["kendo.directives"])
.controller("MyCtrl", function ($scope) {
$scope.dateBackground = function(data) {
return "<span class='color'>" + data.value + "</span>";
}
$scope.datePickerOptions = {
value: new Date(),
month: {
content: (data) => `${$scope.dateBackground(data)}`
}
};
})
</script>
<style>
.k-calendar-td:has(.color){
background-color: red;
}
</style>
</body>
</html>
However, when I use this code as is in my project I get the error:
VM756:3 Uncaught ReferenceError: $scope is not defined
at eval (eval at compile (kendo.all.min.js:25:7820), <anonymous>:3:284)
at i (kendo.all.min.js:39:5493)
at Object.content (kendo.all.min.js:39:19536)
at init.navigate (kendo.all.min.js:39:11260)
at init.setOptions (kendo.all.min.js:39:9887)
at W._setOptions (kendo.all.min.js:39:24841)
at W._calendar (kendo.all.min.js:39:24688)
at W.open (kendo.all.min.js:39:25314)
at W.toggle (kendo.all.min.js:39:25502)
at init._click (kendo.all.min.js:39:29094)
While I don't get this error in the Dojo, I do in my Visual Studio project.
Is what I am doing legitimate?
Is there a better way to do this?
What could be causing the error in my local code?
Thanks.
Is this due to an out of date version of the Kendo library?
Hi Clint,
Regarding the '$scope is not defined' usually such error appears when for example by the time a function is called (ex. $scope.myFunc()....), the angular scope is not yet available. However, the Dojo example seems correct and as the issue is not replicated in it I can not be sure why there is such an error on your side.
Regarding the rendered template, indeed the observed behavior could be due to using an old Kendo version. The approach suggested by Zornitsa is suitable when CSP templates are used. For versions prior to R1 2023 yuo can use the previous syntax as in the exmaple below:
$scope.datePickerOptions = {
value: new Date(),
month: {
content: "<span class='#= data.value == 9 ? 'color' : '' #'>#=data.value#</span>"
},
};
I hope this helps.
Regards,
Neli
Thanks for your help this far. From your example:
$scope.datePickerOptions = {
value: new Date(),
month: {
content: "<span class='#= data.value == 9 ? 'color' : '' #'>#=data.value#</span>"
},
};
Is it possible for the code in the content line to call an AngularJS controller function?
I would like to run some logic over a backend call of dates and colours to work out which one I need to show.
Thanks again.,
I've been asked to dynamically change the background colours when an on-click event is fired, but I am not sure how to re-render the kendo date picker control.
Hi Clint,
If you need to change the styles for every date that has been selected, you can add a custom class in the change event handler:
change: function(e){
$('.k-selected').addClass('custom')
},
If you need to change the style of the currently selected cell you can use the .k-calendar .k-calendar-td.k-selected .k-link selector:
.custom, .k-calendar .k-calendar-td.k-selected .k-link{
background-color: green;
}
Here is the modified Dojo - https://dojo.telerik.com/@NeliK/IhUyomat.
Regards,
Neli
The issue is that we are using a Google map, which has pins. When a pin is clicked, we have a click listener on the google.maps.Marker objects.
When this click event is fired I have a back-end call to get a list of dates and CCS colour codes from an endpoint that is calling a database-stored procedure, and at that point, I have to apply them to the date picker background colours.
Is this possible? If so how? Thanks again.
Hi Clint,
I am afraid i could not suggest a possible approach to redraw the DatePicker. The DatePicker API does not provide a refresh() method.
If you think that such a method will be a valuable addition to the suite i would suggest logging a feature request in our official Feedback Portal:
- https://feedback.telerik.com/kendo-jquery-ui/
Regards,
Neli
Thanks, Neli for the response.
Not sure if this is correct, however, I found with experimentation, that if I put an ng-if statement on the surrounding DIV tag, that is holding the date picker control, then set it to false (hidden) then true (visible), this readds the date picker to the DOM and the column template function fires.
Thanks again for your help in this issue, appreciate it.
Hi Clint,
As far as I understand you have managed to achieve the desired result.
Thank you very much for sharing your approach with the community.
Regards,
Neli