Using calendar_select in DHTML Calendar plugin for rails
The DHTML / JavaScript Calendar is a javascript library for creating customized calendars. The library has been ported as a rails plugin, dhtml calendar.
Using calendar_select from this plugin, I found that the date time fields were not being updated after I picked up a date from date select popup. I had to modify lines 27-35 in calendar.rb from
if show_field == :select
input_field_year_id = "#{object}[#{method}(1i)]"
input_field_month_id = "#{object}[#{method}(2i)]"
input_field_day_id = "#{object}[#{method}(3i)]"
if calendar_options.has_key?(:showsTime) and calendar_options[:showsTime]
input_field_hour_id = "#{object}[#{method}(4i)]"
input_field_minute_id = "#{object}[#{method}(5i)]"
end
end
to
if show_field == :select
input_field_year_id = "#{object}_#{method}_1i"
input_field_month_id = "#{object}_#{method}_2i"
input_field_day_id = "#{object}_#{method}_3i"
if calendar_options.has_key?(:showsTime) and calendar_options[:showsTime]
input_field_hour_id = "#{object}_#{method}_4i"
input_field_minute_id = "#{object}_#{method}_5i"
end
end
The plugin internally uses datetime_select and date_select to create year select with DOM id as “#{object}_#{method}_1i” and DOM name as “#{object}[#{method}(1i)]“. The DOM name variable was been referenced in the calendar.rb which did not update the fields when user tries to pick a date from popup. On changing this to DOM id variable, everything worked fine.
3 Comments, Comment or Ping
Billy
Awesome tip. Just what I was looking for! The line number wasnt correct for my version of calendar.rb. It was line 288 for me. Nothing that searching for “if show_field == :select” didn’t fix. Also be sure to restart rails afterwards for the change to take.
Now to figure out how to get it working the other way around. Selecting a different date via dropdowns does not change the calendar!
May 15th, 2009
Prakash Teli
Thank you so much for this posting. i was pulling my hair to get dhtml_calendar to work in a AJAX call. This fix was the final problem I has to resolve to get it to work.
Thanks again,
May 17th, 2009
Prakash Teli
OK.I solved the second piece of the puzzle with this control.
The problem I was having is that the default date displayed in the select boxes for year, month, date, etc. is always for the current day regardless of the actual value of my datetime form field.
I tracked the problem down to following code fragment in caledar.rb (between lines 350 - 360):
field_options[:start_year] = calendar_options[:range][0]
field_options[:end_year] = calendar_options[:range][1]
Put following code above this lines:
if html_options.has_key?(:date) && html_options[:date]
date = html_options[:date]
field_options[:value] = date
field_options[:default] = date
end
And make sure to pass the the ruby datetime object in the htlml options as follows:
‘date’,
:date => @item.publish_up,
:field_title => ‘Publish Up’,
:button_title => ‘Show calendar’ },
{ :firstDay => 1,
:range => [@item.publish_up.year, @item.publish_up.year + 25],
:step => 1,
:showOthers => true,
:showsTime => true,
:cache => true }
%>
—————
I can’t believe there are so many bugs in this plugin.
It defeats the purpose of using plugins if you have to weed through the code so much.
May 17th, 2009
Reply to “Using calendar_select in DHTML Calendar plugin for rails”