-
Notifications
You must be signed in to change notification settings - Fork 111
Supporting jQuery QueryBuilder with QueryBuilderFilterRule or JsonNetFilterRule or SystemTextJsonFilterRule
In the latest version of dynamic-linq-query-builder there are two ways to support integration with jQuery-Querybuilder, and they depend on how you handle serialization.
The "problem" is that the value assigned to a rule by jQuery-Querybuilder can be either a string or an array. When pushing those rules to the server, this creates problems depending on the serialization/deserialization library you use, as most libraries handle this case differently.
Previous versions of this library recommended using the now obsolete FilterRule, and using some client-side javascript to convert the array value entries produced by jQuery-Querybuilder to a comma-delimited single string. This can technically cause issues if one of the valid values has a comma in it.
Below are two new options for supporting jQuery-Querybuilder without the possibility of a comma bug along with their descriptions and limitations.
Use the serialization library of your choice, or the default serialization library JavascriptSerializer along with the QueryBuilderFilterRule object on the c# side, along with the following client-side code to normalize all value entries in the jQuery-Querybuilder getRules call to arrays. See the Sample project for this implementation.
var convertSingleValuesToArray = function (obj) {
if (obj != null) {
if (obj.hasOwnProperty("value")) {
if (Object.prototype.toString.call(obj.value) !== '[object Array]') {
obj.value = [obj.value];
}
}
if (obj.hasOwnProperty("rules") && obj.rules != null) {
for (var i = 0; i < obj.rules.length; i++) {
convertSingleValuesToArray(obj.rules[i]);
}
}
}
}
var getRules = function() {
try {
var res = jqueryQueryBuilder.queryBuilder('getRules');
convertSingleValuesToArray(res);
return res;
} catch (ex) {
return null;
}
}Use the JsonNetFilterRule along with JSON.Net as your serializer/deserializer. JSON.Net is capable of negotiating and boxing/unboxing the value created by jQuery-Querybuilder as either a string or an array on-demand, with no client-side modifications to the rules before being sent server-side. If you use JSON.Net as your serializer/deserializer, then this may be the optimal solution for you. That said, if you use the default JavascriptSerializer, or a library like Jil to handle serialization/deserialization, the value object will not be handled properly and will cause queries to fail.
Use the SystemTextJsonFilterRule class along with System.Text.Json and the Castle.DynamicLinqQueryBuilder.SystemTextJson NuGet packages. If you are using the System.Text.Json for your serialization, this should suit your needs, but is not included by default because it has a hard dependency on System.Text.Json, while the main package has no external dependencies.