jquery月份选择器:设置初始最小/最大范围与“从”相冲突<"至"功能 [英] jquery month picker: setting an initial min/max range conflicts with "from" <"to" function

查看:166
本文介绍了jquery月份选择器:设置初始最小/最大范围与“从”相冲突<"至"功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery月份选择器(没有日期),格式如下:201110。

I use jQuery month picker (without date) with format like:201110.

我想设置一个最小日期(见代码),所以首先定义它在django forms.py的date-min中,然后将此日期解析为html。首先有最小/最大范围。 (虽然现在不起作用)。

I want to set a min date (see in code), so first define it in django forms.py the date-min, then parse this date to html. There is a min/max range first. (though this not work now).

为了实现到日期始终晚于从开始,我需要使用最小/最大功能那么它也会覆盖我从SQL设置的上一个月的范围。

Then in order to realize the "to" date is always later than "from" date, I need to use min/max function also, then it overrides the previous month ranges I set from SQL.

从日期起,我可以实现到日期。但是最近的日期(来自MYSQL)现在不起作用。

The currenct situation is I could realize "to" date > "from" date. But the min date(coming from MYSQL) doesn't work now.

如何解决这个冲突,请告诉我。感谢您提前

How could I solve this conflict, please tell me. Thanks in advance

感谢您的回覆。

<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>

我如何在dJango中定义我的最小日期:

How I define my min-date in dJango:

在models.py

In models.py

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

在表单中。 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,                                                   
                                                     }),                          
            }


推荐答案

你应该做

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

而不是

$(this).date-min

获取 date-min 属性

您的下一个问题是您在dom准备好的回调中调用 $(this)这是指不是 #from #to 元素的文档对象。

Your next problem is that you are calling $(this) inside the dom ready callback, where this refers to the document object not the #from or #to element.

您正在寻找的解决方案是

The solution you are looking for is

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

我不知道这一行:

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 -->

没有关联模型中没有描述的形式的字段,您可以使用 Min('start-date') Min('end-date')
你的表单定义如下:

It's OK that you have a field in form which is not described in related model, but don't you wand to get something like Min('start-date') or Min('end-date') here? And does your form defined as something like

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

不只是

class Meta:
    ....

这篇关于jquery月份选择器:设置初始最小/最大范围与“从”相冲突&LT;&QUOT;至&QUOT;功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆