Grid using Adapater.Url and Page.OnParametersSetAsync for Query causes random or unwanted behavior

When a grid is set up to use the data adapter's url, a GET is fired as soon as the page initializes.  This is OK in most situations, however, in many of our situations we have to inject a parameter into the url using the query object.  It is easy enough to add the parameter, but the problem lies when the parameter is only known during the page's OnParametersSetAsync event.  

So, you end up with a situation like this:

  1. Page initialization: grid adapter calls GET (url with no parameters)
  2. OnParametersSetAsnc: Query parameter added: grid adapter calls GET (url with known parameter)

Sometimes the initial GET returns AFTER the 2nd one, yielding an empty result set back to the grid after data binding takes place.

What we need, is the ability to only get AFTER the parameter was added to the query.  How can this be achieved?



3 Replies

MS Monisha Saravanan Syncfusion Team June 13, 2022 11:40 AM UTC

Hi Jeff,


Greetings from Syncfusion support.


Query: “What we need, is the ability to only get AFTER the parameter was added to the query.  How can this be achieved?”


We have checked your query and we suggest you to use AddParams  method of query to send additional parameters to the server. We have already documented this topic. Kindly refer the attached UG for your reference.


Reference: https://blazor.syncfusion.com/documentation/datagrid/data-binding#sending-additional-parameters-to-the-server


If we misunderstood your query or if you have further queries then kindly share the below details to validate further at our end.


  1. Share us entire grid code snippet along with model class.
  2. Share us some more details regarding your exact requirement.
  3. Share us the video demonstration explaining  the issue.
  4. If possible share us an simple issue reproduceable sample.


The above-requested details will be very helpful for us to validate the reported query at our end and provide the solution as early as possible.


Regards,

Monisha



JV Jeff Voigt June 13, 2022 12:18 PM UTC

Please read the original post.  We are adding the parameter already.  Here is the code:


<SfGrid @ref="Grid" TValue="ModelType" AllowPaging="true"
                Query="QueryCustomizations" ID="ViewRows">
<SfDataManager Adaptor="Adaptors.ODataV4Adaptor" Url="customview/query" />
<GridPageSettings PageCount="10" PageSize="12" PageSizes="true" />
<GridSelectionSettings PersistSelection="true" />
<GridColumns>
<GridColumn Field="@nameof(ModelType.Id)" IsPrimaryKey="true" Visible="false"></GridColumn>
</GridColumns>
</SfGrid>


Query QueryCustomizations = new Query().Sort("AccountNumber", null);

protected override async Task OnParametersSetAsync()
{
QueryCustomizations.AddParams("ViewId", ViewId);
await Grid.Refresh();
}


The problem is you end up with 2 calls to the backend url.  One without the ViewId (prior to the OnParametersSetAsync method being fired) and one with the ViewId as a parameter (after the OnParametersSetAsync method).  The result from the first call will always be an empty set (and should not be called in our case) while the 2nd call will yield the correct result set.  We need our application to hold off calling anything to the backend URL until we've specified the additional parameters from the OnParametersSetAsync method.






MS Monisha Saravanan Syncfusion Team June 14, 2022 01:36 PM UTC

Hi Jeff,


Greetings from Syncfusion support.


We have checked your query and we suggest you to use the below way to add params in the query property. If we create a new query and assign it to the existing query property it will refresh the Grid based on the new query. Kindly refer the attached code snippet for your reference.


Also kindly remove the refresh method because if we call Grid.Refresh inside OnParamaterSetAsync then it will refresh the Grid multiple times.


 

<SfGrid TValue="Order" @ref="GridObj" AllowPaging="true" AllowSorting="true" Query="@QueryData">

    <GridPageSettings PageSize="10"></GridPageSettings>

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

    </SfDataManager>

    <GridColumns>

        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>

    </GridColumns>

</SfGrid>

 

@code{

    public SfGrid<Order> GridObj;

 

    public Query QueryData = new Query().Sort("Freight", "Descending");

    private Query UpdatedQueryData;

 

    

   protected override async Task OnParametersSetAsync()

    {    

         UpdatedQueryData = new Query().Sort("Freight", "Descending");

         UpdatedQueryData.AddParams("viewid", "check");

         QueryData = UpdatedQueryData.AddParams("CustomerName", "Check");

    }

 

}


Kindly get back to us if you have further queries.


Regards,

Monisha


Loader.
Up arrow icon