基于angular實現(xiàn)三級聯(lián)動的生日插件
寫了一個生日聯(lián)動插件具體的效果是這樣的:
具體的數(shù)據(jù)
我取得數(shù)據(jù)是今年的數(shù)據(jù),如果是想要做三級聯(lián)動的日期插件,改一下時間就好了
var app=angular.module("dataPicker",[])
app.factory('dataPicker', ['$http', '$q', function ($http, $q) {
return {
query: function () {
var lengthYear=100;
var dataPicker={
month:[],
year:[],
day:[]
};
var data = new Date();
var nowyear = data.getFullYear();
for(var i=nowyear,j=0; i>nowyear-lengthYear;i--,j++){
dataPicker.year[j]=i;
}
for(var i=0;i<=11;i++){
if(i<9){
dataPicker.month[i]='0'+(i+1);
}else{
dataPicker.month[i]=String(i+1);
}
}
return dataPicker;
}
}
}])
directive插件的主要內(nèi)容
app.directive('selectDatepicker', function ($http,dataPicker) {
return {
restrict: 'EAMC',
replace: false,
scope: {
birthday: '=birthday'
},
transclude: true,
template: '<span>生日</span>'+
'<select class="sel_year" ng-model="birY" ng-change="changeYear()"><option ng-repeat="x in yearAll">{{x}}</option></select>'+
'<select class="sel_month" ng-model="birM" ng-change="changeMonth()" ng-disabled="birY==\'\'"><option ng-repeat="x in MonthAll">{{x}}</option> </select>'+
'<select class="sel_day" ng-model="birD" ng-disabled="birM==\'\'" ng-change="changeDay()"><option ng-repeat="x in DayAll">{{x}}</option></select>',
link: function (scope, element){
var arr=[];
scope.birthday=scope.birthday=='0000-00-00'?"":scope.birthday
var shuju=dataPicker.query()
scope.yearAll=shuju.year;
scope.MonthAll=shuju.month;
if(scope.birthday){
scope.birY=scope.birthday.birthday.split('-')[0];
scope.birM=String(scope.birthday.birthday.split('-')[1])
}else{
scope.birY="";
scope.birM="";
}
scope.getDaysInOneMonth=function(year, month){
var month1 = Number(month);
month1=parseInt(month1,10)
var d= new Date(Number(year),month1,0);
return d.getDate();
}
scope.getDayArr=function(day){
shuju.day=[];
for(var i=0; i<day;i++){
if(i<9){
shuju.day[i]='0'+(i+1)
}else{
shuju.day[i]=String((i+1));
}
}
}
if(scope.birthday){
var day=scope.getDaysInOneMonth(scope.birthday.birthday.split('-')[0],scope.birthday.birthday.split('-')[1]);
scope.getDayArr(day)
scope.DayAll=shuju.day;
scope.birD=scope.birthday.birthday.split('-')[2]
}
scope.changeYear=function(){
scope.birD="";
scope.birM="";
}
scope.changeMonth=function(){
var day=scope.getDaysInOneMonth(scope.birY,scope.birM);
console.log(day)
scope.getDayArr(day);
scope.DayAll=shuju.day;
scope.birD="";
}
scope.changeDay=function(){
scope.returnDate();
}
scope.returnDate=function(){
if(!scope.birD||!scope.birY||!scope.birM){
scope.birthday.returnValue="";
}else{
arr[0]=scope.birY;
arr[1]=scope.birM;
arr[2]=scope.birD;
scope.birthday.returnValue=arr.join("-");
}
}
}
}
})
});
html
<div select-datepicker birthday="birthday">
js 傳入的數(shù)據(jù)
$scope.birthday={
birthday:1993-01-20,
returnValue:'',
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Webpack 實現(xiàn) AngularJS 的延遲加載
這篇文章主要介紹了Webpack 實現(xiàn) AngularJS 的延遲加載的相關(guān)資料,需要的朋友可以參考下2016-03-03
利用Angular+Angular-Ui實現(xiàn)分頁(代碼加簡單)
這篇文章主要介紹了利用Angular+Angular-Ui實現(xiàn)分頁,利用Angular+Angular-Ui實現(xiàn)的分頁分頁代碼更加簡單,更加容易懂哦,相信本文的內(nèi)容對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
解決angularJS中input標(biāo)簽的ng-change事件無效問題
今天小編就為大家分享一篇解決angularJS中input標(biāo)簽的ng-change事件無效問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Angular.JS判斷復(fù)選框checkbox是否選中并實時顯示
最近因為工作需要做了一個選擇標(biāo)簽的功能,把一些標(biāo)簽展示給用戶,用戶選擇自己喜歡的標(biāo)簽,就類似我們在購物網(wǎng)站看到的那種過濾標(biāo)簽似的,所以這篇文章就給大家介紹了Angular.JS判斷復(fù)選框checkbox是否選中并實時顯示的方法,下面來一起看看吧。2016-11-11
AngularJS中$watch和$timeout的使用示例
這篇文章給大家介紹了AngularJS中$watch和$timeout的使用例子,通過示例代碼相信更能讓大家理解,有需要的朋友們下面來一起看看吧。2016-09-09

