datetimepicker icon indicating copy to clipboard operation
datetimepicker copied to clipboard

Not able to set/get the datetimepicker options dynamically

Open prate3k opened this issue 10 years ago • 8 comments

Once the datetimepicker has been initialized, I am not able to re-set or modify any of its option dynamically.

I tried using the object which gets stored as "data" attribute : $("#element_id").data("xdsoft_datetimepicker").setOptions({...});

It seems like if we change only single option say "allowTimes", it will reset all the remaining options ("format", "formatDate", "formatTime" etc.) to its default value. So in this case, I have to re-initialize datetimepicker by specifying same option list again which is not at all a valid use case. Flexibility should be given to developer to set any parameter dynamically.

So to handle such case, there should be some method to get/set any options dynamically without re-setting rest of the options.

prate3k avatar May 30 '14 03:05 prate3k

$("#element_id").datetimepicker(); ... $("#element_id").datetimepicker({allowTimes:[]}); // set options dinamically

xdan avatar May 30 '14 06:05 xdan

Thanks @xdan for the fast reply.

Got your point.

It would be great if we can provide "getOptions" (or similar) method as well. In our project there is requirement to check in which format, date is getting displayed.

It will help us to do some cross-checking as well.

prate3k avatar May 30 '14 07:05 prate3k

Facing this same issue. There doesn't seem to be a way to change only a subset of options at runtime. All options need to be provided else the options not given the second time, will get defaulted.

kashifshamaz21 avatar Aug 10 '14 17:08 kashifshamaz21

I think I'm having the same problem. After I set one option dynamically, other options return to their defaults. I loose options that has been set on initialization.

apisurfer avatar Aug 29 '14 17:08 apisurfer

@popc0rn I looked up at the source code, and there is a function to change options dynamically: you need to use datetimepicker.setOptions({}); I could use this api in two callback functions: onShow() and onChangeDateTime().

@xdan We should probably have this api's usage in the documentation, its a very handy api that many would want to use.

kashifshamaz21 avatar Aug 29 '14 17:08 kashifshamaz21

@kashifshamaz21 thanks for the tip! I had two inputs, one for 'start date' and one for 'end date'. And I had some restrictions I needed to implement between those two(minDate, maxDate) that change as the user changes selected dates. In my case onChageDateTime callback has to change option on the other instance of datetimepicker so inside that callback I used: $('#anotherInputSelector').data('xdsoft_datetimepicker').setOptions({}); Just another way to get to that method from outside of that object's context. Only problem I now encounter is that value off the other instance is being reset after the setOptions call and now I pass it current value as parameter also, to keep it unchanged.

apisurfer avatar Sep 01 '14 12:09 apisurfer

Add a getter method to the js file. I added the below just before datetimepicker.getOptions = function (_options) {...}

datetimepicker.getOptions = function () {
	return options;
}

Call .getOptions() to get an object with full list of options.

I wonder why a trivial function like this was not available out of the box! The developer must have had something in mind, probably cases that I may not have thought about..?!

fr0z3nfyr avatar Apr 24 '18 14:04 fr0z3nfyr

The options can be retrieved like this: var options = $(input).data("xdsoft_datetimepicker").data('options');

azzimuth avatar Apr 27 '20 15:04 azzimuth