在Rails ActiveAdmin中进行矩阵编辑? [英] Matrix Edit in rails ActiveAdmin?
问题描述
我在Rails中使用ActiveAdmin。
I'm using ActiveAdmin in Rails.
我的用例类似于货币兑换:说我有10种货币,一种货币可以转换为另一种货币一。为了支持编辑,我需要创建一个矩阵,其中行是CurrencyA,列是CurrencyB,值是从CurrencyA到CurrencyB的转换,如下所示:
My use case is similar to currency-exchange: say I have 10 currencies, and one currency can be converted into another one. In order to support editing, I need to create a matrix, where rows is CurrencyA, and columns is CurrencyB, and the value is conversion from CurrencyA to CurrencyB, something like this:
| | SGD | USD | HKD | CNY |
|-----|-----|-----|-----|-----|
| SGD | | | | |
| USD | | | | |
| HKD | | | | |
| CNY | | | | |
相应地,在我的数据库中,我有一个名为 currency_conversions $的表。 c $ c>,其中有:
Correspondingly, in my database, I have a table called currency_conversions
, where it has:
from_currency | to_currency | conversion_rate
(我的实际用例不是货币转换,但此示例可以更好地显示我的用例) 。
(My actual use case is not currency conversion, but this example can better show my use case).
但是,我找不到activeadmin具有这种功能。.有什么建议吗?
However, I cannot find activeadmin have such functionality.. any suggestions?
推荐答案
经过一番调查,我发现了
After quite some investigation, I've figured out
这里是(数据是伪造的)外观:
Here's how it looks like (the data is fake):
这就是我所做的:
- 定义自定义控制器: http://activeadmin.info/docs/8-custom-actions.html 处理get&在视图中发布请求
- ,准备表,这是表单
- 在
app /中定义相应的CSS asset / stylesheets / active_admin.css.scss
- define a custom controller: http://activeadmin.info/docs/8-custom-actions.html, handling the get & post request
- in the view, prepare the table, and it is a form
- define corresponding CSS in the
app/assets/stylesheets/active_admin.css.scss
更多,因为我今天有投票,让我在这里分享我的视图代码(格式为 .html.slim
);我多次重复使用它:
More over, since I've got an upvote today, let me share my code for the view here(it is .html.slim
format); I'm reusing it several times:
/ Required params:
/ - headers -- the headers array. each item would be passed to the header_blk
/ - left_headers -- the array for the headers on the left side
/ - rows -- contents for the table
/ - col_blk -- a block to get the content needed for each column, where what passed in is the:
/ column, row_id, col_id
/ Optional params:
/ - banner_top_right -- the banner text you want to put at the top-right of the splitter
/ - banner_bottom_left -- the banner text you want to put at the bottom-left of the splitter
- banner_top_right ||= ""
- banner_bottom_left ||= ""
table.admin-matrix
thead
tr
td.diagonal-splitter
svg(width='100%' height='100%')
line(x1='0' y1='0' x2='100%' y2='100%' style='stroke:rgb(0,0,0);stroke-width=2')
.triangle-top-right = banner_top_right
.triangle-bottom-left = banner_bottom_left
- headers.each do |header|
td.header
= header
tbody
- rows.each_with_index do |row, rid|
tr
td.header = left_headers[rid]
- row.each_with_index do |col, cid|
td
- col_blk[col, rid, cid]
这篇关于在Rails ActiveAdmin中进行矩阵编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!