2017-03-13 104 views
2

我正在與angular2的mydatepicker一起工作,我使用datepicker爲我的網站在2日期範圍內進行請願。但是我需要做一些驗證,比如最初的日期不是在最後的日期之後,反之亦然,最後的日期不能在最初的日期之前。Angular2 mydatepicker disableSince選項不起作用

so reading the documentation,我發現在選項對象中我可以設置這些屬性,事情是,他們不工作,至少不是實時的,如果我從他們工作的開始修復他們,但這沒有用爲我的網站。

這是HTML模板:

<div class="col-md-5" style="float: right"> 

    <my-date-picker name="mydate" placeholder="Fecha final" [(options)]="optionsFechaFinal" (dateChanged)="onDateChanged($event,1)" required></my-date-picker> 

</div> 

<div class="col-md-5" style="float: right"> 

    <my-date-picker name="mydate2" placeholder="Fecha inicial" [(options)]="optionsFechaInicial" (dateChanged)="onDateChanged($event,0)" required></my-date-picker> 

</div> 

和角碼:

private optionsFechaInicial: IMyOptions = { 
    // other options... 
    dayLabels: { su: "Do", mo: "Lu", tu: "Ma", we: "Mi", th: "Ju", fr: "Vi", sa: "Sa" }, 
    monthLabels: { 1: "Ene", 2: "Feb", 3: "Mar", 4: "Abr", 5: "May", 6: "Jun", 7: "Jul", 8: "Ago", 9: "Sep", 10: "Oct", 11: "Nov", 12: "Dic" }, 
    todayBtnTxt: "Hoy", 
    firstDayOfWeek: "mo", 
    sunHighlight: true, 
    dateFormat: 'dd/mm/yyyy', 
    selectionTxtFontSize: '13px', 
    height: '30px' 
    }; 

    private optionsFechaFinal: IMyOptions = { 
    // other options... 
    dayLabels: { su: "Do", mo: "Lu", tu: "Ma", we: "Mi", th: "Ju", fr: "Vi", sa: "Sa" }, 
    monthLabels: { 1: "Ene", 2: "Feb", 3: "Mar", 4: "Abr", 5: "May", 6: "Jun", 7: "Jul", 8: "Ago", 9: "Sep", 10: "Oct", 11: "Nov", 12: "Dic" }, 
    todayBtnTxt: "Hoy", 
    firstDayOfWeek: "mo", 
    sunHighlight: true, 
    dateFormat: 'dd/mm/yyyy', 
    selectionTxtFontSize: '13px', 
    height: '30px' 
    }; 

/*some code not relevant*/ 


    onDateChanged(event: IMyDateModel, datepicker: number) { 

    //datepicker inicial 
    if (datepicker == 0) { 
     this.optionsFechaFinal.disableUntil = event.date; 
     this.initialDate = Date.parse(event.date.year + '-' + event.date.month + '-' + event.date.day)/1000; 
     console.log(this.optionsFechaFinal.disableUntil); 
     //datepicker final 
    } else if (datepicker == 1) { 

     this.optionsFechaInicial.disableSince = event.date; 
     this.finalDate = Date.parse(event.date.year + '-' + event.date.month + '-' + event.date.day)/1000; 
    } 


    } 

我不知道我是怎麼錯過了,什麼是錯的代碼?

回答

1

你必須改變引用整個選項對象,像這樣:

if (datepicker == 0) { 
    this.optionsFechaFinal = { 
    // other options... 
     disableUntil = event.date; 
    }; 
... 
} 

你不能變異對象的選項。

當然這不是正確的解決方案。首先,您必須複製對象選項而不引用以避免代碼重複,然後添加必要的屬性。

0

有前和使用一些日期後disableSince和disableUntil選項禁用的日期選擇一個選項

onDateChanged(event: IMyDateModel, datepicker: number) { 
    if (datepicker == 0) { 
     this.optionsFechaFinal = { 
      disableUntil : {year:event.date.year,month:event.date.month,day:event.date.day 
      }; 
     this.initialDate = Date.parse(event.date.year + '-' + event.date.month + '-' + event.date.day)/1000;} 
    } else if (datepicker == 1) { 

     this.optionsFechaInicial{ 
      disableSince : {year:event.date.year,month:event.date.month,day:event.date.day 
      }; 
     }; 
     this.finalDate = Date.parse(event.date.year + '-' + event.date.month + '-' + event.date.day)/1000; 
    } 
    }