【问题标题】:Automatic method to set the tabindex using form helpers使用表单助手设置 tabindex 的自动方法
【发布时间】:2008-10-21 23:53:38
【问题描述】:

在 Rails 中使用表单助手时,是否有一种简单的方法可以让表单助手自动设置 tabindex 参数?

基本上,我不想在构建表单时手动设置每个表单元素上的选项卡索引(我在更改内容时总是忘记更新它们)。我写的大部分表格基本上都是一个字段列表。选项卡索引应按它们定义的顺序排列。理想情况下,我会在 form_for 调用中设置初始索引,其他一切都会为我处理。

有人知道怎么做吗?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    我通常会在ApplicationHelper中添加这样的方法

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

    然后在我看来,我用:tabindex => autotab 像这样调用助手:

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

    您还可以一次修改所有 text_fieldcheck_box 方法以自动添加 tabindex,方法是向您的应用程序助手添加类似这样的内容:(未经测试,但您明白了)

    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
    

    这可能比它的价值更麻烦

    【讨论】:

    • 我使用局部变量而不是实例变量,请您解释一下为什么如果我使用 current_tab 而不是 @current_tab 会不起作用
    猜你喜欢
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多