ActiveAdmin如何从列中获取总和

时间:2012-08-09 20:12:00

标签: ruby-on-rails activeadmin

我有一种情况,我想得到一列的总和并显示它

例如在activeadmin中

ActiveAdmin.register Expense do    
   index do       

    column :amount 
    column :details
    column :created_at

    default_actions
  end         
end

我需要对amount列求和并显示它。此外,我无法弄清楚在哪里显示Total Sum,也许是边栏? 如果结果被过滤,则总和必须相应地改变显示的结果。

6 个答案:

答案 0 :(得分:3)

ActiveAdmin.register Expense do    
  amount = 0
  index do       

  column :amount 
  column :details
  column :created_at
  column("sub amount") {|resource| amount = amount + resource.amount}
  default_actions
  div :class => "panel" do
    h3 "Total: #{amount}"
  end
 end         
end

答案 1 :(得分:3)

您可以通过简单地访问将考虑当前应用的过滤器/范围的基础collection,并使用reduce(:+)执行总和来避免计算运行总计:

ActiveAdmin.register Expense do    
   index do       
    column :amount 
    column :details
    column :created_at

    default_actions

    div class: "panel" do
      h3 "Total amount: #{collection.pluck(:amount).reduce(:+)}"
    end
  end         
end

答案 2 :(得分:2)

我有同样的问题但找不到答案,所以在玩了一下后,我想出了这个:

ActiveAdmin.register Expense do
  index do

    column :amount
    column :details
    column :created_at

    default_actions

    div :class => "panel" do
      h3 "Total: #{Expense.search(params[:q]).result.sum(:amount)}"
    end
  end
end

答案 3 :(得分:0)

当我尝试使用上面提到的代码" amount = 0"在" index do"之上,当我刷新管理页面时,值会增加。这解决了我的问题。

Private Sub Calca1()
        If flag60 = 1 Then
        Sheets("KPIAgent").Cells(trow2 + 1, 1).Value = "Agnes"
        CS_Yes = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("R4:R65536"), "Yes", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 30
        CS_No = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("R4:R65536"), "No", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 0
        Sheets("KPIAgent").Cells(trow2 + 1, 3).Value = (CS_Yes + CS_No) / var60
        HT_Yes = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("T4:T65536"), "Yes", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 20
        HT_No = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("T4:T65536"), "No", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 0
        Sheets("KPIAgent").Cells(trow2 + 1, 4).Value = (HT_Yes + HT_No) / var60
        H_Yes = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("V4:V65536"), "Yes", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 40
        H_No = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("V4:V65536"), "No", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 0
        Sheets("KPIAgent").Cells(trow2 + 1, 5).Value = (H_Yes + H_No) / var60
        RP_Yes = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("X4:X65536"), "Yes", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 10
        RP_No = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("X4:X65536"), "No", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 0
        Sheets("KPIAgent").Cells(trow2 + 1, 6).Value = (RP_Yes + RP_No) / varreport60
        trow2 = trow2 + 1
        End If
End Sub

Private Sub CommandButton2_Click()
Sheets.Add.Name = "KPIAgent"
Sheets("KPIAgent").Activate
Sheets("KPIAgent").Cells(1, 1).Value = "Agent Name"
Sheets("KPIAgent").Cells(1, 2).Value = "AVG Score"
Sheets("KPIAgent").Cells(1, 3).Value = "AVG Common Sense Score"
Sheets("KPIAgent").Cells(1, 4).Value = "AVG Human Touch Score"
Sheets("KPIAgent").Cells(1, 5).Value = "AVG Helpful Score"
Sheets("KPIAgent").Cells(1, 6).Value = "AVG Reporting Score"
Sheets("KPIAgent").Cells(1, 7).Value = "Satisfaction - STP"
Sheets("KPIAgent").Cells(1, 8).Value = "Satisfaction - TP"
Sheets("KPIAgent").Cells(1, 9).Value = "Satisfaction - P"
Sheets("KPIAgent").Cells(1, 10).Value = "Satisfaction - SP"
    Sheets("KPIAgent").Columns("A:J").Select
    Selection.EntireColumn.AutoFit
    Sheets("KPIAgent").Range("A1:J1").Font.Bold = True
Dim i As Integer
Dim flag60 As Integer
Dim trow As Integer
Dim trow2 As Integer
Dim var60 As Integer
Dim varreport60 As Integer
var60 = Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("K4:K65536"), "Agnes")
varreport60 = Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("K4:K65536"), "Agnes", Sheets("RawData").Range("Q4:Q65536"), "Recording")
trow = Sheets("RawData").UsedRange.Rows.Count
trow2 = Sheets("KPIAgent").UsedRange.Rows.Count
For i = 4 To trow
        If Sheets("RawData").Cells(i, 11).Value = "Agnes" Then
        flag60 = 1
        End If
Next i
Dim CS_Yes As Integer
Dim CS_No As Integer
Dim CS As Integer
Dim HT_Yes As Integer
Dim HT_No As Integer
Dim HT As Integer
Dim H_Yes As Integer
Dim H_No As Integer
Dim H As Integer
Dim RP_Yes As Integer
Dim RP_No As Integer
Dim RP As Integer
Call Calca1
End Sub

答案 4 :(得分:0)

我更喜欢只在没有列的面板上看到金额("子金额"),这个代码也是如此:

   ActiveAdmin.register Expense do    

    index do       
      amount = 0
      column :amount 
      column :details
      column :created_at
      registers.each {|resource| amount += resource.amount}
      default_actions
     div :class => "panel" do
       h3 "Total: #{amount}"
     end
   end         
 end

答案 5 :(得分:-1)

ActiveAdmin.register Expense do    

  index do       
    sum = 0
    column "Amount" do |expense|
      sum += expense.amount 
      expense.amount
    end
    column :details
    column :created_at
    default_actions
    div :class => "panel" do
      h3 "Total: #{sum}"
    end
  end         
end