This is my implementation:
QueryBuilderSettings.cs class
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
[Serializable]
public class QueryBuilderSettings {
public List<QueryBuilderFilter> filters { get; set; }
public List<string> plugins { get; set; }
public QueryBuilderSettings()
{
this.filters = new List<QueryBuilderFilter>();
this.plugins = new List<string>();
}
}
public class QueryBuilderFilter
{
public string id { get; set; }
public string label { get; set; }
public string type { get; set; }
public List<string> operators { get; set; }
public string input { get; set; }
//public List<object> values { get; set; }
public Dictionary<string, string> values { get; set; }
public QueryBuilderFilter()
{
}
public QueryBuilderFilter(string id, string label, QueryBuilderDataType type, List<QueryBuilderFilterOperators> ops, QueryBuilderInputType input, Dictionary<string, string> values)
{
this.id = id;
this.label = label;
this.type = type.ToString();
this.operators = new List<string>();
foreach (QueryBuilderFilterOperators op in ops)
{
this.operators.Add(op.ToString());
}
this.input = input.ToString();
this.values = values;
}
public static QueryBuilderDataType GetQueryBuilderDataType(string PropertyInputType)
{
QueryBuilderDataType QBFilterType;
switch(PropertyInputType)
{
case "bool":
QBFilterType = QueryBuilderDataType.boolean;
break;
case "DateTime":
QBFilterType = QueryBuilderDataType.datetime;
break;
case "Date":
QBFilterType = QueryBuilderDataType.date;
break;
case "Time":
QBFilterType = QueryBuilderDataType.time;
break;
case "double":
QBFilterType = QueryBuilderDataType.@double;
break;
case "int":
QBFilterType = QueryBuilderDataType.integer;
break;
case "enum_dropdown":
case "dropdown":
case "html":
case "stringChar":
case "stringLine":
QBFilterType = QueryBuilderDataType.@string;
break;
default:
QBFilterType = QueryBuilderDataType.@string;
break;
}
return QBFilterType;
}
public static QueryBuilderInputType GetQueryBuilderInputType(string PropertyType)
{
QueryBuilderInputType QBInputType;
switch (PropertyType)
{
case "bool":
QBInputType = QueryBuilderInputType.radio;
break;
case "enum":
QBInputType = QueryBuilderInputType.select;
break;
case "DateTime":
case "double":
case "int":
case "string":
QBInputType = QueryBuilderInputType.text;
break;
default:
QBInputType = QueryBuilderInputType.text;
break;
}
return QBInputType;
}
public static List<QueryBuilderFilterOperators> GetQueryBuilderFilterOperator(QueryBuilderInputType? QBInputType )
{
List<QueryBuilderFilterOperators> QBFilterOps = new List<QueryBuilderFilterOperators>();
switch (QBInputType)
{
case QueryBuilderInputType.text:
QBFilterOps = DefaultTextOperators;
break;
case QueryBuilderInputType.textarea:
QBFilterOps = DefaultTextAreaOperators;
break;
case QueryBuilderInputType.radio:
QBFilterOps = DefaultRadioOperators;
break;
case QueryBuilderInputType.checkbox:
QBFilterOps = DefaultCheckBoxOperators;
break;
case QueryBuilderInputType.select:
QBFilterOps = DefaultSelectOperators;
break;
case null:
QBFilterOps = DefaultBlankOperators;
break;
default:
QBFilterOps = DefaultAllOperators;
break;
}
return QBFilterOps;
}
// Some Default Operators List for convenience
public static List<QueryBuilderFilterOperators> DefaultTextOperators = new List<QueryBuilderFilterOperators>
{
QueryBuilderFilterOperators.equal,
QueryBuilderFilterOperators.not_equal,
QueryBuilderFilterOperators.@in,
QueryBuilderFilterOperators.not_in,
QueryBuilderFilterOperators.less,
QueryBuilderFilterOperators.less_or_equal,
QueryBuilderFilterOperators.greater,
QueryBuilderFilterOperators.greater_or_equal,
QueryBuilderFilterOperators.between,
QueryBuilderFilterOperators.not_between,
QueryBuilderFilterOperators.begins_with,
QueryBuilderFilterOperators.not_begins_with,
QueryBuilderFilterOperators.contains,
QueryBuilderFilterOperators.not_contains,
QueryBuilderFilterOperators.ends_with,
QueryBuilderFilterOperators.not_ends_with,
QueryBuilderFilterOperators.is_empty,
QueryBuilderFilterOperators.is_not_empty,
QueryBuilderFilterOperators.is_null,
QueryBuilderFilterOperators.is_not_null
};
public static List<QueryBuilderFilterOperators> DefaultTextAreaOperators = new List<QueryBuilderFilterOperators>
{
QueryBuilderFilterOperators.equal,
QueryBuilderFilterOperators.not_equal,
QueryBuilderFilterOperators.@in,
QueryBuilderFilterOperators.not_in,
QueryBuilderFilterOperators.less,
QueryBuilderFilterOperators.less_or_equal,
QueryBuilderFilterOperators.greater,
QueryBuilderFilterOperators.greater_or_equal,
QueryBuilderFilterOperators.between,
QueryBuilderFilterOperators.not_between,
QueryBuilderFilterOperators.begins_with,
QueryBuilderFilterOperators.not_begins_with,
QueryBuilderFilterOperators.contains,
QueryBuilderFilterOperators.not_contains,
QueryBuilderFilterOperators.ends_with,
QueryBuilderFilterOperators.not_ends_with,
QueryBuilderFilterOperators.is_empty,
QueryBuilderFilterOperators.is_not_empty,
QueryBuilderFilterOperators.is_null,
QueryBuilderFilterOperators.is_not_null
};
public static List<QueryBuilderFilterOperators> DefaultRadioOperators = new List<QueryBuilderFilterOperators>
{
QueryBuilderFilterOperators.equal,
QueryBuilderFilterOperators.not_equal,
QueryBuilderFilterOperators.is_empty,
QueryBuilderFilterOperators.is_not_empty,
QueryBuilderFilterOperators.is_null,
QueryBuilderFilterOperators.is_not_null
};
public static List<QueryBuilderFilterOperators> DefaultCheckBoxOperators = new List<QueryBuilderFilterOperators>
{
QueryBuilderFilterOperators.@in,
QueryBuilderFilterOperators.not_in,
QueryBuilderFilterOperators.is_empty,
QueryBuilderFilterOperators.is_not_empty,
QueryBuilderFilterOperators.is_null,
QueryBuilderFilterOperators.is_not_null
};
public static List<QueryBuilderFilterOperators> DefaultSelectOperators = new List<QueryBuilderFilterOperators>
{
QueryBuilderFilterOperators.equal,
QueryBuilderFilterOperators.not_equal,
QueryBuilderFilterOperators.is_empty,
QueryBuilderFilterOperators.is_not_empty,
QueryBuilderFilterOperators.is_null,
QueryBuilderFilterOperators.is_not_null
};
public static List<QueryBuilderFilterOperators> DefaultBlankOperators = new List<QueryBuilderFilterOperators>();
public static List<QueryBuilderFilterOperators> DefaultAllOperators = new List<QueryBuilderFilterOperators>
{
QueryBuilderFilterOperators.equal,
QueryBuilderFilterOperators.not_equal,
QueryBuilderFilterOperators.@in,
QueryBuilderFilterOperators.not_in,
QueryBuilderFilterOperators.less,
QueryBuilderFilterOperators.less_or_equal,
QueryBuilderFilterOperators.greater,
QueryBuilderFilterOperators.greater_or_equal,
QueryBuilderFilterOperators.between,
QueryBuilderFilterOperators.not_between,
QueryBuilderFilterOperators.begins_with,
QueryBuilderFilterOperators.not_begins_with,
QueryBuilderFilterOperators.contains,
QueryBuilderFilterOperators.not_contains,
QueryBuilderFilterOperators.ends_with,
QueryBuilderFilterOperators.not_ends_with,
QueryBuilderFilterOperators.is_empty,
QueryBuilderFilterOperators.is_not_empty,
QueryBuilderFilterOperators.is_null,
QueryBuilderFilterOperators.is_not_null
};
}
//enums
public enum QueryBuilderDataType
{
@string,
@integer,
@double,
@date,
@time,
@datetime,
@boolean
}
public enum QueryBuilderFilterOperators
{
equal,
not_equal,
@in,
not_in,
less,
less_or_equal,
greater,
greater_or_equal,
between,
not_between,
begins_with,
not_begins_with,
contains,
not_contains,
ends_with,
not_ends_with,
is_empty,
is_not_empty,
is_null,
is_not_null
}
public enum QueryBuilderInputType
{
text,
textarea,
radio,
checkbox,
select
}
WebService
[WebMethod(Description = "Get Query Builder Filter Option")]
public QueryBuilderSettings GetQueryBuilderFilterOption()
{
string QBID;
string QBLabel;
string propertyInputType;
QueryBuilderDataType QBDataType;
List<QueryBuilderFilterOperators> QBFilterOperators;
string propertyType;
QueryBuilderInputType QBInputType;
//List<object> dropdownValues = new List<object>();
Dictionary<string, string> dropdownValues = new Dictionary<string, string>();
QueryBuilderSettings settings = new QueryBuilderSettings();
// plugins
settings.plugins.Add("bt-tooltip-errors");
settings.plugins.Add("not-group");
settings.plugins.Add("sortable");
// filters
List<string> PropertyNames = GetPropertyList();
foreach(string propertyName in PropertyNames)
{
QBID = propertyName;
QBLabel = PropertyTitle[propertyName];
propertyInputType = GetPropertyInputType(propertyName);
QBDataType = QueryBuilderFilter.GetQueryBuilderDataType(propertyInputType);
propertyType = GetPropertyType(propertyName);
QBInputType = QueryBuilderFilter.GetQueryBuilderInputType(propertyType);
QBFilterOperators = QueryBuilderFilter.GetQueryBuilderFilterOperator(QBInputType);
dropdownValues = GetDropdownDictionary(propertyName);
settings.filters.Add(new QueryBuilderFilter(QBID, QBLabel, QBDataType, QBFilterOperators, QBInputType, dropdownValues));
//dropdownValues = new List<object>();
dropdownValues = new Dictionary<string, string>(); //Clear the Dictionary or it will add up all dropdown from different properties
}
return settings;
}
Calling it from webpage MyPage.cshtml
<script src="../../Scripts/jQuery.extendext-master/jQuery.extendext.js"></script>
<script src="../../Scripts/jquery.query-builder/js/query-builder.min.js"></script>
<script src="../../Scripts/jquery.query-builder/js/query-builder.standalone.min.js"></script>
<div class="query-builder form-inline">
<div id="builder"></div>
</div>
<script type="text/javascript">
$(function () {
var settings=AjaxFromWebService("../../WS/YourWebService.ASMX/GetQueryBuilderFilterOption", "");
console.log(settings); // you can see the object when you press F12 on your web browser
$(document).ready(function () {
$('#builder').queryBuilder(settings);
})
});
function AjaxFromWebService(Url,JsonData){
var returnData="";
$.ajax({
type: "post",
contentType: "application/json; charset=utf-8",
url: Url,
data: JsonData,
dataType: "json",
async:false,
success: function (result) {
if(result.d){
returnData=result.d;
} else {
}
},
error: function () {
BootstrapDialog.alert({message:"Ajax failed",type:BootstrapDialog.TYPE_DANGER});
}
});
return function(){
return returnData;
}();
}
</script>