Hi.
I am trying to bind the grid to the results of a webAPI call. This is a blazor app.
Grid definition looks like this.
<SfGrid TValue="Organization" class="mr-2"
AllowPaging="true" AllowFiltering="false" AllowSorting="true" AllowTextWrap="true" AllowResizing="true"
AllowGrouping="false" RowHeight="30"
Toolbar="@(new List<string>() {"Add", "Edit", "Delete", "Update", "Cancel" })">
<SfDataManager Url="api/organization" Adaptor="Syncfusion.Blazor.Adaptors.WebApiAdaptor" />
<GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true"
AllowEditOnDblClick="false" Mode="EditMode.Normal" AllowNextRowEdit="false"
ShowConfirmDialog="true" ShowDeleteConfirmDialog="true" />
<GridPageSettings PageSize="40"></GridPageSettings>
<GridColumns>
<GridColumn Field=@nameof(Organization.Orgsan) HeaderText="ID" IsPrimaryKey="true" IsIdentity="true" LockColumn="true" AllowAdding="false" TextAlign="TextAlign.Left" MinWidth="10" Width="10" />
<GridColumn Field=@nameof(Organization.OrgName) HeaderText="Name" IsPrimaryKey="false" LockColumn="false" AllowAdding="true" TextAlign="TextAlign.Left" MinWidth="10" Width="35" />
@*<GridColumn Width="10">
<Template>
@{
var frmfth = (context as Organization);
<SfButton CssClass="e-info e-small" @onclick="@((args) => onToggleClick_Resolved(args,frmfth))" IsToggle="true" IsPrimary="true">Detail</SfButton>
}
</Template>
</GridColumn>*@
</GridColumns>
</SfGrid>
The grid is using sfdata manager.
The HTTP controller looks like this.
// GET: api/<ValuesController>
[HttpGet]
public object GetAll()
{
IQueryable<Organization> data = _orgRepo.GetAll().AsQueryable();
var count = data.Count();
var queryString = Request.Query;
if (queryString.Keys.Contains("$inlinecount"))
{
StringValues Skip;
StringValues Take;
int skip = (queryString.TryGetValue("$skip", out Skip)) ? Convert.ToInt32(Skip[0]) : 0;
int top = (queryString.TryGetValue("$top", out Take)) ? Convert.ToInt32(Take[0]) : data.Count();
return new { Items = data.Skip(skip).Take(top), Count = count };
}
else
{
return data;
}
}
Where I think the problem lies is in this line of code. I do not think the URL is being called correctly. How does it now to add this https://localhost:44318/ to the URL. I am wondering if the issue is that the API is not being call at the correct port?
<SfDataManager Url="api/organization" Adaptor="Syncfusion.Blazor.Adaptors.WebApiAdaptor" />
It seems that the grid control and API reference are not communicating.
When I test the API independently through a CURL call, it is returning a result set.
As always, your help is greatly appreciated.
Marc.
Your example works great. Couple of follow up questions.
#1 - You define the grid as follows:
<SfGrid @ref="Grid" TValue="Organization" AllowFiltering="true" AllowSorting="true" AllowPaging="true"
Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
.....
@code{
SfGrid<Organization> Grid { get; set; }
}
What is the purpose of the @ref? Organizations is already covered in the TValue so is this really required?
#2 - WebAPI in Separate Project
The sample project was very helpful. However, it had the web page and API in the same project. I have a solution that has the blazor server app and webapi in different projects.
In the below code example, I need to specify the full path of the API project with port number. Just referencing the API does not work. Is it possible to get this URL property from a variable or set it in code?
@*<SfDataManager Url="https://localhost:44318/api/Organization" Adaptor="Adaptors.WebApiAdaptor"></SfDataManager>*@
<SfDataManager Url="api/Organization" Adaptor="Adaptors.WebApiAdaptor"></SfDataManager>
|
<SfGrid ID="Grid" TValue="Orders" @ref="Grid" AllowExcelExport="true"
Toolbar="@(new List<string>() { "ExcelExport"})" Height="315" Width="900">
<GridEvents OnToolbarClick="ToolbarClick" TValue="Orders"></GridEvents>
<SfDataManager Url=https://localhost:44395/api/Default Adaptor="Adaptors.WebApiAdaptor"></SfDataManager>
<GridColumns>
<GridColumn Field=@nameof(Orders.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field=@nameof(Orders.OrderDate) HeaderText=" Order Date" Format="d" TextAlign="TextAlign.Right" Width="130" Type="ColumnType.Date"></GridColumn>
<GridColumn Field=@nameof(Orders.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>
</GridColumns>
</SfGrid> |