Example of Rule Template in ASP.NET MVC Query Builder Control
This sample demonstrates the Rule Template functionality in QueryBuilder component using RadioButton components. In the Country column, user can change the Operator as equal/not equal using RadioButton component and select the Value from DropDownList component.
@Html.UiQueryBuilder(new UiQueryBuilderOptions
{
Id = "ruleQb",
ShowButtons = new UiQueryShowButtons { RuleDelete = false },
OnCreated = "QbRuleTemplate.show",
OnRuleChange = "QbRuleTemplate.show",
Columns = new List<UiQueryBuilderColumn>
{
new UiQueryBuilderColumn { Field = "City", Label = "City" },
new UiQueryBuilderColumn
{
Field = "Temperature", Label = "Temperature", Type = UiQueryColumnType.Number,
Operators = new List<string> { "greaterthan", "lessthan", "equal" }, RuleTemplate = "temperatureRule"
},
new UiQueryBuilderColumn { Field = "Sunny", Label = "Sunny", Type = UiQueryColumnType.Boolean }
},
Rule = (UiRuleGroup)ViewData["rule"]
})
<template id="temperatureRule">
<span class="qb-rule-card">
<strong data-qb-text="label"></strong>
<select data-qb-operator aria-label="Operator"></select>
<input type="number" min="-40" max="60" data-qb-value aria-label="Temperature in degrees" style="width:6em" />
<span aria-hidden="true">°C</span>
<button type="button" class="btn btn-sm btn-link" data-qb-action="delete">Remove</button>
</span>
</template>
@Html.UiQueryBuilder(new UiQueryBuilderOptions
{
Id = "ruleQb",
ShowButtons = new UiQueryShowButtons { RuleDelete = false },
OnCreated = "QbRuleTemplate.show",
OnRuleChange = "QbRuleTemplate.show",
Columns = new List<UiQueryBuilderColumn>
{
new UiQueryBuilderColumn { Field = "City", Label = "City" },
new UiQueryBuilderColumn
{
Field = "Temperature", Label = "Temperature", Type = UiQueryColumnType.Number,
Operators = new List<string> { "greaterthan", "lessthan", "equal" }, RuleTemplate = "temperatureRule"
},
new UiQueryBuilderColumn { Field = "Sunny", Label = "Sunny", Type = UiQueryColumnType.Boolean }
},
Rule = (UiRuleGroup)ViewData["rule"]
})
<pre id="ruleTemplateSql" class="small mt-3 p-2 border rounded" aria-live="polite" aria-label="The SQL"></pre>
<script>
window.QbRuleTemplate = (function () {
'use strict';
function show() {
document.getElementById('ruleTemplateSql').textContent = window.Lumen.get('ruleQb').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/RuleTemplate.cshtml; it draws what ViewData holds.
public ActionResult RuleTemplate()
{
UiRuleGroup rule = new UiRuleGroup
{
Condition = "and",
Rules =
{
new UiRule { Field = "Temperature", Operator = "greaterthan", Value = 25 },
new UiRule { Field = "City", Operator = "startswith", Value = "San" }
}
};
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 ruleTemplate to the columns in the QueryBuilder. This is used for creating custom user interface for the columns with custom components and update the rule collection by using the component events.