2015-10-29 8 views
6

Uso il selettore mese jQuery (senza data) con il formato: 201110.Selettore jquery mese: impostazione di un intervallo min/max iniziale in conflitto con la funzione "da" <"a"

Voglio impostare una data minima (vedere nel codice), quindi prima definirla in django forms.py la data-min, quindi analizzare questa data in html. C'è prima un intervallo min/max. (anche se questo non funziona ora).

Quindi per realizzare la data "to" è sempre successiva alla data "from", ho bisogno di utilizzare anche la funzione min/max, quindi sostituisce gli intervalli del mese precedente che ho impostato da SQL.

La situazione attuale è che potrei realizzare "a" data> "da" data. Ma la data minima (proveniente da MySQL) non funziona ora.

Come potrei risolvere questo conflitto, per favore dimmelo. Grazie in anticipo

Grazie per la vostra risposta in anticipo.

<script type="text/javascript"> 
    $(function() { 
$("#from, #to").datepicker({ 
     changeMonth: true, 
     changeYear: true, 
     showButtonPanel: true, 
     dateFormat: 'yy MM', 
     minDate: $(this).date-min, ///it is here doesn't work, the minDate is coming from Django 

     onClose: function(dateText, inst) { 
      var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val(); 
      var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();    
      $(this).datepicker('setDate', new Date(year, month, 1)); 
     }, 
     beforeShow : function(input, inst) { 
      if ((datestr = $(this).val()).length > 0) { 
       year = datestr.substring(datestr.length-4, datestr.length); 
       month = jQuery.inArray(datestr.substring(0, datestr.length-5), $(this).datepicker('option', 'monthNames')); 
       $(this).datepicker('option', 'defaultDate', new Date(year, month, 1)); 
       $(this).datepicker('setDate', new Date(year, month, 1));  
      } 
      var other = this.id == "from" ? "#to" : "#from"; 
      var option = this.id == "from" ? "maxDate" : "minDate";   
      if ((selectedDate = $(other).val()).length > 0) { 
       year = selectedDate.substring(selectedDate.length-4, selectedDate.length); 
       month = jQuery.inArray(selectedDate.substring(0, selectedDate.length-5), $(this).datepicker('option', 'monthNames')); 
       $(this).datepicker("option", option, new Date(year, month, 1)); 
      } 
     } 
    }); 
    $("#btnShow").click(function(){ 
    if ($("#from").val().length == 0 || $("#to").val().length == 0){ 
     alert('All fields are required'); 
    } 
    else{ 
     alert('Selected Month Range :'+ $("#from").val() + ' to ' + $("#to").val()); 
     } 
    }) 
    }); 


    </script> 

Come definisco la mia min-date in django:

In models.py

class XXX(models.Model): 
    start_date=models.DateField(auto_now_add=True) 
    end_date=models.DateField(auto_now_add=True) 

In forms.py

class Meta: 
     model = XXX 
     fields = ('start_date','end_date') 
     date_min=XXX.objects.all().aggregate(Min('date')) <this field is NOT defined in models.py, is that OK?; and format is like 2011-11-01 --> 

     widgets = { 
       'start_date': forms.DateInput(attrs={'class':'datepicker', 
                'date-min':date_min,             
                }),       
      } 
+0

Non si può aggregare su un campo indefinito. Come vuoi definire 'date_min'? – Uri

risposta

2

Prima di tutto, si dovrebbe fare

$(this).attr('date-min') 

invece di

$(this).date-min 

per ottenere l'attributo date-min.

Il tuo prossimo problema è che chiami $(this) all'interno del dom ready callback, dove questo si riferisce all'oggetto documento non l'elemento #from o #to.

la soluzione che state cercando è

$(function() { 
    $("#from, #to").each(function() { 
     $(this).datepicker({ 
      .... 
      minDate: $(this).attr('date-min'), 
      .... 
     }) 
    }) 
) 

E non sono sicuro su questa linea:

date_min=XXX.objects.all().aggregate(Min('date')) <this field is NOT defined in models.py, is that OK?; and format is like 2011-11-01 --> 

E 'OK che si dispone di un campo in forma che non sia descritta nella relativa modello, ma non bacchetta per ottenere qualcosa come Min('start-date') o Min('end-date') qui? e fa il modulo definito come qualcosa di simile a

class XXXForm(forms.ModelForm): 
    class Meta: 
     .... 

non solo

class Meta: 
    .... 
Problemi correlati