Javascript 如何自定义角度材料日期选择器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33646049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Customise the angular material date-picker?
提问by Mr AJ
I am using the material design date picker
我正在使用材料设计日期选择器
<md-content flex class="padding-top-0 padding-bottom-0" layout="row">
<md-datepicker ng-model="user.submissionDate" md-placeholder="Start date" flex ng-click="ctrl.openCalendarPane($event)"></md-datepicker>
<md-datepicker ng-model="user.submissionDate" md-placeholder="Due date" flex></md-datepicker>
</md-content>
and its shows the UI like this
I want to remove the calendar icon and include the ng-click functionality in the input box.
我想删除日历图标并在输入框中包含 ng-click 功能。
How to bind the runtime event with the input box?
如何将运行时事件与输入框绑定?
css
css
<style>
.inputdemoBasicUsage .md-datepicker-button {
width: 36px; }
.inputdemoBasicUsage .md-datepicker-input-container {
margin-left: 2px;
}
.md-datepicker-input-container{
display:block;
}
.md-datepicker-input[placeholder]{
color=red;
}
.padding-top-0{
padding-top:0px;}
.padding-bottom-0{
padding-bottom:0px;
}
</style>
回答by Emir Marques
Use the function for manipulate event and hide image of calendar as:
使用操作事件和隐藏日历图像的功能为:
var app = angular.module('StarterApp', ['ngMaterial']);
app.controller('AppController', function($scope) {
$scope.initDatepicker = function(){
angular.element(".md-datepicker-button").each(function(){
var el = this;
var ip = angular.element(el).parent().find("input").bind('click', function(e){
angular.element(el).click();
});
angular.element(this).css('visibility', 'hidden');
});
};
});
.inputdemoBasicUsage .md-datepicker-button {
width: 36px;
}
.inputdemoBasicUsage .md-datepicker-input-container {
margin-left: 2px;
}
.md-datepicker-input-container {
display: block;
}
.md-datepicker-input[placeholder] {
color:red;
}
.padding-top-0 {
padding-top: 0px;
}
.padding-bottom-0 {
padding-bottom: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Angular Material Dependencies -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-animate.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-aria.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angular_material/0.11.2/angular-material.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/0.11.2/angular-material.min.css">
<div ng-app="StarterApp" ng-controller="AppController" ng-init="initDatepicker();">
<md-content flex class="padding-top-0 padding-bottom-0" layout="row">
<md-datepicker ng-model="user.submissionDate1" md-placeholder="Start date" flex ng-click="ctrl.openCalendarPane($event)"></md-datepicker>
<md-datepicker ng-model="user.submissionDate2" md-placeholder="Due date" flex></md-datepicker>
</md-content>
</div>
回答by LeonardoGuimaraes
using the above anwser, I changed this lines to display better:
使用上面的 anwser,我更改了这一行以更好地显示:
.md-datepicker-input-container {
width: 100%;
margin-left: 0px;
}
回答by shreedhar bhat
Additional to @EmirMarques answer, I wanted to make the input field read only. so i added this code. Here user can't edit the date input field. so its
除了@EmirMarques 的回答,我想让输入字段只读。所以我添加了这段代码。这里用户不能编辑日期输入字段。所以是
Better One
更好的一个
$scope.initDatepicker = function(){
angular.element(".md-datepicker-button").each(function(){
var el = this;
var ip = angular.element(el).parent().find("input").bind('click', function(e){
angular.element(el).click();
});
angular.element(el).parent().find("input").prop('readonly', true);
angular.element(this).css('visibility', 'hidden');
});
};
And this CSS made the look better
而这个 CSS 使看起来更好
.md-datepicker-input-container {
display: block;
margin-left: 0 !important;
width: 100% !important;;
}
回答by A. Salim
Why not using AngularJs MD functionality?
为什么不使用 AngularJs MD 功能?
- md-hide-icons="all|triangle|calendar": to hide all/triangle or calendar buttons;
- And md-open-on-focus to open datepicker on input box click.
- md-hide-icons="all|triangle|calendar":隐藏所有/三角形或日历按钮;
- 并且 md-open-on-focus 在输入框点击时打开日期选择器。
<md-content flex class="padding-top-0 padding-bottom-0" layout="row">
<md-datepicker ng-model="user.submissionDate" md-placeholder="Start date" flex md-hide-icons="calendar" md-open-on-focus></md-datepicker>
<md-datepicker ng-model="user.submissionDate" md-placeholder="Due date" flex md-hide-icons="calendar" md-open-on-focus></md-datepicker>
</md-content>