【问题标题】:How could I set my datepicker to only show dates after a certain date如何将我的日期选择器设置为仅显示某个日期之后的日期
【发布时间】:2015-04-24 12:50:08
【问题描述】:

问题是我有两个日期选择器,一个用于 leave_start,另一个用于 leave_end。如果有人为未来的 text_field 选择“是”,我希望 datepicker 只显示人员周年纪念日之后的日期。我可能遇到的一个问题是员工的周年纪念日将是他们被雇用的时间,所以我不确定如何仅使用周年纪念日的月份和日期来仅显示周年纪念日之后的今年日期。 .

例如,我的名字是 Joe Smith,我选择未来的请求为“是”,我的周年纪念日是 2012 年 2 月 14 日。我希望我的 leave_start leave_end datepickers 仅显示今年 2/14 之后的日期。这可能吗?任何帮助将不胜感激!!!!

基本上我只是不希望人们选择未来的请求并且能够选择他们周年纪念日之前的几天。

如果有人需要更多信息,请告诉我这是我目前掌握的信息。任何帮助将不胜感激!!!!

这是我 entry.js.coffee 中的日期选择器

jQuery ->

 $('#leave_start').datepicker({minDate: 0, beforeShowDay: $.datepicker.noWeekends });


 $('#leave_end').datepicker({minDate: 0, beforeShowDay: $.datepicker.noWeekends });

这是我的看法

=simple_form_for @entry, :url => url_for(:controller => 'entry', :action 


%td.lt= f.error :range_days, :class => 'er'


%table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
  %th.lt Indirect Code:
  %td.lt= f.input_field :indirect_id, :as => :select, :id => 'indirect_id', :label => false, :collection => ['PD', 'VAC', 'ABS'], :hint => "If you choose ABS(this is considered an unpaid absence)"
  %td.lt= f.text_field :sick_day, :id => 'sick_day', :label => "This is for if you feel the need to comment on your unpaid request", :placeholder => 'Optional Comment', :input_html => {:value => ''}
  %td.lt= f.error :indirect_id, :class => 'er'

%table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
  %th.lt Will This Request be after your anniversary date?:
  %td.lt= f.input_field :future, :as => :select, :id => 'future', :label => false, :collection => ["YES", "NO"]
  %td.lt= f.error :future, :class => 'er'

%table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
  %th.lt Leave Start:
  %td.lt= f.text_field :leave_start,  :label => false, :id => 'leave_start', :input_html => {:value => ''}
  %td.lt= f.error :leave_start, :class => 'er'

%table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
  %th.lt Leave End:
  %td.lt= f.text_field :leave_end,  :label => false, :id => 'leave_end', :input_html => {:value => ''}
  %td.lt= f.error :leave_end, :class => 'er'


%td.lt= f.text_field :range_days, :label => false, :id => 'range_days', :input_html => {:value => ''}
%td.lt= f.text_field :full_range, :label => false, :id => 'gdates', :input_html => {:value => ''}


%table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
= f.button :submit, "Submit", :class => 'customSub'

这是我的这个人周年纪念日的模型

class Empcomp < ActiveRecord::Base

  establish_connection :vdev

  self.table_name = 'empcomp'

  belongs_to :entry

end

这是我新建和创建的入口控制器

  def new
    @entry = Entry.new
    @empaccrl = Empaccrl.where("person_id = ? and is_active = 'Y'", current_user.person_id)
    @empcomp = Empcomp.where("person_id = ?", current_user.person_id)
    respond_to do |format|

      format.html# new.html.haml
      format.xml { render :xml => @entry }
    end
  end


  def create
    params.permit!
    @entry = Entry.new(params[:entry])
    @entry.t_d
    @entry.day_hours
    @entry.current_user = current_user
    @empaccrl = Empaccrl.where("person_id = ? and is_active = 'Y'", current_user.person_id)
    @empcomp = Empcomp.where("person_id = ?", current_user.person_id)
    # send my email
    respond_to do |format|

      if @entry.save
        if current_user.email.nil?
          format.html { redirect_to(entry_path( @entry ), :notice => 'Entry successfully created, but you will not recieve any notifications, because you email is blank!') }
          format.xml { render :xml => @entry, :status => :created, :location => @entry }
        else
          EntryMailer.submit_for_approval(@entry).deliver
          format.html { redirect_to(entry_path( @entry ), :notice => 'Entry successfully created.') }
          format.xml { render :xml => @entry, :status => :created, :location => @entry }
        end
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @entry.errors, :status => :unprocessable_entity }
      end
    end
  end

这样的事情是我希望它工作的方式,但我无法从我的模型中获得周年纪念日。这个日期会有所不同,所以我不能将它设置为静态日期

$(document).ready(function(){
 $('#future').click(function() {
    var selected = $(this).val();
    if (selected == 'YES') {
     $('#leave_start').datepicker({minDate: '#anviersary_date', beforeShowDay: $.datepicker.noWeekends });
    } else {
     $('#leave_start').datepicker({minDate: 0, beforeShowDay: $.datepicker.noWeekends });
    }
  });
 });

【问题讨论】:

  • TL;博士!您只需设置 minDatemaxDate 设置来限制日期范围
  • 虽然更多的信息通常比太少的信息要好,但你可能在这里做得有点过头了。您应该尝试将代码限制为导致问题的代码。我怀疑你的咖啡脚本就足够了。
  • 我更新了我的问题以显示我希望它做什么和发挥作用..@Shadwell
  • 我更新了我的问题以显示我希望它做什么和发挥作用.. @adeneo
  • 我仍然认为,如果您缩短问题并更加集中而不是增加问题,您更有可能获得更好的答复。

标签: javascript jquery ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

这就是您拥有minDatemaxDate 的原因。

在初始化器上:

$( ".selector" ).datepicker({
  minDate: new Date(2007, 1 - 1, 1)
});

初始化后的设置:

$( ".selector" ).datepicker( "option", "minDate", new Date(2007, 1 - 1, 1) );

您无需在此处发布所有代码。请明确点。 :)

【讨论】:

  • 我希望这个日期基于他们的周年日期,我将有多个具有不同周年日期的用户..所以我怎么能像这样 $( "#leave_start" ) 把周年日期放在那里。 datepicker("option", "minDate", new Date('#aniversary_date')); $( "#leave_end" ).datepicker( "option", "minDate", new Date('#aniversary_date') );如果他们为未来选择“是”? ((((((((如果他们不选择日期选择器将保持与现在相同) $('#leave_start').datepicker({minDate: 0, beforeShowDay: $.datepicker.noWeekends }); $('# leave_end') @Marcos Lima
  • 用setter方法做这个应该没问题,怎么得到这个日期就是简单的js。如果你没有最小日期,它应该为 null,而不是 0。
  • 我不确定我是否理解这一点,我想为开始日期设置的日期将在我的 Rails 模型中。我如何告诉 datepicker 仅在我的模型中显示该日期的日期我无法设置最小日期,因为最小日期总是会根据用户周年日期而有所不同@Marcos Lima
  • 我更新了我的问题以显示我希望它做什么和发挥作用..@Marcos Lima
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多