Skip to content

Commit 62f6f44

Browse files
Ruud-cbmistic100
authored andcommitted
Merge pull request mistic100#428 Fix mistic100#426 add "skip_empty" flag to "getRules"
1 parent e5d48f8 commit 62f6f44

File tree

3 files changed

+76
-15
lines changed

3 files changed

+76
-15
lines changed

examples/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,10 @@ <h3>Output</h3>
595595
$('.parse-json').on('click', function() {
596596
$('#result').removeClass('hide')
597597
.find('pre').html(JSON.stringify(
598-
$('#builder').queryBuilder('getRules', { get_flags: true }),
598+
$('#builder').queryBuilder('getRules', {
599+
get_flags: true,
600+
skip_empty: true
601+
}),
599602
undefined, 2
600603
));
601604
});

src/public.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,15 @@ QueryBuilder.prototype.getModel = function(target) {
7272

7373
/**
7474
* Validate the whole builder
75+
* @param {object} options
76+
* - skip_empty: false[default] | true(skips validating rules that have no filter selected)
7577
* @return {boolean}
7678
*/
77-
QueryBuilder.prototype.validate = function() {
79+
QueryBuilder.prototype.validate = function(options) {
80+
options = $.extend({
81+
skip_empty: false
82+
}, options);
83+
7884
this.clearErrors();
7985

8086
var self = this;
@@ -84,6 +90,10 @@ QueryBuilder.prototype.validate = function() {
8490
var errors = 0;
8591

8692
group.each(function(rule) {
93+
if (!rule.filter && options.skip_empty) {
94+
return;
95+
}
96+
8797
if (!rule.filter) {
8898
self.triggerValidationError(rule, 'no_filter', null);
8999
errors++;
@@ -109,17 +119,21 @@ QueryBuilder.prototype.validate = function() {
109119
done++;
110120

111121
}, function(group) {
112-
if (parse(group)) {
122+
var res = parse(group);
123+
if (res === true) {
113124
done++;
114125
}
115-
else {
126+
else if (res === false) {
116127
errors++;
117128
}
118129
});
119130

120131
if (errors > 0) {
121132
return false;
122133
}
134+
else if (done === 0 && !group.isRoot() && options.skip_empty) {
135+
return null;
136+
}
123137
else if (done === 0 && (!self.settings.allow_empty || !group.isRoot())) {
124138
self.triggerValidationError(group, 'empty_group', null);
125139
return false;
@@ -137,15 +151,17 @@ QueryBuilder.prototype.validate = function() {
137151
* @param {object} options
138152
* - get_flags: false[default] | true(only changes from default flags) | 'all'
139153
* - allow_invalid: false[default] | true(returns rules even if they are invalid)
154+
* - skip_empty: false[default] | true(remove rules that have no filter selected)
140155
* @return {object}
141156
*/
142157
QueryBuilder.prototype.getRules = function(options) {
143158
options = $.extend({
144159
get_flags: false,
145-
allow_invalid: false
160+
allow_invalid: false,
161+
skip_empty: false
146162
}, options);
147163

148-
var valid = this.validate();
164+
var valid = this.validate(options);
149165
if (!valid && !options.allow_invalid) {
150166
return null;
151167
}
@@ -170,6 +186,10 @@ QueryBuilder.prototype.getRules = function(options) {
170186
}
171187

172188
group.each(function(rule) {
189+
if (!rule.filter && options.skip_empty) {
190+
return;
191+
}
192+
173193
var value = null;
174194
if (!rule.operator || rule.operator.nb_inputs !== 0) {
175195
value = rule.value;
@@ -198,7 +218,10 @@ QueryBuilder.prototype.getRules = function(options) {
198218
groupData.rules.push(self.change('ruleToJson', ruleData, rule));
199219

200220
}, function(model) {
201-
groupData.rules.push(parse(model));
221+
var data = parse(model);
222+
if (data.rules.length !== 0 || !options.skip_empty) {
223+
groupData.rules.push(data);
224+
}
202225
}, this);
203226

204227
return self.change('groupToJson', groupData, group);

tests/data.module.js

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$(function(){
1+
$(function() {
22
var $b = $('#builder');
33

44
QUnit.module('data', {
@@ -31,9 +31,9 @@ $(function(){
3131
type: 'string',
3232
input: 'select',
3333
values: [
34-
{one: 'One'},
35-
{two: 'Two'},
36-
{three: 'Three'}
34+
{ one: 'One' },
35+
{ two: 'Two' },
36+
{ three: 'Three' }
3737
]
3838
}],
3939
rules: {
@@ -207,7 +207,7 @@ $(function(){
207207
QUnit.test('custom data', function(assert) {
208208
var rules = {
209209
condition: 'AND',
210-
data: [1,2,3],
210+
data: [1, 2, 3],
211211
rules: [{
212212
id: 'name',
213213
value: 'Mistic',
@@ -361,13 +361,13 @@ $(function(){
361361
};
362362

363363
assert.rulesMatch(
364-
$b.queryBuilder('getRules', {get_flags: true}),
364+
$b.queryBuilder('getRules', { get_flags: true }),
365365
rules_changed_flags,
366366
'Should export rules with changed flags'
367367
);
368368

369369
assert.rulesMatch(
370-
$b.queryBuilder('getRules', {get_flags: 'all'}),
370+
$b.queryBuilder('getRules', { get_flags: 'all' }),
371371
rules_all_flags,
372372
'Should export rules with all flags'
373373
);
@@ -455,13 +455,48 @@ $(function(){
455455
);
456456
});
457457

458+
/**
459+
* Test skip_empty option
460+
*/
461+
QUnit.test('skip empty', function(assert) {
462+
$b.queryBuilder({
463+
filters: basic_filters
464+
});
465+
466+
$b.queryBuilder('setRules', {
467+
condition: 'AND',
468+
rules: [{
469+
id: 'name',
470+
operator: 'equal',
471+
value: 'Mistic'
472+
}, {
473+
empty: true
474+
}]
475+
});
476+
477+
assert.rulesMatch(
478+
$b.queryBuilder('getRules', {
479+
skip_empty: true
480+
}),
481+
{
482+
condition: 'AND',
483+
rules: [{
484+
id: 'name',
485+
operator: 'equal',
486+
value: 'Mistic'
487+
}]
488+
},
489+
'Should skip empty rules for getRules'
490+
);
491+
});
492+
458493
/**
459494
* Test allow_empty_value option
460495
*/
461496
QUnit.test('allow empty value', function(assert) {
462497
var filters = $.extend(true, [], basic_filters);
463498
filters.forEach(function(filter) {
464-
filter.validation = $.extend({allow_empty_value: true}, filter.validation);
499+
filter.validation = $.extend({ allow_empty_value: true }, filter.validation);
465500
});
466501

467502
$b.queryBuilder({

0 commit comments

Comments
 (0)