Hi,
I am trying to implement pagination, filtering, searching using cloud firestore in ASP.NET Core ( DataGrid ) by using DataManagerRequest and UrlAdaptor. I know how to do that using SQL and entity framework, but it is different from this, because firestore works very differently. I hope you have a sample for that. Please help.
Thank you
Regards
Ahmed Mohammedali
|
public ActionResult UrlDatasource([FromBody]DataManagerRequest dm)
// in the dm variable you can get the skip and take values sent from client side
{
// You need to perform the paging in your api and return the data as result and count below
. . .
return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource);
}
|
Hi,
Thank you for your reply. Before answering your questions, I want to point out that I implemented a similar grid for the part of my data stored in MSSQL, and it works perfectly. So, I am trying to use the same concept here but I found that cloud firestore API is very different.
Here is my server-side code:
public IActionResult Index()
{
return View();
}
Here is the function being called via ajax , which I am trying to implement and complete:
public async Task<IActionResult> UrlDatasourceAsync([FromBody] DataManagerRequest dm)
{
CollectionReference collection = _firestore.Collection("students");
Query query;
if (dm.Search != null && dm.Search.Count > 0)
{
//DataSource = operation.PerformSearching(DataSource, dm.Search); //Search
}
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
//DataSource = operation.PerformSorting(DataSource, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
//DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
int count = (await collection.GetSnapshotAsync()).Count();
if (dm.Skip != 0)
{
//DataSource = operation.PerformSkip(DataSource, dm.Skip); //Paging
}
if (dm.Take != 0)
{
//DataSource = operation.PerformTake(DataSource, dm.Take);
}
query = collection.Limit(dm.Take == 0 ? 10 : dm.Take);
var list = new List<StudentViewModel>();
foreach (DocumentSnapshot doc in await query.GetSnapshotAsync())
{
list.Add(doc.ConvertTo<StudentViewModel>());
}
var ss = new JsonSerializerSettings();
ss.ContractResolver = new DefaultContractResolver();
return dm.RequiresCounts ? Json(new { result = list, count = count }, ss) : Json(list);
}
Here is the grid code:
<ejs-grid id="Grid" allowPaging="true" allowSorting="true" allowFiltering="true" toolbar="@(new List<string>() { "Search"})" load="actionComplete" created="gridCreated" dataBound="actionComplete" actionComplete="actionComplete" actionBegin="actionBegin">
<e-grid-filterSettings type="Excel"> </e-grid-filterSettings>
<e-grid-pagesettings pageSize="10" pageSizes="true"></e-grid-pagesettings>
<e-data-manager url="@Url.Action("UrlDatasource","RegisteredStudents")" adaptor="UrlAdaptor"></e-data-manager>
<e-grid-columns>
<e-grid-column field="Name" headerText="Name"></e-grid-column>
<e-grid-column field="AttendantCategry" headerText="Attendant Categry"></e-grid-column>
<e-grid-column field="StudentClassification" headerText="Student Classification"></e-grid-column>
<e-grid-column field="Major" headerText="Major" width="160"></e-grid-column>
<e-grid-column field="Phone" headerText="Phone"></e-grid-column>
<e-grid-column field="SignUpDate" headerText="Sign Up Date" customFormat="@(new {type = "datetime", format = "M/d/y hh:mm a" })"></e-grid-column>
@*<e-grid-column headerText="" template="#template"></e-grid-column>*@
</e-grid-columns>
</ejs-grid>
<script>
function gridCreated() {
var grid = document.getElementById("Grid").ej2_instances[0];
grid.hideSpinner = () => true;
ej.popups.setSpinner({ type: 'Bootstrap' });
$(".e-spinner-pane").css("display", "block");
}
function actionComplete() {
$(".e-spinner-pane").css("display", "none");
}
function actionBegin() {
$(".e-spinner-pane").css("display", "block");
}
document.getElementById('clear').addEventListener('click', () => {
var gridObj = document.getElementById("Grid").ej2_instances[0];
gridObj.searchSettings.key = '';
});
</script>
The steps that I followed are in this documentation:
Data Binding in ASP.NET Core Grid control - Syncfusion
I hope I answered your questions.
Thanks again for your help
Regards
I also want to mention that I am using the package Google.Cloud.Firestore. Here is the official link: Google.Cloud.Firestore | Google.Cloud.Firestore (googleapis.dev). I think in this case I cannot use IQueryable, right?
|
<div class="row">
<ejs-grid id="job_listing_grid" toolbar="@(new List<string>() {"Search" })" allowPaging="true" allowSorting="true" allowFiltering="true">
<e-data-manager url="/Home/UrlDataSource" adaptor="UrlAdaptor"></e-data-manager>
<e-grid-filterSettings type="Menu"></e-grid-filterSettings>
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>
<e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>
<e-grid-column field="Freight" width="150"></e-grid-column>
<e-grid-column field="IsClose" width="120" displayAsCheckBox="true" textAlign="Left"></e-grid-column>
</e-grid-columns>
</ejs-grid>
</div> |
|
@{
ViewData["Title"] = "Home Page";
}
<div class="row">
<ejs-grid id="job_listing_grid" toolbar="@(new List<string>() {"Search" })" allowPaging="true" dataStateChange="dataStateChange" created="created" allowSorting="true" allowFiltering="true">
<e-grid-filterSettings type="Menu"></e-grid-filterSettings>
<e-grid-columns>
. . .
</e-grid-columns>
</ejs-grid>
</div>
<script>
var grid;
function getData(gridquery) {
. . .
// based on the grid query we have form the url and get the result you can generate query based on your service
var ajax = new ej.base.Ajax(
. . . );
ajax.send();
ajax.onSuccess = data => {
if (grid.element !== undefined) {
var final = JSON.parse(data);
// finally return the value as result(JSON Object) and count(total count) pair
grid.dataSource = { result: final.value, count: final.count };
}
};
}
function dataStateChange(state) {
// get grid queries from the Grid action
getData(state); // trigger for every grid action (page, sort, ect.,)
}
function created() {
grid = this;
var state = { skip: 0, take: 10 };
getData(state); // for initial grid settings
}
</script> |
Hi,
Thank you for your reply. I am familiar with what you explained above. I hope you help me with pagination with firestore. Specifically, what the equivalent of these lines will be:
if (dm.Skip != 0)
{
DataSource = operation.PerformSkip(DataSource, dm.Skip); //Paging
}
if (dm.Take != 0)
{
DataSource = operation.PerformTake(DataSource, dm.Take);
}
As this works for MSSQL and EntityFramework without problems, it does not work with firestore. This is the problem here.
Thank you