Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To get a dynamic result a standardized naming is the best way to go. You start by standardizing the criteria selectors, giving them a general class and differentiating with id properties. Following a naming convetion for values is also recommended. This would produce something in the lines of</p> <pre><code> &lt;select id="deal" class="criteriaSelector"&gt; &lt;option value="all"&gt;All deals&lt;/option&gt; &lt;option value="50"&gt;$50&lt;/option&gt; &lt;option value="25"&gt;$25&lt;/option&gt; &lt;/select&gt; &lt;select id="review" class="criteriaSelector"&gt; &lt;option value="all"&gt;All reviews&lt;/option&gt; &lt;option value="positive"&gt;Positive reviews&lt;/option&gt; &lt;option value="negative"&gt;Negative reviews&lt;/option&gt; &lt;/select&gt; </code></pre> <p>Then you give all the items you want to filter a generic class besides the filtering classes. I will use <em>.item</em> as an example. You will also want to be able to construct the class from selected value and criteria name so using the <em>.{id}-{value}</em> system will work perfectly. This will leave us with something in the lines of </p> <pre><code> &lt;div class="item review-positive deal-25"&gt;&lt;/div&gt; &lt;div class="item review-negative deal-50"&gt;&lt;/div&gt; </code></pre> <p>This setup will allow us to build dynamic and extensible code:</p> <pre><code> $('.criteriaSelector').change(function() { // Initialize criteria string var criteria = ''; // Set value for all selector var showAll = true; // Iterate over all criteriaSelectors $('.criteriaSelector').each(function(){ // Get value var val = $(this).children(':selected').val(); // Check if this limits our results if(val !== 'all'){ // Append selector to criteria criteria += '.' + $(this).attr('id') + '-' + val; // We don't want to show all results anymore showAll = false; } }); // Check if results are limited somehow if(showAll){ // No criterias were set so show all $('.item').show(); } else { // Hide all items $('.item').hide(); // Show the ones that were selected $(criteria).show(); } }); </code></pre> <p>Now adding a new criteria selector won't require changes in this piece of code as long as naming conventions are followed.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload