ActiveAdmin 自定义列表页

ActiveAdmin 自定义列表页

列表页

  • 最简单的列表样式
  index do
    column :title
    default_actions
  end

index页面有多种样式,可选的有:

  • Table: A table drawn with each row being a resource
  • Grid: A set of rows and columns each cell being a resource
  • Blocks: A set of rows (not tabular) each row being a resource
  • Blog: A title and body content, similar to a blog index
  • 显示外键
    如果表格里有外键,如Post 中有 Category id。

直接写 column :category_id 必然会显示一个数字,而我们要的肯定是显示 Category Name。

因此需要在 Category Model中,定义一个category_name方法。

def category_name
    self.name
end

使用时

 index do
    column :title
    column :category_name
    default_actions
  end
  • 设置是否分页
ActiveAdmin.register Post do
  config.paginate = false
end
  • 设置每页条数
ActiveAdmin.register Post do
  config.per_page = 10
end
  • 设置默认排序条件
ActiveAdmin.register Post do
  config.sort_order = 'name_asc'
end
  • 禁用删除功能
ActiveAdmin.register Post do
  actions :all, except: [:destroy]
end
  • 禁用新建功能
ActiveAdmin.register Post do
  actions :all, except: [:new]
end
  • 禁用更新功能
ActiveAdmin.register Post do
  actions :all, except: [:update]
end

过滤器

选项
  • 最简单的过滤器
filter :title
  • 指定标签
filter :title, label: '标题'
  • 指定类型
filter :title, as: :string, label: "标题"

过滤器类型,
:string 输入框
:date_range 选择日期区间
:numeric 选择“等于”、“包含”、“”
:select 下拉框
:check_boxes 勾选框

  • 下拉选择筛选
  filter :organization_id, :as=>:select, collection: proc { Organization.all }, :label=>"机构"
关闭过滤器

1、局部关闭
仅在Post中关闭过滤器。

ActiveAdmin.register Post do
  config.filters = false
end

2、全局关闭
关闭后,所有模块都不会显示过滤器。

ActiveAdmin.setup do |config|
  config.filters = false
end
参考资料

Customizing the Index Page
Disabling Actions on a Resource
Activeadmin disabling the “new resource” method

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值