简体   繁体   English

Rails 管理员输入掩码 [Rails 6]

[英]Rails Admin input mask [Rails 6]

I need some inputs to have masks.我需要一些输入来获得掩码。 Example: the phone field should be something like "(xxx) xxxxx-xxxx"示例:电话字段应类似于“(xxx)xxxxxx-xxxx”

How can I include these "()" and "-" with masks.如何在掩码中包含这些“()”和“-”。

My aplication uses Rails-Admin, and I tried to add some js and doesn't work when I acces ' /admin '我的应用程序使用 Rails-Admin,我尝试添加一些 js,但当我访问“/admin”时不起作用

You will need to use a custom plugin for this i recommend inputmask for jQuery because rails admin already includes jQuery.您需要为此使用自定义插件,我推荐jQuery的输入掩码,因为 rails admin 已经包含 jQuery。

Then you can include the library inputmask.js on然后你可以包含库inputmask.js

app/assets/javascripts/rails_admin/custom/inputmask.js

And the bindings for automasking on以及用于自动屏蔽的绑定

app/assets/javascripts/rails_admin/custom/inputmask.binding.js

And then add this file on your project然后将此文件添加到您的项目中

app/assets/javascripts/rails_admin/custom/ui.js

With this content有了这个内容

//= require_tree .
//= require 'rails_admin/custom/inputmask.js'
//= require 'rails_admin/custom/inputmask.binding.js'

Then is just a matter of configuring the field with the mask as the documentation of inputmask.js suggests something like this然后只是用掩码配置字段的问题,因为 inputmask.js 的文档建议像这样

  rails_admin do
    edit do
      field :phone do
        html_attributes do
          {
            'data-inputmask' => "'mask': '(999) - 99999-9999'"
          }
        end
      end
    end

I tried it on my project in it looks like this我在我的项目上试过它看起来像这样

Screenshot of field with custom mask带有自定义掩码的字段的屏幕截图

Struggled with this for a while as well, but this procedure finally worked:也为此苦苦挣扎了一段时间,但这个过程终于奏效了:

  1. Get the latest recommended version of this gem here and add to Gemfile: https://rubygems.org/gems/jquery-mask-plugin在此处获取此 gem 的最新推荐版本并添加到 Gemfile: https://rubygems.org/gems/jquery-mask-plugin

  2. Run bundle in terminal在终端中运行捆绑

  3. Restart rails server重启 Rails 服务器

  4. Add these lines (exactly as they are):添加这些行(原样):

//= require jquery.inputmask //= 需要 jquery.inputmask

//= require jquery.inputmask.extensions //= 需要 jquery.inputmask.extensions

//= require jquery.inputmask.numeric.extensions //= 需要 jquery.inputmask.numeric.extensions

//= require jquery.inputmask.date.extensions //= 需要 jquery.inputmask.date.extensions

..in this folder in your project: app/assets/javascripts/application.js ..在您项目的这个文件夹中:app/assets/javascripts/application.js

  1. Follow the instruction in the gem documentations for the method that suits you best: https://github.com/RobinHerbots/Inputmask (I used the jquery plugin)按照 gem 文档中的说明找到最适合您的方法: https://github.com/RobinHerbots/Inputmask (我使用了 jquery 插件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM