Example of Value Template in ASP.NET MVC Query Builder Control
This sample demonstrates the integration of the Dropdown List and Slider components as templates in the Query Builder control and also showing the different types of queries such as CEL and SpEL. The query preview can be changed using the tab component.
@Html.UiQueryBuilder(new UiQueryBuilderOptions
{
Id = "valueQb",
OnCreated = "QbValue.show",
OnRuleChange = "QbValue.show",
Columns = new List<UiQueryBuilderColumn>
{
new UiQueryBuilderColumn { Field = "Category", Label = "Category" },
new UiQueryBuilderColumn { Field = "PaymentMode", Label = "Payment Mode", Operators = new List<string> { "equal", "notequal" }, Template = "paymentTemplate" },
new UiQueryBuilderColumn { Field = "Amount", Label = "Amount", Type = UiQueryColumnType.Number, Operators = new List<string> { "greaterthanorequal", "lessthanorequal", "equal" }, Template = "amountTemplate" },
new UiQueryBuilderColumn { Field = "TransactionDate", Label = "Transaction Date", Type = UiQueryColumnType.Date }
},
Rule = (UiRuleGroup)ViewData["rule"]
})
<template id="paymentTemplate">
<select data-qb-value aria-label="Payment mode">
<option value="">Choose a mode</option>
<option>Cash</option>
<option>Credit Card</option>
<option>Debit Card</option>
<option>Net Banking</option>
</select>
</template>
<template id="amountTemplate">
<input type="range" min="0" max="10000" step="500" data-qb-value aria-label="Amount" />
<output data-qb-text="value"></output>
</template>
@Html.UiQueryBuilder(new UiQueryBuilderOptions
{
Id = "valueQb",
OnCreated = "QbValue.show",
OnRuleChange = "QbValue.show",
Columns = new List<UiQueryBuilderColumn>
{
new UiQueryBuilderColumn { Field = "Category", Label = "Category" },
new UiQueryBuilderColumn { Field = "PaymentMode", Label = "Payment Mode", Operators = new List<string> { "equal", "notequal" }, Template = "paymentTemplate" },
new UiQueryBuilderColumn { Field = "Amount", Label = "Amount", Type = UiQueryColumnType.Number, Operators = new List<string> { "greaterthanorequal", "lessthanorequal", "equal" }, Template = "amountTemplate" },
new UiQueryBuilderColumn { Field = "TransactionDate", Label = "Transaction Date", Type = UiQueryColumnType.Date }
},
Rule = (UiRuleGroup)ViewData["rule"]
})
<pre id="valueRules" class="small mt-3 p-2 border rounded" aria-live="polite" aria-label="The SQL"></pre>
<script>
window.QbValue = (function () {
'use strict';
/** The rules' SQL: the template's values are in the rule set like any other. */
function show() {
document.getElementById('valueRules').textContent = window.Lumen.get('valueQb').getSqlFromRules();
}
return { show: show };
})();
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class QueryBuilderController : Controller
{
// The page is Views/QueryBuilder/ValueTemplate.cshtml; it draws what ViewData holds.
public ActionResult ValueTemplate()
{
UiRuleGroup rule = new UiRuleGroup
{
Condition = "and",
Rules =
{
new UiRule { Field = "PaymentMode", Operator = "equal", Value = "Credit Card" },
new UiRule { Field = "Amount", Operator = "greaterthanorequal", Value = 2500 }
}
};
ViewData["rule"] = rule;
return View();
}
}
}
@* 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>
This sample illustrates how to integrate the Dropdown List, Slider, and Checkbox components with the Query Builder. The available types of templates are:
-
DropDownList -
AutoComplete -
CheckBox -
Slider
In this demo, queries are exported in CEL and SpEL formats.