【问题标题】:Custom template types in ex_admin (Phoenix)ex_admin (Phoenix) 中的自定义模板类型
【发布时间】:2017-05-13 08:12:59
【问题描述】:

我在 Phoenix 中使用后端的 ex_admin 构建了一个博客,我想覆盖我的博客文章模板的一些默认输入。

我的帖子具有以下属性:

  schema "posts" do
    field :title, :string
    field :image, :string   # will store as URL, will reference external host like S3
    field :created_on, :datetime
    field :content, :text

    timestamps()
  end 

我想做以下事情:

  1. :title 字段保留为文本输入
  2. :image 创建上传者输入
  3. 保留Phoenix默认的多重下拉选择方案created_on
  4. 为我的:content 创建一个Quill wysiwyg(基本上放在一个针对:content 文本字段的脚本中)

理想情况下,我想做这样的事情:

defmodule MyBlog.ExAdmin.Post do
    use ExAdmin.Register

    register_resource MyBlog.Post do

    end

    form post do
        inputs do
            input post, :title, type: :string
            input post, :image, type: :file
            input post, :created_on, type: :datetime
            input post, :content, type: :text
        end
    end

    # Add a javascript hook that fires here to target the :content input

end

:title:image:created_on 字段都显示文本输入...我注意到改变文本字段的唯一类型是 type: :password。有没有办法从inputs do 列表中动态删除这些类型,还是我必须创建一个自定义模板并引用它?

【问题讨论】:

  • 花几分钟时间来查找您尝试执行的操作的语法。
  • 您是否尝试从input 宏中删除所有:type 选项?它们应该根据架构元数据自动正确呈现。
  • 感谢@StevePallen,在lib/ex_admin/form.ex 中找到了文档。删除 :type 会给我默认行为。

标签: elixir admin backend phoenix-framework


【解决方案1】:

首先,您通常不需要指定字段的类型。一次例外是 text 字段,因为文本和字符串字段都在模式元数据中键入为字符串。

这应该适用于表单上的 javascript:

form post do
   inputs do
        input post, :title, type: :string
        input post, :image, type: :file
        input post, :created_on, type: :datetime
        input post, :content, type: :text
    end
    javascript do
      form_javascript()
    end
end
def form_javascript, do: """
  $(document).ready(function() {
    // ... 
  });
"""

您真的不需要使用form_javascript 函数。你可以直接嵌入就可以了

    javascript do
      """
      $(document).ready(function() {
         // ... 
      });
      """
    end

但是,我喜欢将 javascript 分开,特别是如果它相当长的话。

【讨论】:

  • 效果很好。仍在尝试找出文件输入。 form.ex:346* :prompt - Sets a HTML placeholder。有没有这样的例子?尝试传递一个字符串input post, :image, prompt: "<input type=\"file\" name=\"pic\" accept=\"image/*\">"....和一个列表input post, :image, prompt: [input file]
  • 看看ex_admin_demo。我使用arc_ecto 上传文件。
  • type: :file,很简单。优秀作品。我将探索演示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 2013-06-19
  • 2012-01-04
  • 2011-12-16
  • 1970-01-01
相关资源
最近更新 更多