列の合計を表示する必要があるフッターを持つTelerikグリッドがあります。ただし、列のデータ型の1つはですTimeSpan
。これはTelerikのSum集計ではサポートされていません。GridBoundColumnBuilder.Aggregate()
を使用して集計を追加する必要があります。したがって、基本的に問題は、telerikのAggregate()メソッドでカスタム集計を参照する方法だと思います。そして、私が間違っていることに気付いた場合は、遠慮なく指摘してください:)この記事SumAggregate
を使用して、以下に示す、カスタム集計用のクラスを作成しました。(これは完了していないことに注意してください-記事から抜粋しました。実際にはまったく異なる集計を実装しています)
SumAggregate.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Web.Mvc;
namespace TelerikPOC.CustomAggregates
{
public class SumAggregate : AggregateFunction
{
private System.Collections.Generic.List<object> distinctValues;
/// <summary>
/// Initializes the current aggregate function to its initial
/// state ready to accumulate and merge values.
/// </summary>
/// <remarks>
/// This method is called every time the accumulation of values
/// must start over for a new subset of records from the data source.
/// </remarks>
public void Init()
{
this.distinctValues = new System.Collections.Generic.List<object>();
}
/// <summary>
/// Accumulates new argument values to the current aggregate function.
/// </summary>
/// <remarks>
/// This aggregate function accepts one argument:
/// number - a numeric value to accumulate to the aggregate function;
/// </remarks>
public void Accumulate(object[] values)
{
if (!distinctValues.Contains(values[0]))
{
distinctValues.Add(values[0]);
}
}
/// <summary>
/// Merges the specified aggregate function to the current one.
/// </summary>
/// <param name="Aggregate">
/// Specifies an aggregate function to be merged to the current one.
/// </param>
/// <remarks>
/// This method allows the reporting engine to merge two accumulated
/// subsets of the same aggregate function into a single result.
/// </remarks>
public void Merge(AggregateFunction aggregate)
{
// Accumulate the values of the specified aggregate function.
System.Collections.Generic.List<object> sums1 = ((SumAggregate)aggregate).distinctValues;
foreach (object o in sums1)
{
this.Accumulate(new object[] { o });
}
}
/// <summary>
/// Returns the currently accumulated value of the aggregate function.
/// </summary>
/// <returns>
/// The currently accumulated numeric value of the aggregate function.
/// </returns>
public object GetValue()
{
return this.distinctValues.Count;
}
}
}
そして、これが集計を追加するためのコードです。これは、 index.cshtmlの以下のコードと競合しますが、答えにさらにオプションを与えるために、集計を追加する両方の方法を含めたいと思いました。これは、現在使用しているような組み込みの集計ではなく、カスタム集計を使用するように変更する必要があります。
GridHelper.cs(未完成-後で列などをループするロジックを追加します。ちなみに、誰かがそれを手伝ってくれるなら、私は非常に感謝していますが、私はまだ認めません」まだ何も試していません。)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Web.Mvc.UI.Fluent;
using TelerikPOC.CustomAggregates;
namespace TelerikPOC.Helpers
{
public class GridHelper
{
public static void AddAggregateToColumn(GridBoundColumnBuilder<dynamic> columnBuilder, string Aggregate)
{
switch (Aggregate)
{
case "Sum":
{
columnBuilder.Aggregate(aggregates => aggregates.Sum())
.GroupFooterTemplate(result => "Sum:" + result.Sum)
.ClientFooterTemplate("Sum: <#= Sum #>")
.FooterTemplate(result => "Total: " + result.Sum);
}
break;
}
}
}
}
そして、次のHtmlHelper
ように、クラスを使用してtelerikグリッドを構築/レンダリングしています。
Index.cshtmlから:(必ず右側のコメントを読んでください)
@{
Html.Telerik()
.Grid(Model)
.Name("statisticalGrid")
.Columns(columns =>
{
columns.Bound(o => o.PlanID).Aggregate(something); //This is probably going to be where
columns.Bound(o => o.SessionID).Aggregate(something); //I need the help. Just not sure
columns.Bound(o => o.TimeSpan).Aggregate(something); //how to reference the custom
columns.Bound(o => o.TimeSpanDouble).Aggregate(something); //aggregate here, in
}) //place of `something`
.Sortable(sortable => sortable.Enabled(true))
.Filterable()
.Pageable(page => page.PageSize(25))
.Reorderable(reorder => reorder.Columns(true))
.Groupable(groupable => groupable.Enabled(true))
.ClientEvents(events => events
.OnColumnReorder("onReorder"))
.Render();
}
つまり、基本的に問題は、telerikのAggregate()
メソッドでカスタム集計を参照する方法だと思います。そして、私が間違っていることに気づいたら、遠慮なく指摘してください:)
編集:CreateAggregateExpression(Expression, bool)
SumAggregateクラスにメソッドを実装する必要があることに気づきました。ただし、それを実装する方法が完全にはわかりません。
最終編集:カスタムの列ビルダーメソッドを使用して列を作成しているため、ここでフォーマットを行う方法が正確にわかりません。telerik呼び出しのコンテキスト外では変数にアクセスできないため、合計をフォーマットできましたが、列の残りの部分はフォーマットできませんでしたitem
。基本的に、私の列作成ロジックは次のようになります。
telerikコードでは、
.Columns(a => GridHelper.GenerateColumns(a, Model.SelectedReport))
そして、列の生成は次のようになります。
public static void GenerateColumns(GridColumnFactory<dynamic> columnFactory, Company.Project.Data.Entity.Report reportStructure)
{
foreach (var columnLayout in reportStructure.ReportCols.OrderBy(o => o.ColumnSequence))
{
GridBoundColumnBuilder<dynamic> columnBuilder = columnFactory.Bound(columnLayout.ColumnType);
//do other stuff here (add aggregates, formatting, etc)
}
では、このコンテキストでフォーマットをどのように行うのでしょうか?