Get Total Count from Odata4adaptor

Hi Team Syncfusion,

I am using an OData4Adaptor for the SfGrid and I am wondering how I get the total count of rows from the REST query made.

Thank you for any assistance you can provide.


5 Replies 1 reply marked as answer

PS Prathap Senthil Syncfusion Team May 6, 2024 11:53 AM UTC


Hi Mike-E,


Based on your requirement, we suggest that to get the total count using the OData v4 link, you can make an HTTP request to the '$count' endpoint and retrieve the count value. Kindly refer to the code snippet and sample below for reference.


 

@using Syncfusion.Blazor

@using Syncfusion.Blazor.Data

@using Syncfusion.Blazor.Grids

@inject HttpClient Client;

 

<SfGrid TValue="EmployeeData" ID="Grid" AllowPaging="true">

    <SfDataManager Url=https://services.odata.org/V4/Northwind/Northwind.svc/Orders/ Adaptor="Adaptors.ODataV4Adaptor"></SfDataManager>

    <GridColumns>

        <GridColumn Field=@nameof(EmployeeData.OrderID) TextAlign="TextAlign.Center" HeaderText="Order ID" Width="120"></GridColumn>

        <GridColumn Field=@nameof(EmployeeData.CustomerID) TextAlign="TextAlign.Center" HeaderText="Customer Name" Width="130"></GridColumn>

        <GridColumn Field=@nameof(EmployeeData.EmployeeID) TextAlign="TextAlign.Center" HeaderText="Employee ID" Width="120"></GridColumn>

    </GridColumns>

</SfGrid>

<p>@totalCount</p>

@code {

 

    protected override async Task OnInitializedAsync()

    {

 

        var response = await Client.GetAsync(https://services.odata.org/V4/Northwind/Northwind.svc/Orders/$count);

        if (response.IsSuccessStatusCode)

        {

            var content = await response.Content.ReadAsStringAsync();

            totalCount = int.Parse(content);

        }

 

    }

}


Please get back to us if you face any difficulties with reproducing the issue with a simple sample.


Sample: https://blazorplayground.syncfusion.com/embed/LZBfDoNdKRFhoDaQ?appbar=true&editor=true&result=true&errorlist=true&theme=bootstrap5

Regards,
Prathap Senthil



MI Mike-E May 6, 2024 12:44 PM UTC

Thank you for the reply Prathap!  It is appreciated.  Please pardon the confusion.  I am aware that I can make a call directly via HTTP.  The SfDataManager has already made this call, correct?  I would rather make one call to the API via SfDataManager and leverage the results that are already in memory rather than make another call (making it two: SfDataManager + HttpClient), does that make sense?

Thank you for any assistance you can provide.



PS Prathap Senthil Syncfusion Team May 7, 2024 01:23 PM UTC

Based on your concern, we suggest using the grid event's OnDataBound argument to access the count property for retrieving the total count of records. Please refer to the code snippet and sample below for your reference

 

<SfGrid TValue="EmployeeData"  ID="Grid" AllowPaging="true">

    <GridEvents OnDataBound="DataBoundHandler" TValue="EmployeeData"></GridEvents>

 

    <SfDataManager  Url=https://services.odata.org/V4/Northwind/Northwind.svc/Orders/ Adaptor="Syncfusion.Blazor.Adaptors.ODataV4Adaptor"></SfDataManager>

    <GridColumns>

        -----------

    </GridColumns>

</SfGrid>

<p>Count:@totalCount</p>

@code {

 

    public int totalCount { get; set; }

 

 

    public class EmployeeData

    {

        public int OrderID { get; set; }

        public string CustomerID { get; set; }

        public int EmployeeID { get; set; }

    }

    public void DataBoundHandler(BeforeDataBoundArgs<EmployeeData> args)

    {

        totalCount = args.Count;

    }

   

}



Sample: https://blazorplayground.syncfusion.com/embed/hXhzXoZQCPDcgFFP?appbar=true&editor=true&result=true&errorlist=true&theme=bootstrap5


Marked as answer

MI Mike-E May 7, 2024 01:54 PM UTC

AWESOME!  Thank you so much Prathap for that guidance, it is appreciated.



PS Prathap Senthil Syncfusion Team May 8, 2024 12:10 PM UTC

Thanks for the update,


We are happy to hear that the provided solution was helpful. We are closing the thread now.



Loader.
Up arrow icon