Skip to content

Commit

Permalink
Close #360 add "value_separator" filter option
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Oct 22, 2016
1 parent cdf19bb commit f5d4359
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ <h3>Output</h3>
en: 'Name',
fr: 'Nom'
},
value_separator: ',',
type: 'string',
optgroup: 'core',
default_value: 'Mistic',
Expand Down Expand Up @@ -481,6 +482,10 @@ <h3>Output</h3>
operator: 'equal',
value: 'B.3'
}]
}, {
id: 'name',
operator: 'in',
value: ['Mistic', 'Damien']
}, {
empty: true
}]
Expand Down
2 changes: 2 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,8 @@ QueryBuilder.prototype.updateRuleOperator = function(rule, previousOperator) {
}

this.trigger('afterUpdateRuleOperator', rule);

this.updateRuleValue(rule);
};

/**
Expand Down
9 changes: 9 additions & 0 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,12 @@ QueryBuilder.prototype.getRuleValue = function(rule) {
}
}

if (operator.multiple && filter.value_separator) {
value = value.map(function(val) {
return val.split(filter.value_separator);
});
}

if (operator.nb_inputs === 1) {
value = value[0];
}
Expand Down Expand Up @@ -393,6 +399,9 @@ QueryBuilder.prototype.setRuleValue = function(rule, value) {
break;

default:
if (operator.multiple && filter.value_separator && $.isArray(value[i])) {
value[i] = value[i].join(filter.value_separator);
}
$value.find('[name=' + name + ']').val(value[i]).trigger('change');
break;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ QUnit.assert.match = function(actual, regex, message) {
var basic_filters = [{
id: 'name',
label: 'Name',
type: 'string'
type: 'string',
value_separator: ','
}, {
id: 'category',
label: 'Category',
Expand Down
29 changes: 29 additions & 0 deletions tests/data.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,35 @@ $(function(){
);
});

/**
* Test value separator
*/
QUnit.test('value separator', function(assert) {
$b.queryBuilder({
filters: basic_filters,
rules: [{
id: 'name',
operator: 'equal',
value: 'Mistic,Damien'
}]
});

$('[name=builder_rule_0_operator]').val('in').trigger('change');

assert.rulesMatch(
$b.queryBuilder('getRules'),
{
condition: 'AND',
rules: [{
id: 'name',
operator: 'in',
value: ['Mistic', 'Damien']
}]
},
'Should split values on comma'
);
});


var validation_filters = [{
id: 'radio',
Expand Down

0 comments on commit f5d4359

Please sign in to comment.