Custom data in subtotal cell

Hello, 

Is it possible to have a custom value displayed in a PivotViewValue summary cell that is not mathematically based ? 

For example if I had the following: 



<SfPivotView TValue="TeamMember" Height="900">
    <PivotViewDataSourceSettings DataSource="@data" ExpandAll="true">
        <PivotViewRows>
            <PivotViewRow Name="Department"></PivotViewRow>
            <PivotViewRow Name="Name"></PivotViewRow>
        </PivotViewRows>
        <PivotViewValues>
            <PivotViewValue Name="Age" Caption="Age"></PivotViewValue>
        </PivotViewValues>
    </PivotViewDataSourceSettings>
</SfPivotView>
@code {
    private List<TeamMember> data = new List<TeamMember>();


    public class TeamMember
    {
        public string Department { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
    protected override void OnInitialized()
    {
        var member1 = new TeamMember() { Department = "Sales", Name = "Johny", Age = 22 };
        var member2 = new TeamMember() { Department = "Sales", Name = "Sarah", Age = 30 };
        data.Add(member1);
        data.Add(member2);
    }
}

In the PivotViewValue for "Age" summary for the group can I have a custom value that would list the ages like "22, 30" instead of any sum, avg, min, max, ect, listed in the aggregation docs below?

https://blazor.syncfusion.com/documentation/pivot-table/aggregation


Thank you,


3 Replies 1 reply marked as answer

AP AngelinFaithSheeba PaulvannanRajadurai Syncfusion Team August 15, 2022 01:13 PM UTC

Hi Leland,


Using CellTemplate option, you can customize the pivot cell values in the pivot table. Please refer the below code example.


Code Example:

<PivotViewTemplates>

        <CellTemplate>

            @{

                var data = (context as AxisSet);

                if (data != null)

                {

                    if (data.Axis == "value" )

                    {

                        if (data.Axis == "value" && data.RowHeaders=="Sales"){

                            @data.FormattedText.Replace("52","20,32");

                        }

                        else

                        {

                            @data.FormattedText

                        }                      

                    }

                 else

                 {

                     @data.FormattedText

                 }

                }

            }

        </CellTemplate>

    </PivotViewTemplates>


You can also customize the pivot table values by using EnginePopulated event. Please refer the below code example and screenshot.


Code Example:

private void enginePopulated(EnginePopulatedEventArgs args)

    {

        for (var i = 0; i < args.PivotValues.Count(); i++)

        {

            for (var j = 0; (args.PivotValues[i] != null && j < args.PivotValues[i].Count()); j++)

            {

                if (args.PivotValues[i][0] != null && args.PivotValues[i][j] != null && args.PivotValues[i][0].Axis == "row")

                {

                    if (args.PivotValues[i][j].FormattedText == "52" && args.PivotValues[i][j].RowHeaders=="Sales")

                    {

                        args.PivotValues[i][j].FormattedText = "20,32";

                    }

                }

            }

        }

    }


Screenshot:


Meanwhile, we have prepared a sample for your reference.


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/PivotTable1418282398


Please refer the below UG document to know more about CellTemplate and EnginePopulated event property.


Document: https://blazor.syncfusion.com/documentation/pivot-table/row-and-column#cell-template

https://blazor.syncfusion.com/documentation/pivot-table/events#enginepopulated


Please let us know if you have any concerns.


Regards,

Angelin Faith Sheeba.


Marked as answer

LE Leland August 15, 2022 02:08 PM UTC

Thank you very much for the quick reply. This appears to be what I needed. I look into this more.


Thank you,



AP AngelinFaithSheeba PaulvannanRajadurai Syncfusion Team August 16, 2022 05:05 AM UTC

Hi Leland,


Please let us know if you have any other queries. We are always happy to assist you.


Regards,

Angelin Faith Sheeba.


Loader.
Up arrow icon