Example of Integration With Data Grid in ASP.NET MVC Query Builder Control
This sample demonstrates the integration of the Data Grid component to showcase data population based on the created filters using the Query Builder component.
@Html.UiQueryBuilder(new UiQueryBuilderOptions
{
Id = "gridQb",
Columns = new List<UiQueryBuilderColumn>
{
new UiQueryBuilderColumn { Field = "EmployeeId", Label = "Employee ID", Type = UiQueryColumnType.Number },
new UiQueryBuilderColumn { Field = "FirstName", Label = "First Name" },
new UiQueryBuilderColumn { Field = "Title", Label = "Title" },
new UiQueryBuilderColumn { Field = "Country", Label = "Country", Values = new List<object> { "USA", "UK", "Ireland" } },
new UiQueryBuilderColumn { Field = "HireDate", Label = "Hire Date", Type = UiQueryColumnType.Date },
new UiQueryBuilderColumn { Field = "Salary", Label = "Salary", Type = UiQueryColumnType.Number },
new UiQueryBuilderColumn { Field = "IsActive", Label = "Active", Type = UiQueryColumnType.Boolean }
},
Rule = (UiRuleGroup)ViewData["rule"]
})
@Html.UiGrid(new UiGridOptions
{
Id = "qbGrid",
DataUrl = Url.Action("Filter", "QueryBuilderDemo"),
AllowPaging = true,
PageSize = 5,
AllowFiltering = true,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "EmployeeId", HeaderText = "ID", Type = UiColumnType.Number, IsPrimaryKey = true, Width = 80 },
new UiGridColumn { Field = "FirstName", HeaderText = "First Name", Width = 120 },
new UiGridColumn { Field = "LastName", HeaderText = "Last Name", Width = 120 },
new UiGridColumn { Field = "Title", Width = 190 },
new UiGridColumn { Field = "Country", Width = 100 },
new UiGridColumn { Field = "HireDate", HeaderText = "Hire Date", Type = UiColumnType.Date, Format = "MM/dd/yyyy", Width = 120 },
new UiGridColumn { Field = "Salary", Type = UiColumnType.Number, Format = "c0", TextAlign = UiTextAlign.Right, Width = 110 },
new UiGridColumn { Field = "IsActive", HeaderText = "Active", Type = UiColumnType.Boolean, Width = 90 }
}
})
@Html.UiQueryBuilder(new UiQueryBuilderOptions
{
Id = "gridQb",
Columns = new List<UiQueryBuilderColumn>
{
new UiQueryBuilderColumn { Field = "EmployeeId", Label = "Employee ID", Type = UiQueryColumnType.Number },
new UiQueryBuilderColumn { Field = "FirstName", Label = "First Name" },
new UiQueryBuilderColumn { Field = "Title", Label = "Title" },
new UiQueryBuilderColumn { Field = "Country", Label = "Country", Values = new List<object> { "USA", "UK", "Ireland" } },
new UiQueryBuilderColumn { Field = "HireDate", Label = "Hire Date", Type = UiQueryColumnType.Date },
new UiQueryBuilderColumn { Field = "Salary", Label = "Salary", Type = UiQueryColumnType.Number },
new UiQueryBuilderColumn { Field = "IsActive", Label = "Active", Type = UiQueryColumnType.Boolean }
},
Rule = (UiRuleGroup)ViewData["rule"]
})
<div class="d-flex gap-2 my-3">
<button type="button" class="btn btn-sm btn-primary" id="gridQbFilter">Filter</button>
<button type="button" class="btn btn-sm btn-outline-secondary" id="gridQbClear">Clear</button>
</div>
@Html.UiGrid(new UiGridOptions
{
Id = "qbGrid",
DataUrl = Url.Action("Filter", "QueryBuilderDemo"),
AllowPaging = true,
PageSize = 5,
AllowFiltering = true,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "EmployeeId", HeaderText = "ID", Type = UiColumnType.Number, IsPrimaryKey = true, Width = 80 },
new UiGridColumn { Field = "FirstName", HeaderText = "First Name", Width = 120 },
new UiGridColumn { Field = "LastName", HeaderText = "Last Name", Width = 120 },
new UiGridColumn { Field = "Title", Width = 190 },
new UiGridColumn { Field = "Country", Width = 100 },
new UiGridColumn { Field = "HireDate", HeaderText = "Hire Date", Type = UiColumnType.Date, Format = "MM/dd/yyyy", Width = 120 },
new UiGridColumn { Field = "Salary", Type = UiColumnType.Number, Format = "c0", TextAlign = UiTextAlign.Right, Width = 110 },
new UiGridColumn { Field = "IsActive", HeaderText = "Active", Type = UiColumnType.Boolean, Width = 90 }
}
})
<script>
(function () {
'use strict';
function qb() { return window.Lumen.get('gridQb'); }
function grid() { return window.Lumen.get('qbGrid'); }
/** The complete rules go with the grid's request; the server filters with them (QueryBuilderDemoController.Filter). */
function filter() {
grid().reload({ rule: JSON.stringify(qb().getValidRules()) });
}
document.getElementById('gridQbFilter').addEventListener('click', filter);
document.getElementById('gridQbClear').addEventListener('click', function () {
qb().reset();
grid().reload({});
});
})();
</script>
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace YourApp.Controllers
{
public class QueryBuilderController : Controller
{
// The page is Views/QueryBuilder/IntegrationWithDataGrid.cshtml; it draws what ViewData holds.
public ActionResult IntegrationWithDataGrid()
{
UiRuleGroup rule = new UiRuleGroup
{
Condition = "and",
Rules = { new UiRule { Field = "Country", Operator = "equal", Value = "UK" } }
};
ViewData["rule"] = rule;
return View();
}
private static readonly JsonSerializerSettings Answers = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
DateFormatString = "yyyy-MM-ddTHH:mm:ss"
};
private static readonly JsonSerializerSettings Requests = new JsonSerializerSettings
{
DateParseHandling = DateParseHandling.None,
MaxDepth = 256
};
/// <summary>
/// The DataGrid of Integration With Data Grid: its request, with the Query Builder's rule set (JSON) as the parameter "rule",
/// filtered by the rules as the grid's own filter (ToGridFilter), then sorted and paged as usual.
/// </summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult Filter(UiDataRequest request, string rule)
{
request = request ?? new UiDataRequest();
if (!string.IsNullOrWhiteSpace(rule))
{
try
{
UiRuleGroup rules = JsonConvert.DeserializeObject<UiRuleGroup>(rule, Requests);
request.Where.AddRange(UiQueryTranslator.ToGridFilter(rules, QueryEmployees.Columns()));
}
catch (Exception ex) when (ex is JsonException || ex is UiQueryException)
{
Response.StatusCode = 400;
return Answer(new { error = ex.Message });
}
}
return Json(UiDataOperations.Execute(QueryEmployees.All.AsQueryable(), request, "EmployeeId"));
}
private ActionResult Answer(object value)
{
return Content(JsonConvert.SerializeObject(value, Answers), "application/json");
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
@* Query Builders with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
This sample illustrates how to integrate the Data Grid component with the Query Builder. The Data Grid component will be refreshed while editing the filters in Query Builder.