angular 日期选择器

Picking a date. Should be simple enough right? After multiple iterations of using the default HTML5 date-picker and various npm packages, the truth dawned upon me. Not quite. Although they work for their designated use cases, ultimately there isn’t one out there that fulfills them all. So, I rolled up my sleeves to build one from scratch. (with some help) Here’s how I did it.

选择日期。 应该足够简单吧? 在使用默认HTML5日期选择器和各种npm包进行了多次迭代之后,真相大白。 不完全的。 尽管他们为指定的用例工作,但最终没有一个能满足他们的全部需求。 因此,我卷起袖子从头开始打造袖子。 (在一些帮助下 )这是我的操作方式。

Date Time Range Picker
日期时间范围选择器

Let me retrace my steps to explain how I built this brick by brick, date by date, month by month, year by… Okay you get the point. Lets go right to the beginning and get a peek of my process.

让我回顾我的步骤,以解释我是如何逐块,按日期,按月,按年建造此积木的。 让我们从头开始,一窥我的过程。

1.列出要求 (1. List out the Requirements)

We need an intuitive date picker that is capable of checking the following boxes:

我们需要一个直观的日期选择器,它可以选中以下框:

  • Allow picking a date允许选择日期
  • Provide option to include end date so as to allow picking start date as well as end date提供包括结束日期的选项,以便允许选择开始日期和结束日期
  • Provide option to include time提供选项以包括时间
  • Allow entering time in various formats (Essentially, we need a smart time picker)允许输入各种格式的时间(基本上,我们需要一个智能的时间选择器)

Now that we’ve got the todos listed down. Lets roughly visualize how this component should ideally look like. Here’s an early illustration:

现在,我们列出了待办事项。 让我们大致可视化该组件的理想外观。 这是一个早期的例子:

Rough Illustration
粗糙的插图

… Thank god for css.

……感谢上帝的支持。

Okay, going by our exquisite illustration. What we will need to put in place first, is a basic calendar that is navigable by months showing the year and days of the week.

好吧,按照我们的精美插图。 我们首先需要设置的是基本日历,可以通过显示年份和星期几的月份导航。

2.结构化数据: (2. Structuring The Data:)

We need to design our data structure that will allow us to map days of each month. This grid like structure can be implemented in the following manner:

我们需要设计数据结构,以便我们可以绘制每个月的日期。 这种网格状结构可以通过以下方式实现:

“Here obj is an entity that represents the day which we will later use to store custom key values that will indicate if the day is available, is selected and so on”

“这里的obj是一个代表日期的实体,我们以后将使用它存储自定义键值,该值将指示该日期是否可用,是否已选择等”

gridArr = {        "Jan": [obj, obj,...], // 31 objects        "Feb": [obj, obj, ...], // 28/29 objects          ...        "Dec": [obj, obj, ...], // 31 objects} 

Only, that was the mistake I initially made. In trying to bring the mockups to life, it did not cross my head what would happen if we navigate across years.

只是,那是我最初犯的错误。 试图使样机栩栩如生,这并没有让我费解,如果我们跨年航行会发生什么

So, we need another layer of restructuring.

因此,我们需要进行另一层重组。

gridArr = {        "2020": {                  "Jan": [obj, obj,...], // 31 objects                  "Feb": [obj, obj, ...], // 28/29 objects                    ...                  "Dec": [obj, obj,...], // 31 objects                },        "2021": {                  ...                 }}

3.填补空白 (3. Filling in the Gaps)

In every calendar month, there are certain number of days that are grayed out or blank. Basically, only the days belonging to the current month matter to us. Using Moment.js, we can get the start/end day of the month and its corresponding day number of the week. So all the cells that appear before our starting day number and those that appear after our last day number, are empty or blank.

在每个日历月中,都有一定天数变灰或空白。 基本上,只有属于当月天对我们非常重要。 使用Moment.js,我们可以获得当月的开始/结束日期及其对应的星期几。 因此,出现在我们开始日期编号之前和之后的所有单元格为空或空白。

const firstDayDate = moment(currentDate).startOf('month');this.initialEmptyCells = firstDayDate.weekday();const lastDayDate = moment(currentDate).endOf('month');this.lastEmptyCells = 6 - lastDayDate.weekday();

4.用户友好的可视化: (4. User friendly Visualization:)

Leveraging Moment.js and the css classes provided by Bulma, we map our gridArr to represent the calendar, highlighting today’s date, the selected start date, the selected end date and most importantly the range between the two.

利用Moment.js和Bulma提供的css类,我们映射了gridArr来表示日历,突出显示了今天的日期 ,选定的开始日期,选定的结束日期以及最重要的是两者之间的范围

Bulma CSS classes & usage
Bulma CSS类别和用法

Beginning from the start date all the way up to the end date, we add a key to the objects of the gridArr (obj) called inRange and set the value to true for all the days that fall in between. We apply the calendar-range css class to all the objects whose inRange value is true.

从开始日期一直到结束日期,我们将一个名为inRange的键添加到gridArr( obj )的对象中,并将介于此之间的所有日期的值设置为true。 我们将日历范围的 css类应用于inRange值为true的所有对象。

5.直觉行为 (5. Intuitive Behavior)

To decide the behavior of the date time range picker, I analyzed multiple scenarios where multiple dates are picked and existing date-pickers to zero in on the best of the lot. For example, any flight ticket booking page that requires picking the arrival and departure dates and the date-pickers that popular project management tools use.

为了确定日期时间范围选择器的行为,我分析了多个场景,其中选择了多个日期,并且在很多情况下将现有的日期选择器置零。 例如,任何需要预订到达和离开日期以及流行项目管理工具使用的日期选择器的机票预订页面。

And from the culmination of the research, I arrived at the following:

从研究的高潮中,我得出以下结论:

Date Time Range Picker Behavior
日期时间范围选择器行为

We indicate which date (start or end) is being picked by providing the container box a blue shadow. And going beyond the limits of the selected date flips them. i.e While choosing an end date, if we pick a date before the start date, we assign that date to be the start date and our existing start date becomes the new end date. In a similar fashion, the same logic applies while picking the start date as well. I maintained two modes namely, ‘start’ and ‘end’ as an indicator of which date (start or end) is currently being picked.

我们通过为容器框提供一个蓝色阴影来指示选择哪个日期(开始或结束)。 并超出所选日期的限制将其翻转。 即,在选择结束日期时,如果我们选择开始日期之前的日期,则我们将该日期指定为开始日期,而我们现有的开始日期将成为新的结束日期。 以类似的方式,在选择开始日期时也适用相同的逻辑。 我维护了“开始”和“结束”两种模式 ,以指示当前选择哪个日期(开始或结束)。

Exception: What happens if we pick an end date equal to the start date? Or a a start date equal to the end date? We simply toggle the mode!

例外 :如果我们选择结束日期等于开始日期会发生什么? 还是开始日期等于结束日期? 我们只需切换模式!

6. AM到PM,PM到AM funk (6. AM to the PM, PM to the AM funk)

I mean, We need a smart time picker. One way would be to maintain a bunch of select drop-downs that have options ranging from 1–12 for hours, 0–60 for minutes and AM & PM for the time format. And there we have it, the ugliest time picker ever.

我的意思是,我们需要一个聪明的时间选择器。 一种方法是维护一堆选择下拉菜单,这些下拉菜单的选择范围为1–12小时,0–60分钟,以及AM和PM时间格式。 我们拥有了有史以来最丑陋的时间选择器。

Okay. How about this? We take the entire field as an input text and handle all the possible ways that the user can enter the time. Sure. This should do for now. Note: For any invalid input to the time picker, we indicate with a red box shadow

好的。 这个怎么样? 我们将整个字段作为输入文本,并处理用户可以输入时间的所有可能方式。 当然。 现在应该这样做。 注意:对于时间选择器的任何无效输入,我们用红色框阴影表示

Time Picker
时间选择器

7.对话很便宜,下面是代码: (7. Talk is cheap, here’s the code:)

Parent Component:

父组件

Parent Component Template:

父组件模板

Date Time Range Picker Component:

日期时间范围选择器组件:

datepicker.component.ts
datepicker.component.ts

Date Time Range Picker Template:

日期时间范围选择器模板:

datepicker.component.html
datepicker.component.html

Date Time Range Picker CSS:

日期时间范围选择器CSS:

datepicker.component.css
datepicker.component.css

8.一些依赖性: (8. Some Dependencies:)

Install Bulma and Moment.js:

安装Bulma和Moment.js:

npm install --save bulma moment

Launch your code editor, and as a first thing enable bulma css by adding this line “../node_modules/bulma/css/bulma.css” in the “styles” array in the angular-cli.json file.

启动代码编辑器,首先,通过在angular-cli.json文件的“样式”数组中添加以下行“ ../node_modules/bulma/css/bulma.css”来启用bulma css。

To add a proper calendar style to our datepicker, we need to grab this code from GitHub and save it in a file called bulma-calendar.min.css within our src folder and then make it available by adding another line in our angular-cli.json:

要为我们的日期选择器添加适当的日历样式,我们需要从GitHub中获取此代码 ,并将其保存在src文件夹中的bulma-calendar.min.css文件中,然后通过在angular-cli中添加另一行使其可用.json

.angular-cli.json
.angular-cli.json

And we’re all set.

我们都准备好了。

Head here to my GitHub to access the complete code.

头在这里我的GitHub上访问完整的代码。

Click Here to head to the tutorial that was greatly helpful in gathering the right tools to build this component.

单击此处转到该教程,该教程在收集用于构建此组件的正确工具方面非常有帮助。

And there we have it! A Date Time Range Picker capable of handling start and end dates along with the respective timestamps. There, now it’s simple!

我们终于得到它了! 日期时间范围选择器,能够处理开始日期和结束日期以及相应的时间戳。 在那里,现在很简单!

翻译自: https://medium.com/swlh/building-a-date-time-range-picker-with-angular-bulma-and-moment-js-16cb8ed9d140

angular 日期选择器


http://www.taodudu.cc/news/show-4473382.html

相关文章:

  • 机器学习为什么重要_机器学习:它是如何工作的; 更重要的是,为什么它起作用?...
  • 构建开发环境 构建demo_构建自己的wotsapp第6部分
  • 华研c语言从入门到精通,2020考研答案_ 2018年12月英语六级答案【三套全】(华研外语版)_沪江英语...
  • win10“任务栏图标取消固定,重启后又出现”解决办法
  • 一只猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个;第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半加一个。到第N天早上想再吃时,见只剩下一个桃子了。
  • Linux下kill -9杀不死进程或杀死又自动启动,解决
  • 7-35 猴子吃桃问题 (15 分) 一只猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个;第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半加一个。到
  • 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。到第10天早上时,只剩下一个,求第一天有多少桃子。
  • phpStudy解决mysql 启动后又自动关闭问题方法
  • K8S中删除pod,又被重新创建的问题解决
  • win10 windows update禁用后又自动开启
  • 计算机玩游戏 突然黑屏 又亮了,显示器黑一下又亮了怎么回事?解决显示器黑屏一会又亮一会又黑...
  • 解决CentOS7(无限登录)登录后又跳转到登录页面的问题
  • Vue - Ant Design Vue 中 <a-select> 选择器组件,既可从下拉列表选择,又可输入手动输入文本(既可下拉选择又可自己输入)
  • 接受offer又不去入职,需要承担啥责任嘛?
  • `spyder总是闪退?spyder打不开?spyder又又又又又出错啦?
  • 删除文件过一会又回来_内存卡数据删了又自动回来怎么回事
  • 又是面试题?对,合并有序序列。
  • 华为又一重磅APP永久下线。网友:太难了……
  • python实验总结心得体会_山东省实验学子在“2020未来太空学者大会中国区终选赛”中获佳绩...
  • 第九周周总结
  • 福布斯30岁以下30强名单新出炉!区块链从业者占比13%
  • 2020-10-18大二第七周
  • 胡润首次发布《2019胡润全球独角兽榜》,11家区块链公司入选!
  • 嘉楠科技任命成进为首席财务官:高管变动频繁,多名核心股东减持
  • 上市早不如巧,深度剖析嘉楠“两个第一”的内涵
  • 嘉楠往事
  • apt apt-get_Windows用户准备好进行apt-get吗?
  • php 小程序 运动步数_【小程序+ thinkphp5】 获取微信运动数据
  • 互联网月饼大赏,你最喜欢哪家的?

angular 日期选择器_使用Angular,Bulma和Moment.JS构建日期时间范围选择器相关推荐

  1. angular ui组件_使用Angular Material将现代UI组件添加到Angular项目中

    angular ui组件 Learn how to use Angular Material in this full course for beginners from Codevolution. ...

  2. shell date 日期格式化_抖音短视频系统开发,日期加减

    let dateTime=new Date(); this.dateFomat是对日期格式化为 yyyy - mm - dd的格式 // 减一天 dateTime = dateTime.setDate ...

  3. java calendar日期计算_利用Java中Calendar计算两个日期之间的天数和周数

    前言 究竟什么是一个 Calendar 呢?中文的翻译就是日历,那我们立刻可以想到我们生活中有阳(公)历.阴(农)历之分.它们的区别在哪呢? 比如有: 月份的定义 - 阳`(公)历 一年12 个月,每 ...

  4. angular跳转指定页面_通过 angular CDK 实现页面元素拖放

    通过导入@angular/cdk/drag-drop模块我们可以轻松实现元素在页面中得拖放功能,如元素在页面中随意拖动.在特定区域内拖动亦或对列表进行拖放排序等等. 通过 angular CDK 实现 ...

  5. 4 angular 重构 项目_再遇angular(angular4项目实战指南)

    这两天看了看angular4的文档,发现他和angular1.X的差别真的是太大了,官方给出的那个管理英雄的Demo是一个非常好的入门项目,这里给出一个管理个人计划的小项目,从头至尾一步一步讲解如何去 ...

  6. angular 代码生成器_使用Angular 10构建QR代码生成器

    angular 代码生成器 In this tutorial, we'll learn how to build a QR Codes generator application using the ...

  7. angular id标记_使用传单在Angular中构建地图,第2部分:标记服务

    angular id标记 In my last post, we set up our environment to create a basic map using Leaflet and Angu ...

  8. angular过滤字符_如何使用Angular和Azure计算机视觉创建光学字符读取器

    angular过滤字符 介绍 (Introduction) In this article, we will create an optical character recognition (OCR) ...

  9. angular元素属性绑定_【Angular】表单

    模板式表单 表单数据模型通过组件模板中的相关指令定义,但是只适用于一些简单的场景.比较死板 指令:来自FormsModule模块中 在app.modules.ts中 import { FormsMod ...

最新文章

  1. SourceTree的安装
  2. python实现冒泡排序算法的非递归版本_python排序算法速度比较:快速排序,归并排序,冒泡排序...
  3. 12.JDK1.8 JVM运行时数据区域概览、各区域介绍、程序计数器、Java虚拟机栈、本地方法栈、堆、堆空间内存分配(默认情况下)、字符串常量池、元数据区、jvm参数配置
  4. 课程2 谈论音乐行业的趋势
  5. uva 12627——Erratic Expansion
  6. postgres与osm初步使用
  7. android中文转字节数组,如何将Android中的byte []转换为C中的uint8_T数组?
  8. PingCAP黄东旭:云原生、开源与分布式是数据库行业发展关键词
  9. 雷军求别骂!小米9 Pro定价或许会很高
  10. 计算机等级考试四级信息安全工程师
  11. 阿里 java 电话面试_阿里Java开发电话面试经历--惨败
  12. LeetCode每日一题(22年1月27日-2月5日)
  13. 1007: 鸡兔同笼 C语言
  14. c语言作业 分解质因数,C语言算法之分解质因数
  15. 2017年2月16日-----------乱码新手自学.net 之MVC模型
  16. 《我是一只小小鸟》读后感
  17. python功能主菜单_Pycharm主菜单学习
  18. XCELSIUS例子(CX钻取).
  19. Mac安装软件时提示已损坏的解决方法
  20. @RestController当中的value 含义

热门文章

  1. linux minicon乱码,路径中带中文出现乱码问题
  2. 通识,修 还是不修?——多选一门通识课对绩点影响几何?
  3. upvar, pass by name
  4. android百度地图 自定义气泡,百度地图自定义吹出框(气泡)(转)
  5. intel服务器芯片排行,【2021Intel服务器CPU排行榜】Intel服务器CPU哪款好_热门Intel服务器CPU推荐-太平洋产品报价...
  6. iOS开发之2块钱一根的数据线能用多久
  7. C++ Programming language读书笔记
  8. DAPI(Distributed Application Program Interface)
  9. 蚂蚁开放平台开发者专场(上海站)欢迎参加!
  10. 从技术层面浅析美链BEC从诞生到覆灭