Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ <h3>Output</h3>
$('.parse-json').on('click', function() {
$('#result').removeClass('hide')
.find('pre').html(JSON.stringify(
$('#builder').queryBuilder('getRules', {get_flags: true}),
$('#builder').queryBuilder('getRules', {get_flags: true, skip_empty : true}),
undefined, 2
));
});
Expand Down
19 changes: 15 additions & 4 deletions src/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ QueryBuilder.prototype.getModel = function(target) {

/**
* Validate the whole builder
* @param {object} options
* - skip_empty: false[default] | true(skips validating rules that have no filter selected)
* @return {boolean}
*/
QueryBuilder.prototype.validate = function() {
QueryBuilder.prototype.validate = function(options) {
this.clearErrors();

var self = this;
Expand All @@ -84,6 +86,11 @@ QueryBuilder.prototype.validate = function() {
var errors = 0;

group.each(function(rule) {
if(!rule.filter && !rule.operator && options.skip_empty){
done++;
return;
}

if (!rule.filter) {
self.triggerValidationError(rule, 'no_filter', null);
errors++;
Expand Down Expand Up @@ -137,15 +144,17 @@ QueryBuilder.prototype.validate = function() {
* @param {object} options
* - get_flags: false[default] | true(only changes from default flags) | 'all'
* - allow_invalid: false[default] | true(returns rules even if they are invalid)
* - skip_empty: false[default] | true(skips validating rules that have no filter selected)
* @return {object}
*/
QueryBuilder.prototype.getRules = function(options) {
options = $.extend({
get_flags: false,
allow_invalid: false
allow_invalid: false,
skip_empty : false
}, options);

var valid = this.validate();
var valid = this.validate(options);
if (!valid && !options.allow_invalid) {
return null;
}
Expand Down Expand Up @@ -194,7 +203,9 @@ QueryBuilder.prototype.getRules = function(options) {
ruleData.flags = flags;
}
}

if( options.skip_empty && !ruleData.id && !ruleData.field && !ruleData.type && !ruleData.operator){
return;//we want to skip rules that are empty. Don't return them.
}
groupData.rules.push(self.change('ruleToJson', ruleData, rule));

}, function(model) {
Expand Down