-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Month and/or year selecion in DatePicker #4853
Comments
it will be added eventually #675 (comment) (and following discussion) |
dup of #5845 |
oh whoops, misread this, not a dup |
@mmalerba, they are very similar, in fact 😄 This component let me choose the year (it returns an integer instead of a Date or Moment object): Returning an integer is an alternative when you use a datepicker configured to work in Month or Year mode. It's better than just use a regular input with |
This is a first approach of a wrapper to the datepicker in order to achieve some kind of pickers I need in a project of mine. Maybe it can help if someone needs something like a month picker. It's not fully tested: https://stackblitz.com/edit/angular-mulitdate-picker-demo @mmalerba, the |
@julianobrasil I want to try to abstract out the concept of a calendar view, have it be some interface that people can provide their component as. Then allow people to put custom views in the calendar and have the calendar interact with them via that interface. Similar idea to how the form field works with custom form field controls, haven't gotten the time to work on it yet though. |
thanks a lot fot your month picker |
@julianobrasil is that feature is added? . If yes means please elaborate that. Thanks in advance. |
@krishragu12395, if you are referring to the year picker and month picker, strictly speaking, no, they are not available yet. If you are asking whether it's possible to emulate these features, fortunately, the answer is yes. However, this demands the 6-beta versions of material to be installed because you'll use the new |
Thank you @julianobrasil ... |
Month, week, quarter, semester, etc.. Would be awesome if we would have an adapter as mentioned above as it would cover any kind of custom input format. Quarters/Week/Month formatters are all supported by moment and date-fns libs. |
@dojchek, at some point, there will be a way to have all these custom pickers as @mmalerba mentioned in #4853 (comment) |
@julianobrasil, in your demo you have the line: I get an error saying: |
Yeah... in my projects I use, instead: import * as moment from 'moment';
type Moment = moment.Moment; |
Ahh, ok. Thanks |
Onother way to avoid the "moment has no exported member 'default'" error is to set Here is an example
|
@julianobrasil Thank you so much !!! |
@julianobrasil Do you know why in your demo the chosenMonthDate in the _monthSelectedHandler is one month less than what was chosen? I put in this code: I chose July and above printed: However, the formcontrol displays the correct month/year: 07/2017. I got into this as I was trying to figure out how to display Jul 2017 |
@jdewell JavaScript uses 0-indexed months, January = 0, February = 1, etc. |
Hey, @pavan6cs, what exactly are you trying to do? I'm afraid you've tried to do something like Well, I'm not exposing the |
@julianobrasil ,what im trying to do is to emitt an event when date is changed by input or by picking from calandrer menu.
in the above code (dateChange) will not trigger when the monthSelected handler is added. Can you please suggest how can i emitt the event when date is changed through input and also through calender pick? |
Oh, I got it. It won't fire a date change in this case (it's in the docs), because the date is really changed only when the user goes through all the process and selects a day. The day selection is the only event that triggers a date change (maybe you can update the control when the year is selected to force a date change...) |
@julianobrasil thank you.. |
I have tried something and might fulfil the purpose. |
@pavan6cs, I'm sorry, but I missed your #4853 (comment). Well, I've built up something for But @aayushwadhwa's comment answered exactly what you asked. |
just found a solution about this problem //for month// //in your controller @ViewChild('picker') datePicker: MatDatepicker; // close($event) { //for year// //in your controller @ViewChild('picker') datePicker: MatDatepicker; // close($event) { |
ohh my bad @aayushwadhwa already answered it. |
you can do this also without using <div>
<mat-form-field>
<input matInput [matDatepicker]="fromDatePicker" placeholder="From">
<mat-datepicker-toggle matSuffix [for]="fromDatePicker"></mat-datepicker-toggle>
<mat-datepicker startView="multi-year" (yearSelected)="closeDatePicker(fromDatePicker)" #fromDatePicker></mat-datepicker>
</mat-form-field>
<mat-form-field>
<input matInput [matDatepicker]="toDatePicker" placeholder="To">
<mat-datepicker-toggle matSuffix [for]="toDatePicker"></mat-datepicker-toggle>
<mat-datepicker startView="multi-year" (yearSelected)="closeDatePicker(toDatePicker)" #toDatePicker></mat-datepicker>
</mat-form-field>
</div> closeDatePicker(elem: MatDatepicker<any>) {
elem.close();
} |
Yes We can using moment, add below lines above @componentimport { default as _rollupMoment, Moment } from 'moment'; const moment = _rollupMoment || _moment; // See the Moment.js docs for the meaning of these formats:
|
The only trouble with this was the form control isn't updating his value, so for me the input keeps empty, besides you can get the value of the year adding the envent as input in closeDatapicker
|
Is there a way for the user to dynamically decide wether she wants to select a day, month, or year? If I understand correctly, the date picker is closed when a year has been selected, so there is no way to pick a month or day with that config. |
@tobiasschweizer you can try to make a function for both monthSelected and yearSelected output, something like
|
Ideally, the DatePicker would allow for only selecting a month if this is what the user wants and then close itself. Would you suggest to set up different configuration options elsewhere in the template (to find out if "user checked to select only month" etc.) or something similar and then react to to this? There could be a selection for precision the user could choose from: "year" | "month" | "day". |
I had a look at the components/src/material/datepicker/date-selection-model.ts Lines 12 to 26 in 9f01912
In the template, the start and end dates can be bound to an I think from the logical point of view this is all that's needed to represent dates with month and year precision. Internally, these can be treated as ranges from the first to the last day of a month or a year. In the GUI, however, these need to be displayed differently. Clicking on a month or year should be enough to create a date range for a date with month or year precision, the same applies when initializing an existing date range. I think I am also thinking of periods, but they can be represented by two date ranges. With imprecise start and end dates of a period you then have four dates, but logically you just have to worry about the start date of the period's beginning end the end date of the period's end. @mmalerba I am not sure if this belongs here. Shall I try to describe this in a doc? Where would that go? |
@tobiasschweizer we have a design doc template, though I'm not sure all of the sections really apply in this case, since we're talking about adding a feature to an existing component, not making a new component. I think just to give us a starting point to discuss you can copy the above into the "Proposed implementation" section. Though it might be possible to represent the precision as a I think the question of how we represent precision is important, but probably more important is how we work it into the existing pieces that are in play in the datepicker. Some of the pieces are definitely hardcoded to a specific precision (e.g. MonthView), and we might need to come up with a more generic framework that allows for more flexibility in terms of precision |
@mmalerba Ok, thanks for the input. I made a copy of the design doc template and will proceed as you proposed. I will then share the doc with you. |
@mmalerba I shared the design doc with you that I created. Are you able to access it? |
Yep, its on my list of things to take a look at |
Here is how I solved it: https://stackblitz.com/edit/angular-pyeuls?file=app%2Fdatepicker-views-selection-example.html |
Please remove moment js. This should not be a dependency to achieve things like that. I see that Angular Material officially supports moment js. There is an adapter for the mat-datepicker etc. This is really annoying. Instead of that, allow date processor, a middleware or something like this. ... This framework is too complicated. You should get inspired by other Frameworks like Vuetify etc. And note that there are better alternatives to momentjs. Like dayjs or date-fns, etc. ... I just wonder: Why does the mat-datepicker input displays the correct local date, but fails on user input with exactly the same date format? And why does this work in other Frameworks? Sorry, I'm pissed a bit because this is very time wasting stuff. ... Argh... |
Bug, feature request, or proposal:
Feature
What is the expected behavior?
User being able to select a month in a year, or just a full year number. The component already have a startView attribute. It could have also a view attribute so the component could be configured to show just months of the year, or years.
What is the current behavior?
The user can select a full date only (day, month and year).
What is the use-case or motivation for changing an existing behavior?
Although the design docs does mention that will be a date & time option in the future, it does not mention this feature (at least, not explicitly). It's sometimes useful to set the view of a datepicker to allow the selection of just a month or a year. Maybe the problem is related to the date object, that demands a complete date. But it can be left to the developer the control of what is being grabbed from the datepicker, for example:
The developer would have to know how he configured the component and what kind of information it'll give back to him.
The current state of the component can be used for the user to select a month and a year, but it's too confusing to the user if I put a full calendar to choosing just a month (along with some information like "choose any day in the desired month").
A work around to the lack of this feature is to use a regular mdInput with type="number" (that's the way I use for now, because, after all, representing months as numbers are common knowledge). But it's not a nice way because the user must select (in case of months) the month and the year in separate controls.
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
all
Is there anything else we should know?
no
The text was updated successfully, but these errors were encountered: