bootstrap datetimepicker 在 angular 项目中的运用
datetimepocker 是一个日期时间选择器,bootstrap datetimepicker 是 bootstrap 日期时间表单组件。访问 bootstrap-datetimepicker 。
这次在项目中用到日期时间选择器,分享其用法和总结。
一、截图
功能完成后的项目截图
1. 时间起止输入框

2. 十年视图

3. 年视图

4. 月视图

5. 日视图

6. 时视图

二、依赖
1. 需要 bootstrap 的下拉菜单组件 (dropdowns.less) 的某些样式,还有 bootstrap 的 sprites (sprites.less and associated images) 中的箭头图标。
先安装 less
- 安装淘宝镜像(可能由于网络慢或被墙的原因,npm可能无法访问到) npm install -g cnpm --registry=https://registry.npm.taobao.org
- 安装 less cnpm install -g less (lessc -v返回了版本号说明安装成功)
然后编译 build/build_standalone.less lessc build/build_standalone.less datetimepicker.css
三、选择器需求
1. 初始化时,开始时间为当前时间减去一年,结束时间为当前时间。
2. 时间格式为 "2018-09-28 00:00:00 "。
3. 开始时间不能大于结束时间。
4. 开始时间不能大于当前时间。
5. 结束时间不能大于当前时间。
四、引入依赖
在 .angular-cli.json 文件中引入 bootstrap-datetimepicker.min.css(刚刚编译成的 datetimepicker.css 的压缩版),bootstrap-datetimepicker.min.js, bootstrap-datetimepicker.zh-CN.js(简体中文格式)。
五、实现
选择器模板,由于该模板不是表单,所以使用双向数据绑定模板语法,将选择的时间流向组件处理,在显示到视图。
当错误提示为空时,将其隐藏。
<div class="start-end-time-search search-item">
<label for="startTime" class="search-label start-time-label">起止时间</label>
<input type="text" [(ngModel)]="startTime" placeholder="请选择开始时间" readonly id="startTime" class="search-input start-time-input">
<label for="endTime" class="search-label end-time-label">至</label>
<input type="text" [(ngModel)]="endTime" placeholder="请选择结束时间" readonly id="endTime" class="search-input end-time-input">
</div>
<button type="button" class="bg-btn search-btn" (click)="searchByKeyword()"
[title]="isEnabledSearchBtn ? '查询离线记录' : '不可用(时间选择不正确)'"
[class.isDisabled]="!isEnabledSearchBtn">
<i class="iconfont icon-search"></i> 查询
</button>
<span class="error-tip" [hidden]="!startTimeErrorTip">{{startTimeErrorTip}}</span>
<span class="error-tip" [hidden]="!endTimeErrorTip">{{endTimeErrorTip}}</span>
选择器组件
定义公有属性并赋值,将开始时间和结束时间赋值为空字符串。
/*定义公有属性*/
public startTime: string = "";
public endTime: string = "";
public isEnabledSearchBtn: boolean = true; // 查询按钮是可用的
public startTimeErrorTip: string = ""; // 开始时间选择错误提示
public endTimeErrorTip: string = ""; // 结束时间选择错误提示
将日期时间格式化,当出现一位数的格式时将其前面加 "0"。并返回两种日期时间格式,一种对象,便于拆分,设置开始时间为当前时间减一年。一种字符串,拼接好的格式。
/*
* 日期时间格式化
* 日期时间格式 "2018-09-28 00:00:00"
* */
dateTimeFormat(dateTime) {
let dateTimeObj = {
"year": dateTime.getFullYear(),
"month": dateTime.getMonth() + 1,
"day": dateTime.getDate(),
"hours": dateTime.getHours(),
"minutes": dateTime.getMinutes(),
"seconds": dateTime.getSeconds()
};
for (let k in dateTimeObj) {
if (dateTimeObj[k] < 10) {
dateTimeObj[k] = "0" + dateTimeObj[k];
}
}
let dateTimeString = dateTimeObj.year + "-" + dateTimeObj.month + "-" + dateTimeObj.day + " "
+ dateTimeObj.hours + ":" + dateTimeObj.minutes + ":" + dateTimeObj.seconds;
return {"obj": dateTimeObj, "string": dateTimeString};
}
这里的当前时间有两种,初始化时和选中时间的时候, 选中时间的当前时间要写在日期被改变时的方法中。
/*页面初始化时,填充时间*/
ngOnInit() {
this.isEnabledSearchBtn = true; // 初始化默认选择按钮可用 let initNowTime = this.dateTimeFormat(new Date()); // 开始时间用当前时间减去一年,结束时间使用当前时间。
this.startTime = initNowTime.obj.year - 1 + "-" + initNowTime.obj.month + "-" + initNowTime.obj.day + " "
+ initNowTime.obj.hours + ":" + initNowTime.obj.minutes + ":" + initNowTime.obj.seconds; this.endTime = initNowTime.string; /*下面处理选择的时间*/
let jQuery: any = $;
let that = this;
jQuery("#startTime").datetimepicker({
autoclose: true, // 选择完成自动关闭时间选择器
format: "yyyy-mm-dd hh:ii:ss", // 时间格式
language: "zh-CN", // 使用简体中文
todayBtn: "linked", // 选择器底部显示今天,快速选择当前时间
todayHighlight: true, // 高亮显示今天,或已被选择的时间
zIndexOffset: 1000 // UI,避免被其他设置了 z-index 样式的元素覆盖
}).on("changeDate", function(startTime){ // 日期被改变时触发
/*选中时间的时候的当前时间格式化*/
let nowTimeString = that.dateTimeFormat(new Date()).string; that.startTime = that.dateTimeFormat(startTime.date).string; if (that.startTime > nowTimeString) {
that.startTimeErrorTip = "开始时间不能大于当前时间";
that.isEnabledSearchBtn = false;
} else if (that.startTime > that.endTime) {
that.startTimeErrorTip = "开始时间不能大于结束时间";
that.isEnabledSearchBtn = false;
} else {
that.startTimeErrorTip = "";
that.endTimeErrorTip = "";
that.isEnabledSearchBtn = true;
}
}).data('datetimepicker'); jQuery("#endTime").datetimepicker({
autoclose: true,
format: "yyyy-mm-dd hh:ii:ss",
language: "zh-CN",
todayBtn: "linked",
todayHighlight: true,
zIndexOffset: 1000
}).on("changeDate", function(endTime) {
/*选中时间的时候的当前时间格式化*/
let nowTimeString = that.dateTimeFormat(new Date()).string; that.endTime = that.dateTimeFormat(endTime.date).string; if (that.endTime > nowTimeString) {
that.endTimeErrorTip = "结束时间不能大于当前时间";
that.isEnabledSearchBtn = false;
} else if (that.startTime > that.endTime) {
that.endTimeErrorTip = "结束时间不能小于开始时间";
that.isEnabledSearchBtn = false;
} else {
that.startTimeErrorTip = "";
that.endTimeErrorTip = "";
that.isEnabledSearchBtn = true;
}
}).data('datetimepicker');
}
六、错误提示演示



时间选择错误时,给出提示,并且查询按钮不可用(提示选择正确的时间)。
bootstrap datetimepicker 在 angular 项目中的运用的更多相关文章
- gulp 在 angular 项目中的使用
gulp 在 angular 项目中的使用 keyword:gulp,angularjs,ng,ngAnnotate,jshint,gulpfile 最后附完整简洁的ng项目gulpfile.js 准 ...
- angular项目中各个文件的作用
原文地址 https://www.jianshu.com/p/176ea79a7101 大纲 1.对angular项目中的一些文件的概述 2.对其中一些文件的详细描述 2.1.package.json ...
- Angular 项目中如何使用 ECharts
在有些使用 ECharts 库的 Angular 项目中,通常除了安装 npm 包之外,还会在 angular.json 中配置 “build.options.scripts”,将 “node_mod ...
- angular项目中遇到的问题
一.angular项目中如何实现路由缓存 需要实现的效果,对请求的数据进行缓存,比如进入文章详情页之后点击返回,不会再调用后台数据接口:而是加载缓存中的数据,如何数据变动的情况下,可使用下拉刷新刷新页 ...
- 在angular项目中使用bootstrap的tooltip插件时,报错Property 'tooltip' does no t exist on type 'JQuery<HTMLElement>的解决方法和过程
在angular4的项目中需要使用bootstrap的tooltip插件. 1. 使用命令安装jQuery和bootstrap npm install bootstrap jquery --save ...
- angular项目中使用Primeng
1.第一步把依赖添加到项目中 npm install primeng --save npm install @angular/animations --save npm install font-aw ...
- Angular项目中引入jQuery
npm install --save jquery npm install @types/jquery --save 在对应的组件中引入 import * as $ from "jquery ...
- angular项目中使用jquery的问题
1.使用npm命令往项目中添加jQuery. npm install jquery --save 2.在你想要用jQuery的组件中添加. import * as $ from "jquer ...
- Angular项目中共享模块的实现
创建share Modele:ng g m share import进来所有需要共享的模块都export出去, 一.共享CommonModule 暂时只有CommonModule,以后会有一些需要共享 ...
随机推荐
- c#基础学习(0806)之可变参数、ref和out关键字的简单使用
params可变参数: 1.无论方法有几个参数,可变参数必须出现再参数列表的最后,可以为可变参数直接传递一个对应类型的数组: 2.可变参数可以传递参数也可以不传递参数,如果不传递参数,则数组为一个长度 ...
- webfrom后台
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- MyEclipse在不同编辑面间快速切换
想在下面的编辑页面间切换有两种方式,一种是按“栈”方式切换,一种是按“链表”方式切换. 按栈方式切换: Alt + 左箭头: 上一次编辑页面 Alter + 右箭头 : 下一个编辑也页面 按链表(绝对 ...
- Hive,HANA可视化客户端工具
目前市面上的Hive可视化客户端工具,大都是C/S模式的,安装使用都不是太方便,目前有一款基于WEB的可视化工具TreeSoft,通过浏览器就可以访问使用了,并且可以同时管理.维护.监控MySQL,O ...
- JSP学习笔记(5)-Java Servlet
1.什么是Servlet? Servlet(Servlet Applet),全程Java Servlet,是用Java编写的服务器端程序,其主要功能在于交互式地浏览和修改数据,生成动态WEB内容.侠义 ...
- LOJ#6463 AK YOI 树分治+线段树合并
传送门 既然是树上路径统计问题,不难想到要使用树分治,这里以点分治为例 由点分治的性质,每层只需要考虑经过重心的路径 因为需要维护路径长度在一定范围内的最大权值和,所以要用一个数据结构维护一下到根节点 ...
- Android smartimageview网络图片查看器
调用代码: SmartImageView siv = (SmartImageView) findViewById(R.id.siv);siv.setImageUrl(et_path.getText() ...
- Android Studio 使用Intent实现页面的跳转(带参数)
不管是在APP,还是在网站中,页面之间的跳转都是很常见的,本文主要讲一下在APP中,如何通过Intent实现页面的跳转. 不带参数: 写在MainActivity页面的代码: Intent inten ...
- DBFlow(4.2)新版使用
DBFlow新版使用 一.DBFlow4.2.4介绍 DBFlow是一个基于AnnotationProcessing(注解处理器)的ORM框架.此框架设计为了速度.性能和可用性.消除了大量死板的数据库 ...
- C#Owin auth20开发 OwinStartup 不会触发的解决办法
在使用owin auth20设置token时候遇到一个问题.项目中已经存在如下初始化配置类 using Microsoft.Owin; using Owin; [assembly: OwinStart ...