Automatic method to set the tabindex using form helpers

后端 未结 1 1815
终归单人心
终归单人心 2021-02-01 07:17

Is there an easy way to have the form helpers set the tabindex parameter automatically when using form helpers in Rails?

Basically, I don\'t want to have to manually set

1条回答
  •  北恋
    北恋 (楼主)
    2021-02-01 07:53

    I usually add a method like this to ApplicationHelper

    def autotab
      @current_tab ||= 0
      @current_tab += 1
    end
    

    Then in my views I make calls to the helper with a :tabindex => autotab like so:

    <%= text_field "post", "login",:tabindex => autotab, :value => @login %>
    

    You can also modify all the text_field, check_box, methods one at a time to add the tabindex automatically, by adding something like this to your application helper: (untested but you get the point)

    def text_field_with_tabindex(*args)
      options = args.last
      options[:tabindex] = autotab if options.is_a?(Hash) && options[:tabindex].nil?
    
      text_field_without_tabindex(*args)
    end
    
    def self.included(base)
      base.class_eval do
        alias_method_chain :text_field, :tabindex
      end
    end
    

    That might be more trouble than it's worth

    0 讨论(0)
提交回复
热议问题