Allow DateTime to have a starting date without setting the object's value
Ionic version: (check one with "x") [X] 2.x
I'm submitting a ... (check one with "x") [X] feature request
Current behavior: Currently to have the DateTime object open with a value (other than the max value) you must set the ngModel value for the object before the DateTime is opened. Sometimes this isn't desired, such as when you'd like to leave the field blank if the user does not input a date.
Expected behavior: Adding parameters to start the DateTime at a certain date, like today, will enhance usability. For example, if my date selection range is from 2000 - 2025, but the users most often select a date within a few weeks of today, it would be much easier to start the DateTime interface with today's date instead of 1-1-2025.
Steps to reproduce: Open DateTime.
Ionic info: (run ionic info from a terminal/cmd prompt and paste output below):
Cordova CLI: 6.4.0
Ionic Framework Version: 2.0.0-rc.4
Ionic CLI Version: 2.1.17
Ionic App Lib Version: 2.1.7
Ionic App Scripts Version: 0.0.48
ios-deploy version: 1.9.0
ios-sim version: 5.0.11
OS: OS X El Capitan
Node Version: v6.9.2
Xcode version: Xcode 8.2.1 Build version 8C1002
@whitecat - 3.10.3 is not a valid version of the ionic framework. Perhaps that is your CLI version? not sure.
To check your Ionic Framework version, you can run ionic info and the look for the line that says Ionic Framework. It will look something like this:
Ionic Framework : ionic-angular 3.9.2
You can also query NPM to find out what version of ionic-angular is installed. That will be your version of Ionic Framework.
You are correct, that is why it is not working. I am on 3.6.0
Fixed in 3.7.1. Should satisfy all of the above. use initialValue to set the initial value, but note that will only "commit" if the user chooses something and hits "done"
This is unfortunately not good enough. The initialValue needs to be visible immediately in the field when set, not only after picker is opened and done is hit. The behaviour i'm getting now on 3.9.2 is that the field is initially empty
To have the value presented on the field immediately, add this code after this._initialize(): if (this.initialValue) this.setValue(this.initialValue); If someone can please examine if this is the right place and commit a change to the trunk. Thank you.
@misterti see https://github.com/ionic-team/ionic/issues/13292#issuecomment-340839395
You're (understandably) misunderstanding
initialValueand should just usengModel.
initialValueis meant to set the picker wheels / dropdown to a sensible starting spot while not affecting the data in the model. It's not meant to set the initial value of theDateTimeas the property name implies, but of the picker when it opens.
Hi, i see, but if i have a ngFor loop, how else can i set and present initial value for multiple items in the template?
Then we need a way for ion-datetime to present an "initial" value in the field
@misterti - what exactly are you attempting to accomplish here?
You mention using ngFor so I assume you have a collection of objects and need to edit the dates or something (really hard to know without context).
If I had a bunch of objects and a standard input in a regular old Angular CLI app, I would do something like:
<input *ngFor="let item of items" [(ngModel)]="item.description" />
If I needed to initialize the description fields beforehand I would do that at some point in the TypeScript.
So it would be basically the same for datetime, something like this:
<ion-datetime *ngFor="let item of items" [(ngModel)]="item.startDate"></ion-datetime>
And again if startDate needed a value that wasn't set for some reason, I would handle that in the TypeScript.
The idea behind the (somewhat awkwardly named) initialValue property is that you don't want to set the model value, and thus don't want to display anything until the user picks something, but you still want to give them a default option when they open the picker, as described in the initial write-up and further clarified in the comments that the followed:
Currently to have the DateTime object open with a value (other than the max value) you must set the ngModel value for the object before the DateTime is opened. Sometimes this isn't desired, such as when you'd like to leave the field blank if the user does not input a date.
I had to do a transformation of the date before presentation, so i tried [(ngModel)]="transformationFunction(item.startDate)" in a ngFor, but that didn't work. Doing transformation in typescript before presentation and only putting [(ngModel)]="item.startDate" indeed works and also changes the list model accordingly. Thank you for clarifying.
What about the default label value? I have a form where there are two different <ion-datetime>s, both using the same formControlName, one for date, one for time. Consumers want two fields, what can I do?
However, no matter how hard I try, when I'm just doing time with a formControlName I never get the timezone offset calculated for the label..even if the field itself has been properly adjusted.
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.