Set datatable editor datetime field max and min date
Set datatable editor datetime field max and min date
Hi,
I am using datatable editor. I use datetime field like this in editor initialization
{
label: "Start: ",
name: "Start",
type: "datetime",
opts: {
minDate: new Date($("#CampaignStartDate").val()),
maxDate: new Date('2018-08-21')
},
}
Now I would like to be able to set the minDate and maxDate dynamically using javascript. How can I do that?
Thank you very much for your help
Jerry
This question has accepted answers - jump to:
Answers
Hi Jerry,
The
datetime
field type hasminDate
andmaxDate
methods available, so you can do something like:Regards,
Allan
That works. Thank you so much.
Follow up question: Can I set the default date dynamically using Javascript as well?
Thank you very much.
Jerry
Yes indeed. The
field().def()
method can be used (def
rather thandefault
sincedefault
is a reserved word in Javascript and can sometimes cause issues - the same withremove()
rather thandelete()
).Allan
When I use the field().def() function, I got some interesting result. I used the following statement
editor.field('Start').def(defaultDate);
in editor 'PreOpen' event handler. It does not work the first time I open the editor. However, it works second time I open the editor. I can't figure out what is going on. Should I put the code in different event handler?
Thanks.
Jerry
Could you show me the full code you are using please?
Setting the default on
preOpen
isn't normally what you would what to do, since the default isn't useful in edit or delete modes (it already has a value).initCreate
might be more useful depending on what exactly you want to do.Allan
Hi, Allan,
Here is the code to set the default date:
Both 'CampaignStartDate' and 'CampaignEndDate' are datetime html controls outside of datatable editor. It is interesting to see that the first time I click 'add' to open editor, the default dates are not set. But after the first time, then default dates are set properly. I can not figure out why.
Thank you.
Jerry
Got it - its because
initCreate
is triggered after the default values have been set. The idea being that you can useinitCreate
to change the values set by the default if needed.So instead of using
field().def()
in this case usefield().val()
:Regards,
Allan