Popup Calendar and DateBoxEngine and Change Made

Community,

I have been putting off implementing a Date selector for too long.
Tonight, I finally put it in. I made the choice to go with the datebox
engine.

Once I got it installed and working, I realized I didn’t like the way it
required you to call it. Example:

<% @dateField = model.date %>
<%= date_box %>

or…

<% @dateField1 = model.date %>
<%= date_box(@dateField1)

which then requires you to trap on these fields during the update/create
actions of the controller. This didn’t seem correct. I wanted multiple
dates on a page and I wanted an easy way to get the calendar to popup.
Here is what I wanted:

<%= date_box( ‘model_name’, ‘field_name_for_date1’ ) %>
<%= date_box( ‘model_name’, ‘field_name_for_date2’ ) %>

etc…

which is consisent with other form helpers (minus the options hash).
Therefore, I made a minor change to the engine code. I made this change
to meet my needs and hopefully others will get some use of it too. It
changes the definition and I didn’t touch every aspect of the engine -
only the piece that I was interested in. Here it is for anyone else who
wants to work with the date_box like I wanted to.

If you have any questions or need clarification then let me know.

Thanks,

Michael

Modified file:
vendor/plugins/datebox_engine/app/helpers/application_helper.rb:

Replace existing date_box method with the following:

def date_box(object_name, method)
	calendar_ref = object_name + '_' + method
	<<-EOL
  • #{engine_image('icon-help.gif', :engine => 'datebox', :alt => 'Help', :id => calendar_ref + 'Help' ) }
  • #{text_field object_name, method, { :onChange => "magicDate(id);", :onFocus => "if (this.className != 'error') this.select()"} }
  • #{engine_image('icon-calendar.gif', :engine => 'datebox', :alt => 'Calendar', :id => calendar_ref + 'Button', :style => 'cursor: pointer;' ) }
</ul>
<div style="clear:both"></div>
<div id="#{calendar_ref}Msg"><script 

type=“text/javascript”>document.write(calendarFormatString);


EOL end

Turns out a few people have tried to use the code above and had problems
with the final result. It was pretty late so perhaps I pasted a version
that had a slight bug in it! :frowning: Anyway, here is something that has
worked for everyone who has tried it. I did make a few changes to the
layout so it is slightly different than the original version but more to
my liking.

If this gives you problems then please email me at codeslush at yahoo
dot com.

Regards,

Michael

def date_box(object_name, method)
calendar_ref = object_name + ‘_’ + method
<<-EOL



  • #{text_field object_name, method, { :onChange =>
    “magicDate(id);”, :onFocus => “if (this.className != ‘error’)
    this.select()”} }

  • #{engine_image(‘icon-calendar.gif’, :engine => ‘datebox’, :alt =>
    ‘Calendar’, :id => calendar_ref + ‘Button’, :style => ‘cursor: pointer;’
    ) }

  • #{engine_image(‘icon-help.gif’, :engine => ‘datebox’, :alt =>
    ‘Help’, :id => calendar_ref + ‘Help’ ) }






EOL
end

i just wanted to thank you, i’ve been stumbling with this from a few
days ago, almost got it to work, but it was somewhat buggy, im not too
good at JS, your implementation works great!

Pablo O. wrote:

i just wanted to thank you, i’ve been stumbling with this from a few
days ago, almost got it to work, but it was somewhat buggy, im not too
good at JS, your implementation works great!

Thank you. I wrote the author an email and sent him the changes as well
but I didn’t get any feedback - so I don’t know if he will add it to the
code base or not. Several people have been able to use this now and I
hope that he will consider adding the change. Either way, I’m glad it
was of help to you!

Regards,

Michael

Did this message get through? The reply afterwards doesn’t appear to
make
any mention of it.

On 8/11/06, Nathaniel B. [email protected] wrote:

-NSHB


Nathaniel B.

Inimit Innovations Inc.
http://inimit.com


Kind regards,

Nathaniel B.

Inimit Innovations Inc.
http://inimit.com

Hey Michael,

The patch looks nice, and I do appreciate your diff.

Any chance you able to post it to http://dev.toolbocks.com so I can be
sure
it gets included with the next version?

I promise, one is coming soon!

-NSHB

On 8/9/06, Pablo O. [email protected] wrote:

http://lists.rubyonrails.org/mailman/listinfo/rails


Kind regards,

Nathaniel B.

Inimit Innovations Inc.
http://inimit.com