Hi there,
I’m trying to set up a multiple filters workflow for my app. Actually, it’s possible to filter every collection records, depending on 6 operators, with this function :
export function filter_operator(concat, operator, filter, val) {
var complete_filter = {};
switch (operator) {
case "equalTo":
complete_filter = {[filter]: { $eq: val }};
break;
case "isDifferentFrom":
complete_filter = {[filter]: { $ne: val }};
break
case "contain":
complete_filter = {[filter]: { $regex: '.*' + val + '.*' }};
break
case "doNotContain":
complete_filter = {[filter]: { $not: { $regex: val } } };
break
case "isSet":
complete_filter = {[filter]: { $exists: true } };
break
case "isNotSet":
complete_filter = {[filter]: { $exists: false } };
break
default:
break;
}
return complete_filter;
}
After a click, i’m triggering this function which filter my table results on screen depending on what the user has selected.
But as you may have seen, there is a “concat” var, which i want to use to allow someone to use multiple filters through the same workflow. For example, he first click on a filter button to filter all “partners” which actually have validated accounts, and he can also change filters through a select and an input (it actually works) to use a second filter, to get all validated account AND people that have empty carts, for example. (This is what i want to do now).
But i don’t know how to do it, i tried many things but it still dont work. Any ideas ?
Thanks a lot btw !
Happy coding,
B.
PS : Here the rest of the code if you need :
'click .tw-filter-submit': function (events, template) {
var concatFilters = Template.instance().filtersVar.get();
console.log(concatFilters);
var filterOperator = $('#partnersFilterOperator').val();
var selectFilter = $('#partnersFilterSelect').val();
var filterVal = $('.tw-filter-input').val();
Template.instance().filtersVar.set(filter_operator(concatFilters, filterOperator, selectFilter, filterVal))
},
And :
<div class="row justify-content-end">
{{#if collection_key}}
<select name="partnersFilterSelect" id="partnersFilterSelect">
{{#each collection_key}}
<option value="{{this}}">{{_ this}}</option>
{{/each}}
</select>
{{/if}}
<select name="partnersFilterOperator" id="partnersFilterOperator">
<option value="equalTo">{{_ "Equal to"}}</option>
<option value="isDifferentFrom">{{_ "Is different from"}}</option>
<option value="contain">{{_ "Contain"}}</option>
<option value="doNotContain">{{_ "Do not contain"}}</option>
<option value="isSet">{{_ "Is set"}}</option>
<option value="isNotSet">{{_ "Is not set"}}</option>
</select>
<input type="text" placeholder="Filter by ..." class="col-6 tw-filter-input">
<button class="btn btn-primary tw-filter-submit" type="submit">{{_ "Validate"}}</button>
<button class="btn btn-warning tw-filter-remove" type="submit">{{_ "Remove filters"}}</button>
</div>