mistic100 / jQuery-QueryBuilder

jQuery plugin offering an interface to create complex queries
https://querybuilder.js.org
MIT License
1.68k stars 552 forks source link

Use `values` instead of `value` when having multiple values #475

Open malhayek2014 opened 7 years ago

malhayek2014 commented 7 years ago

Thank you for this awesome package.

I noticed that the getRules() function return the user's selected in a property called value. Value is a string in most time, unless between or multiple select is used then it is changed to an array.

If there a way to override the default behavior so that if the values are array of strings, then put them in a new property called values while if the property is a single string it can stay in the value property. This will make parsing the object easier with c# or ASP.NET MVC framework.

Thank you

mistic100 commented 7 years ago

Use changers. http://querybuilder.js.org/#events

// it's a bit unreadable because the first "value" is the value contained by the event (the rule)
// and the second "value" is the actual rule value
$('#builder').on('ruleToJson.queryBuilder.filter', function(e) {
  if (Array.isArray(e.value.value)) {
    e.value.values = e.value.value;
    delete e.value.value;
  }
});

Please do not close the issue, I think I will make it the default in version 3

mistic100 commented 7 years ago

If you need to reuse stored rules :

$('#builder').on('jsonToRule.queryBuilder.filter', function(e, json) {
  if (json.values !== undefined) {
    e.value.value = json.values;
  }
});
malhayek2014 commented 7 years ago

This is perfect. Thank you so much.

since you want to add this in v3.0.0, it may not be a bad idea to also parse the string when using "in" and "not_in" and store the parsed string in the values property

mistic100 commented 7 years ago

I don't understand, perhaps you want to use the value_separator filter property.

malhayek2014 commented 7 years ago

when when using "in" or "not_in" use the value_separator to parse the string and store the result in values and delete value It'll be one other less step that the user will have to do