开发者

Time select form helper with 12 hour format for Rails 3?

开发者 https://www.devze.com 2023-02-19 04:34 出处:网络
Is there a user-friendly time_select for Rails 3? The default time_select form helper gives you hours[00-23], minutes[00-59] and optionally sec开发者_如何学Conds[00-59]. A dropdown list of hours 0-23

Is there a user-friendly time_select for Rails 3? The default time_select form helper gives you hours[00-23], minutes[00-59] and optionally sec开发者_如何学Conds[00-59]. A dropdown list of hours 0-23 is pretty frustrating for those of us who aren't on military time. A user-friendly solution would display hours 1-12 and an extra am/pm dropdown list.

Is there an option or a separate plugin to handle this? I can't be the first person to want this, but I haven't found any solutions.

Thanks!


You can set :ampm option to true which will show the hours as: 12 PM, 01 AM .. 11 PM.

time_select 'game', 'game_time', {:ampm => true}

http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-time_select


I don't know of a build in helper for exactly what you want. I suggest you build one, and share it! You likely don't want the first item in the dropdown to be 12am either, so that should be a config option.

Also, if 12am is 0 or 24 should be configurable too.

Here's a base using vanilla select_tag:

<%= f.select :your_attribute, [ 
     ["6am", 6], ["7am", 7], ["8am", 8], ["9am", 9], ["10am", 10], ["11am", 11], ["12pm", 12], ["1pm", 13], ["2pm", 14], ["3pm", 15], ["4pm", 16], ["5pm", 17], ["6pm", 18], ["7pm", 19], ["8pm", 20], ["9pm", 21], ["10pm", 22], ["11pm", 23], ["12am", 24], ["1am", 1], ["2am", 2], ["3am", 3], ["4am",4 ], ["5am", 5]
], class:''%>

It displays 12hour time to your user, and posts a 24 hour integer to your server.

Update

I needed one anyway, so I went ahead a wrote it.... In the view:

<%= am_pm_hour_select f, :your_method, start:3 %>

(Note the syntax is not f.helper, but helper f, other_options)

In app/helpers/am_pm_form_helper.rb:

module AmPmFormHelper
  #pass start:Fixnum in the options hash to set the first drop down selection
  def am_pm_hour_select(object, method, options = {}, html_options = {})
    select_options = [ ["6am", 6], ["7am", 7], ["8am", 8], ["9am", 9], ["10am", 10], ["11am", 11], ["12pm", 12], ["1pm", 13], ["2pm", 14], ["3pm", 15], ["4pm", 16], ["5pm", 17], ["6pm", 18], ["7pm", 19], ["8pm", 20], ["9pm", 21], ["10pm", 22], ["11pm", 23], ["12am", 24], ["1am", 1], ["2am", 2], ["3am", 3], ["4am",4 ], ["5am", 5]]
    unless options[:start].nil?
      shift_if_needed = Proc.new{|hour, start| hour<start ? hour+24 : hour}
      select_options.sort!{|x, y| shift_if_needed.call(x.last,options[:start]) <=> shift_if_needed.call(y.last, options[:start]) }
    end

    object.select(method, select_options, options = {}, html_options = {})
  end
end


for this kind of purposes i recommend using jQuery UI. it's way more user friendly

here's a link


One simple thing you could do is convert the military 0..23 to standard 12am,1am,2am..10pm,11pm.

This works well for my situation - I'm usually just picking the hour - but it's not ideal, I admit, with the am/pm in the hour drop down.

= javascript_tag "$$('#event_start_at_4i option').each(function(option) { option.update(convert_hour(option.innerHTML)) })"

Then in Javascript (this can probably be cleaner)

function convert_hour(hour_string) {   
  if (hour_string.substring(0,1) == 0) {
    hour_string = hour_string.substring(1);   
  }   
  // Funny '08' parses to 0, but '07' parses to 7.  Removing leading 0 above fixes.
  var mh = parseInt(hour_string)   
  if (!isNaN(mh)) {
    var nh = mh % 12
    if (nh == 0) { nh = 12 }
    if (mh < 12) {
      nh = nh + 'am'
    } else {
      nh = nh + 'pm'
    }
    return nh   
  }   
  return hour_string 
}


I just had this same problem, and I saw this post so I thought I'd give my answer :

module DateTimeSelectorExtensions
  def self.included(base)
    base.send(:include, InstanceMethods)
    base.send(:alias_method_chain, :select_hour, :twelve_hour_time)
  end

  module InstanceMethods
    def select_hour_with_twelve_hour_time 
      return select_hour_without_twelve_hour_time(@datetime, @options) unless @options[:twelve_hour].eql? true
      if @options[:use_hidden] || @options[:discard_hour]
        build_hidden(:hour, hour)
      else
        select_options = []
        0.step(23, 1) do |value|
          if value == 0 && @options[:midnight]
            text = @options[:midnight].eql?(true) ? 'midnight' : @options[:midnight]
          elsif value == 12 && @options[:noon]
            text = @options[:noon].eql?(true) ? 'noon' : @options[:noon]
          else
            text = "#{value == 12 ? 12 : (value / 12 == 1 ? value % 12 : value)}#{value <= 11 ? ' AM' : ' PM'}"
          end
          tag_options = { :value => value }
          tag_options[:selected] = "selected" if hour == value
          select_options << content_tag(:option, text, tag_options)
        end
        select_options.rotate!(@options[:offset].to_i)
        select_options.unshift(@options[:prompt]) if @options[:prompt].present?
        build_select(:hour, (select_options.join("\n") + "\n").html_safe)
      end       
    end  
  end
end

ActionView::Helpers::DateTimeSelector.send(:include, DateTimeSelectorExtensions)

You use it as such :

select_hour Time.now, :twelve_hour => true, :offset => 6, :prompt => "--", :midnight => true, :noon => "high noon"
0

精彩评论

暂无评论...
验证码 换一张
取 消