我如何为SimpleForm编写更清洁的日期选择器输入 [英] How do i write a cleaner date picker input for SimpleForm

查看:125
本文介绍了我如何为SimpleForm编写更清洁的日期选择器输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢 simple_form gem for rails,但我不喜欢这行代码:

 <%= f.input:deadline,:as => :string,:input_html => {:class => 'date_picker'}%> 

我想写:

 <%= f.input:deadline,:as => :date_picker%> 

甚至可以重写:date / :datetime 完全匹配器。



但我真的不想写一个完整的 custom_simple_form



我认为这一定是可能的......



请帮忙谢谢

p>

解决方案

您必须定义一个新的 DatePickerInput 类。 b
$ b

 模块SimpleForm 
模块输入
class DatePickerInput< Base
def input
@ builder.text_field(attribute_name,input_html_options)
end
end
end
end



您现在可以写入

 < / p %= f.input:截止日期,:as => :date_picker%> 

当然您也需要

  $(input.date_picker)。datepicker(); 

位于 application.js



这对于本地化日期非常有用。看看这个:

  module SimpleForm 
模块输入
class DatePickerInput< Base
def input
@builder.text_field(attribute_name,input_html_options.merge(datepicker_options(object.send(attribute_name))))
end

def datepicker_options(value =无)
datepicker_options = {:value => value.nil? nil:I18n.localize(value)}
end

end
end
end

您现在已在文本字段中显示本地化日期!



更新:更简单的方法

 模块SimpleForm 
模块输入
class DatePickerInput< SimpleForm :: Inputs :: StringInput
def input_html_options
value = object.send(attribute_name)
options = {
value:value.nil ?? nil:I18n.localize(value),
data:{behavior:'datepicker'}#例如
}
#添加您需要的所有html选项...
super。合并选项
结束
结束
结束
结束

继承自 SimpleForm :: Inputs :: StringInput (如@ kikito所说)并添加一些html选项。
如果你还需要一个特定的类,你可以添加一些像

  def input_html_classes 
super.push( 'date_picker')
结束


I love the simple_form gem for rails but i dont like this line of code:

<%= f.input :deadline, :as => :string, :input_html => { :class => 'date_picker' } %>

I would like to write:

<%= f.input :deadline, :as => :date_picker %>

or even over write the :date / :datetime matchers completely.

But i dont really want to write a whole custom_simple_form

I think it must be possible...

Please help thanks

解决方案

You have to define a new DatePickerInput class.

module SimpleForm
  module Inputs
    class DatePickerInput < Base
      def input
        @builder.text_field(attribute_name,input_html_options)
      end    
    end
  end
end

And you can now write

<%= f.input :deadline, :as => :date_picker %>

Off course you also need

 $("input.date_picker").datepicker();

in application.js

This is very useful to localize dates. Look at this:

module SimpleForm
  module Inputs
    class DatePickerInput < Base
      def input
        @builder.text_field(attribute_name, input_html_options.merge(datepicker_options(object.send(attribute_name))))
      end

      def datepicker_options(value = nil)
        datepicker_options = {:value => value.nil?? nil : I18n.localize(value)}
      end

    end
  end
end

You have now a localized date in the text field!

Update: a cleaner way to do the same

module SimpleForm
  module Inputs
    class DatePickerInput < SimpleForm::Inputs::StringInput
      def input_html_options
        value = object.send(attribute_name)
        options = {
          value: value.nil?? nil : I18n.localize(value),
          data: { behaviour: 'datepicker' }  # for example
        }
        # add all html option you need...
        super.merge options
      end
    end
  end
end

Inherit from SimpleForm::Inputs::StringInput (as @kikito said) and add some html options. If you need also a specific class you can add something like

def input_html_classes
  super.push('date_picker')
end

这篇关于我如何为SimpleForm编写更清洁的日期选择器输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆