We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Grid Grouping to aggregate data with a CaptionTemplateContext

Hello,

I am attempting to aggregate and customize data with a caption template when a user groups data on a data table. See attached project for an example. 

In this project this table is grouped by CustomerID then by OrderDate. I am calling a function to aggregate and format the caption of the grouping how I would like. However I do not have all the information I need to make the appropriate query on the underlying data. For example at the second level of grouping for the first customer "ALFKI" the caption template only has the date information for the grouping so I can sum all of the freight totals for that day, but not for that day and that customer because I cant get the which customer this caption is referring to. How can I achieve this?

@{
    var Template = (new string[] { "CustomerID", "OrderDate" });
}
<SfGrid DataSource="@Orders" AllowGrouping="true">
    <GridColumns>
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn>
        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn>
        <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn>
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>
    </GridColumns>
    <GridGroupSettings Columns="Template">
        <CaptionTemplate>
            @{
                var order = (context as CaptionTemplateContext);
                <div>@GetCustomCaptionTemplate(order)</div>
            }
        </CaptionTemplate>
    </GridGroupSettings>
</SfGrid>


@code{
    public List<Order> Orders { get; set; }


    protected override void OnInitialized()
    {
        Orders = Enumerable.Range(1, 75).Select(x => new Order()
        {
            OrderID = 1000 + x,
            CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],
            Freight = 2.1,
            //OrderDate = DateTime.Now.AddDays(-x),
            OrderDate = x <= 30 ? DateTime.Now : DateTime.Now.AddDays(-1)
        }).ToList();
    }


    private string GetCustomCaptionTemplate(CaptionTemplateContext captionTemplateContext)
    {
        var str = "";
        if (captionTemplateContext.Field == "OrderDate")
        {
            var sum = Orders.Where(x => x.OrderDate?.ToString("M/dd/yyyy") == captionTemplateContext.Key).Sum(x => x.Freight); //need to get customer ID so that I can just query this grouped customer
            str = $"Sum for day {captionTemplateContext.Key}: {sum}";
        }
        if (captionTemplateContext.Field == "CustomerID")
        {
            var sum = Orders.Where(x => x.CustomerID.ToString() == captionTemplateContext.Key).Sum(x => x.Freight);
            str = $"{captionTemplateContext.Key} Sum: {sum}";
        }
        return str;
    }
    public class Order
    {
        public int OrderID { get; set; }
        public string CustomerID { get; set; }
        public DateTime? OrderDate { get; set; }
        public double? Freight { get; set; }
    }




Attachment: GridCaptionCustomEx_5753913c.zip

9 Replies

SP Sarveswaran Palani Syncfusion Team September 26, 2022 03:30 AM UTC

Hi Leland,

Greetings from Syncfusion support.

We are currently Validating the reported query at our end, and we will update the further details within two business days. Until then we appreciate your patience.

Regards,

Sarveswaran PK



SP Sarveswaran Palani Syncfusion Team September 28, 2022 05:55 PM UTC

Hi Leland,


Sorry for the delay and Inconvenience caused.


As per current architecture, When grouping we use GroupCaptionTemplate to display the customized group template values. Based on that we only pass the parameters of specific grouped column, value of the grouped column and level of the columns. So that you didn’t able to get the previous level value. Kindly share the necessity of the previous values and share the exact requirement to us, it’ll be more useful for us to further evaluate the query ASAP.


Please get back to us if you have any further queries.


Regards,

Sarveswaran PK



LE Leland September 29, 2022 03:30 PM UTC

Thank you for the explanation. If this is the intended architecture that is fine. Since it seems that the captionTemplates are always iterated through top down, I am able to work around this by keeping track of the necessary information from the higher level CaptionTemplateContext in a local variable to make the appropriate query, it just makes the program less flexible and dynamic. 


Thank you,



SP Sarveswaran Palani Syncfusion Team October 3, 2022 01:32 AM UTC

Hi Leland,


Thanks for an update.


We have analyzed your query and by default we didn’t have support for performing calculation. We suspect that you may be achieved by some work around. If you’re facing difficulties on that. Please get back to us


Regards,

Sarveswaran PK



LE Leland October 8, 2022 05:32 PM UTC

Hello, in the attached project I have a grid that groups its data by Date > TimeBlock > ProductCatagory and then displays that items ID. The custom captionTemplates display all of the IDs for that grouping and its children. I achive this by storing local variables that hold the last Date/TimeBlock. I can then run linq queries against the data source to get the ids and format the string as needed. On initial population/adding/modifying data this works as intended as the grid seems to render the captions top down and the local date/time variables are kept in sync. 

However this workaround breaks when the user clicks on certain Captions in the grid since that click only seems to re-render that one caption. To see the issue run the attached project and click first on the 9/1/2022 | 745 AM | {productCatagory} caption. Since the last stored time variable from the initial render is now 8:45, the caption and linq queries are not grabbing the intended data. 


I understand that this issue is occurring from my workaround and you have stated that you do not support this type of behavior in caption template grouping. So my questions are:

1) Is there a way to not have the template not re-render when the user clicks on the grid caption? This would suffice for my needs currently but would be lacking in functionality for broader projects. 

2) Is there another feature/functionally of a grid to achieve this grouped custom caption in a more dynamic way through other means besides the captionTemplate?


-On inital render



-after initial click on this caption. Local variables are not in sync since the grid did not re render from top down. 



Attachment: DataGridCaptionTemplate_5349aad7.zip


SP Sarveswaran Palani Syncfusion Team October 10, 2022 06:02 PM UTC

Hi Leland,

Thanks for contacting Syncfusion support.


We have checked your query and We suggest you to set StopPropagation as true on CaptionTemplate to prevent losing focus. Kindly refer the attached code snippet for your reference.



<SfGrid DataSource="@Products" AllowGrouping="true" AllowPaging="true">

    <GridGroupSettings Columns=@InitialGrouping>

        <CaptionTemplate>

            @{

                var group = (context as CaptionTemplateContext);

                <div @onclick:stopPropagation="true" >

                    @GetCaption(group)

                </div>

            }

        </CaptionTemplate>

    </GridGroupSettings>


Kindly get back to us if you have further queries.


Regards,

Sarveswaran PK



LE Leland October 12, 2022 05:21 PM UTC

Hello,

Thank you for the suggestion, this addresses that specific issue for onclick but the issue remains somewhere else is clicked in the application. I have tried the same for all of the selection/focus propagations and events with no effect. 


I think regardless this solution will not work. Before in this thread I mentioned that it seemed that the captions update top down when the underlying data changes. This does not seem to be accurate when some nested grouping fields are collapsed. Is there any way in the caption template to get any more info about the caption that is being updated? If we are not able to access the parent grouping objects then are we at all able to access the current caption text before we generate the new custom caption? If I am able to gather almost any other information on which caption is being updated then I will be able to make a solution for this, otherwise I will need to find another tool/product that will work. 


Thank you,



SP Sarveswaran Palani Syncfusion Team October 14, 2022 03:20 AM UTC

Hi Leland,

Thanks for contacting Syncfusion support.

We are currently Validating the reported query at our end, and check possibilities with our dev team. we will update the further details within two business days. Until then we appreciate your patience.

Regards,

Sarveswaran PK



SP Sarveswaran Palani Syncfusion Team November 1, 2022 05:45 PM UTC

Hi Leland,


Sorry for the inconvenience caused.


We would like to inform you that, we didn’t have support for nested grid details in CaptionTemplateContext. Kindly get back to us, if you have any queries.


Regards,

Sarveswaran PK



Loader.
Live Chat Icon For mobile
Up arrow icon