Ever desired a method to handle a DateTime picker in Rhodes without reloading the page? The ideal solution would be to tap the datetime input field to popup the DateTime picker, choose a date and/or time and tap ‘Done’. This would automatically display the selection into the input field.
We were able to achieve this using 2 AJAX calls. Read on and get the full code to do this!
This is the proposed method (in the official Rhodes example) to use the DateTime picker:
- tap a ‘choose’ link (next to the datetime input field)
- choose the date and/or time and tap ‘Done’ button to hide the DateTime picker
- tap ‘Save’ button (which would reload the form) to display the selected date and/or time into the input field
Not only this is a tedious way to select a date and/or time, but it makes it difficult to manage a form (from the programming point of view).
The desired method is the following: tap the datetime input field to popup the DateTime picker, choose a date and/or time and tap ‘Done’ (this would automatically display the selection into the input field). We were able to achieve this using 2 AJAX calls:
- the first AJAX call pops up the native iPhone DateTime picker
<input id="starts_at" onclick="popupDateTimePicker('0', 'Choose Date & Time', 'starts_at');" name="starts_at" type="text" />- the second AJAX call is initiated every 0.3 seconds to check if a datetime was selected and to display it
<script type="text/javascript"><!--mce:0--></script>
The above code belongs to the view.
Here are the functions to add in application.js:
function displaySelectedDateTime(dom_id) { $(document).ready(function() { setInterval(function(){ $.get('/app/Datetime/selection', {field_key: dom_id}, function(data){ $('#'+dom_id).val(data); }); return false; }, 300); }); } function popupDateTimePicker(flag, title, field_key) { $.get('/app/Datetime/popup', { flag: flag, title: title, field_key: field_key }); return false; }
You’ll also need to add the following files:
require 'rho/rhocontroller' require 'dateME' class DatetimeController < Rho::RhoController $date_time = {} def popup flag = @params['flag'] if ['0', '1', '2'].include?(flag) DateTimePicker.choose url_for(:action => :callback), @params['title'], Time.new, flag.to_i, Marshal.dump({:flag => flag, :field_key => @params['field_key']}) end end def selection @datetime = $date_time[@params['field_key']] ? $date_time[@params['field_key']] : '' render :action => :selection, :layout => false, :use_layout_on_ajax => false end def callback if @params['status'] == 'ok' datetime_vars = Marshal.load(@params['opaque']) format = case datetime_vars[:flag] when "0" then '%F %T' when "1" then '%F' when "2" then '%T' else '%F %T' end $date_time[datetime_vars[:field_key]] = Time.at(@params['result'].to_i).strftime(format) WebView::refresh end end end
< %= @datetime %>



App submission changes
iPhone 4 reception issue: myth or fact? Design flaw or software issue?
How to build and submit a Universal App for distribution on the App Store with the iOS 4 SDK
Microsoft Pink smartphone project: KIN gets killed
The WebView::refresh needs to be removed when using the rhomobile 2.0 sdk.
As a newbee, I am trying to run the example with the 2.0 sdk for iphone with latest xcode. The application compiles fine, runs, but the application.js is not used. I have tried to include the
in the layout header, but this does not make a difference, the datepicker is not called, just the regular keyboard.
Torsten, I’ve seen the same thing in bringing up an app in rhomobile 2 and android. I’m modeling our rhomobile 2 behavior after the latest example app at git://github.com/rhomobile/rhodes-system-api-samples.git
Make sure you are running the latest gem and carefully point sdk in build.yml at it.