jQuery-QueryBuilder icon indicating copy to clipboard operation
jQuery-QueryBuilder copied to clipboard

Multi-tiered filter category

Open sergxp opened this issue 6 years ago • 13 comments

I'm trying to implement this library, but I need the ability to add a multi-tiered filter categories. Where the first tier opens another select box with 2nd tier options and so on... Is this currently possible or will this be a new feature?

image

sergxp avatar Mar 16 '18 16:03 sergxp

You can have two levels with optgroups https://github.com/mistic100/jQuery-QueryBuilder/issues/207 Or you can enabled search with bt-selectpicker ou chosen-selectpicker plugins https://github.com/mistic100/jQuery-QueryBuilder/issues/530

I'll keep this issue open, but clearly I don't like to have such complexity

mistic100 avatar Mar 16 '18 16:03 mistic100

@mistic100 I realize you dislike the complexity this adds after my extensive searches on the topic, but it would make this builder much more flexible for developers. I'm in a situation where we have dozens of clients and we'd like to be able to filter for specific parameters around those clients. Adding a maximum of two additional filters would be extremely useful.

Putting each parameter we need into a separate optgroup for each client account would make the filter list extremely long (100+ options) and the UI would become un-navigable. What you have created is excellent - the best I've seen - but without nested filters, we aren't able to use the query builder to its full potential.

dholle02 avatar Apr 27 '18 19:04 dholle02

@dholle02 Did you ever figure out how to implement this? I am running into the same issue where we have ~190 filter options across 20 option groups. The single dropdown works but it would be nice to select the option group then the filter inside that group.

njones57 avatar Aug 23 '18 16:08 njones57

Did you read the issue #530 I linked ?

mistic100 avatar Aug 24 '18 07:08 mistic100

Yes. While searchable single drop-down would be somewhat helpful, having a drop-down to select a option group and a second drop-down to show only the filters in that option group is what is desired.

njones57 avatar Aug 24 '18 10:08 njones57

@njones57 I was never able to find a solution so we put the project on hold, which is a shame because this would have been a great implementation and really helped our end users. The searchable dropdown does not make sense for our UX and the list options we need to display, so we decided to do nothing instead of doing something that didn't make sense. I wish I had a better answer for you. Please let me know if you figure something else out in the meantime.

Perhaps @mistic100 will open his heart to our pleas and improve this already incredible tool :)

dholle02 avatar Aug 24 '18 17:08 dholle02

As I said in the other issue, everything can be done with plugins and event hooks.

You could for example initialize your filters with optgroups and have hook on "afterCreateRuleFilters" to build two new selects from the optgroups and values, have them synchronized and update the original select (which you have hidden) when their value changes.

mistic100 avatar Aug 24 '18 17:08 mistic100

@mistic100 thank for this awesome plugin!

I've been trying to solve the same issue for UI autocomplete for few days; and finally I found a solution. I hope this can help everyone who looking for custom filter creation.

In summary, filter input return typical html in one container and trigger a change when final selection is made. Filter valueGetter is where we get the final selected value to be returned.

Part 1: Create filter with custom input and valueGetter

{
  id: 'ui_autocomplete',
  label: 'Autocomplete Selection',
  type: 'integer',
  default_value: [],
  operators: ['in', 'not_in'],
  input: function(rule, input_name) {
    return getAutocomplete(rule['id'], input_name);

    /*
    // getAutocomplete() return:
    <div class="autocomplete_container"> 
      <input type="text" id="autocomplete" placeholder="autocomplete" class="ac_' + rule_id + '">
      <div class="ac_select_container ac_' + rule_id + '_select">
        <div>
          Selected item label

           // input we get the value from, so it's doesn't matter if there is lot of form input
          <input type="hidden" name="' + input_name + '" value="...">
        </div>
      </div>
    </div>
     */
  },
  valueGetter: function(rule) {
    var operator  = rule.operator;
    var container = rule.$el.find('.rule-value-container');
    var value     = [];

    // My case custom value getter
    for (var i = 0; i < operator.nb_inputs; i++) {
      container.find('[name=' + rule.id + '_value_' + i + ']').each(function() {
        value.push(parseInt($(this).val()));
      });
    }

    return value;
  }
}

Part 2: What important is the trigger change

$('#autocomplete').autocomplete({
  source: function(request, response) {
    return []; // dynamic from db
  },
  select: function(item) {
    // Insert selected to ac_select_container type input:hidden here

    // Important to trigger "valueGetter" 
    // Ref: https://querybuilder.js.org/api/core.js.html#line-711
    $('.autocomplete_container').trigger('change');
  }
});

Part 3: My output of $('#builder').queryBuilder('getRules');

{
  "condition": "AND",
  "rules": [
    {
      "id": "ui_autocomplete",
      "field": "ui_autocomplete",
      "type": "integer",
      "operator": "in",
      "value": [ // populated by "valueGetter" from hidden input
        34,
        40,
        47
      ]
    }
  ],
  "valid": true
}

qaharmdz avatar Jan 14 '19 04:01 qaharmdz

is there any progress on this multi-tier dropdown point ?

chiragkanhasoft avatar Feb 13 '19 14:02 chiragkanhasoft

@mistic100 how does it look, is it still planning?

dejmsi avatar Apr 03 '19 12:04 dejmsi

/*
// getAutocomplete() return:
<div class="autocomplete_container"> 
  <input type="text" id="autocomplete" placeholder="autocomplete" class="ac_' + rule_id + '">
  <div class="ac_select_container ac_' + rule_id + '_select">
    <div>
      Selected item label

       // input we get the value from, so it's doesn't matter if there is lot of form input
      <input type="hidden" name="' + input_name + '" value="...">
    </div>
  </div>
</div>
 */

@qaharmdz I'm trying to solve this again - 2 years later - and am looking at your solution. Can you tell me what you did with the commented out code in part 1 above? How does the autocomplete input field get added to the DOM? Confused where the HTML ends up.

dholle02 avatar Jan 06 '20 00:01 dholle02

any news on making multiple tier/level filter selection?

dimo2008 avatar May 23 '21 12:05 dimo2008

2024: If anyone struggling to create multiple filters - Its possible in unconventional way. I tried having a normal filter with all the filter entities. Along with it, I tried to create two filters to split the filters. Have these two filters injected into the filter container and hide the exisitng filter. Whenever the custom filter control is changed I would set the value of the main filter which was hidden and rest all works acordingly. We need valueSetter event, afterCreateRuleFilters. I will try to add more details soon. Just wanted to put across this initial thought that its possible but not straightforward.

mvksages avatar Apr 19 '24 20:04 mvksages