<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Filtering HubDB elements with HTML Checkboxes in CMS Development</title>
    <link>https://community.hubspot.com/t5/CMS-Development/Filtering-HubDB-elements-with-HTML-Checkboxes/m-p/297319#M13998</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm working on creating a video directory, using HubDB to store the details for each video.&lt;/P&gt;&lt;P&gt;I'm looking for a way where a user can filter the results iterated from HubDB by interacting with HTML checkboxes like this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 180px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/20726i5B9A10D51AE9C45D/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The issue I'm having at the moment is that I don't see a straightforward way to connect the user interacting with checkboxes and the HubDB filter queries.&lt;/P&gt;&lt;P&gt;I tried using javascript for if conditionals and use HubL code inside, but it seems that the HubL code regardless if it passes the logic test or not.&amp;nbsp; I have since removed that and I am a little stuck.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would greatly appreciate some help. Thanks!&lt;/P&gt;&lt;P&gt;I've included the HTML+HubL code below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;{% set nofilter = hubdb_table_rows(1036506)%}
&amp;lt;div class="hubdb-outer-wrapper"&amp;gt;Level-of-interest
  &amp;lt;div class="hubdb-content-wrapper"&amp;gt;
  &amp;lt;div class="hubdb-left-column"&amp;gt;
    Level of Study
    &amp;lt;div class="checkboxes"&amp;gt;
     &amp;lt;input id="emba-box" type="checkbox" name="Level-of-interest-emba" value="Executive+MBA"&amp;gt;EMBA
     &amp;lt;input id="mba-box" type="checkbox" name="Level-of-interest-mba" value="MBA" onclick="mbaFilter()"&amp;gt;MBA&amp;lt;br&amp;gt;
     &amp;lt;input id="masters-box" type="checkbox" name="Level-of-interest-masters" value="Masters" checked&amp;gt;Masters&amp;lt;br&amp;gt;
    &amp;lt;/div&amp;gt;
    Region of Study
    &amp;lt;div class="checkboxes"&amp;gt;
     &amp;lt;input id="china-box" type="checkbox" name="region-study-china" value="China"&amp;gt;China
     &amp;lt;input id="india-box" type="checkbox" name="region0study-india" value="India"&amp;gt;India&amp;lt;br&amp;gt;
     &amp;lt;input id="africa-box" type="checkbox" name="region-study-africa" value="Africa"&amp;gt;Africa&amp;lt;br&amp;gt;
      &amp;lt;/div&amp;gt;
   &amp;lt;/div&amp;gt;
   &amp;lt;div class="hubdb-right-column"&amp;gt;
  {% for row in nofilter %}
      &amp;lt;div class="hubdb-box"&amp;gt;
       &amp;lt;div class="hubdb-image"&amp;gt;
          &amp;lt;img src="{{ row.thumbnail.url }}"&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div class="hubdb-title"&amp;gt;
          {{ row.name }}
        &amp;lt;/div&amp;gt;
        &amp;lt;div class="hubdb-description"&amp;gt;
          {{ row.description }}
        &amp;lt;/div&amp;gt;
     &amp;lt;/div&amp;gt;
     {% endfor %}
   &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 17 Oct 2019 13:26:53 GMT</pubDate>
    <dc:creator>Aaaaandrei</dc:creator>
    <dc:date>2019-10-17T13:26:53Z</dc:date>
    <item>
      <title>Filtering HubDB elements with HTML Checkboxes</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Filtering-HubDB-elements-with-HTML-Checkboxes/m-p/297319#M13998</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm working on creating a video directory, using HubDB to store the details for each video.&lt;/P&gt;&lt;P&gt;I'm looking for a way where a user can filter the results iterated from HubDB by interacting with HTML checkboxes like this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 180px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/20726i5B9A10D51AE9C45D/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The issue I'm having at the moment is that I don't see a straightforward way to connect the user interacting with checkboxes and the HubDB filter queries.&lt;/P&gt;&lt;P&gt;I tried using javascript for if conditionals and use HubL code inside, but it seems that the HubL code regardless if it passes the logic test or not.&amp;nbsp; I have since removed that and I am a little stuck.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would greatly appreciate some help. Thanks!&lt;/P&gt;&lt;P&gt;I've included the HTML+HubL code below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;{% set nofilter = hubdb_table_rows(1036506)%}
&amp;lt;div class="hubdb-outer-wrapper"&amp;gt;Level-of-interest
  &amp;lt;div class="hubdb-content-wrapper"&amp;gt;
  &amp;lt;div class="hubdb-left-column"&amp;gt;
    Level of Study
    &amp;lt;div class="checkboxes"&amp;gt;
     &amp;lt;input id="emba-box" type="checkbox" name="Level-of-interest-emba" value="Executive+MBA"&amp;gt;EMBA
     &amp;lt;input id="mba-box" type="checkbox" name="Level-of-interest-mba" value="MBA" onclick="mbaFilter()"&amp;gt;MBA&amp;lt;br&amp;gt;
     &amp;lt;input id="masters-box" type="checkbox" name="Level-of-interest-masters" value="Masters" checked&amp;gt;Masters&amp;lt;br&amp;gt;
    &amp;lt;/div&amp;gt;
    Region of Study
    &amp;lt;div class="checkboxes"&amp;gt;
     &amp;lt;input id="china-box" type="checkbox" name="region-study-china" value="China"&amp;gt;China
     &amp;lt;input id="india-box" type="checkbox" name="region0study-india" value="India"&amp;gt;India&amp;lt;br&amp;gt;
     &amp;lt;input id="africa-box" type="checkbox" name="region-study-africa" value="Africa"&amp;gt;Africa&amp;lt;br&amp;gt;
      &amp;lt;/div&amp;gt;
   &amp;lt;/div&amp;gt;
   &amp;lt;div class="hubdb-right-column"&amp;gt;
  {% for row in nofilter %}
      &amp;lt;div class="hubdb-box"&amp;gt;
       &amp;lt;div class="hubdb-image"&amp;gt;
          &amp;lt;img src="{{ row.thumbnail.url }}"&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div class="hubdb-title"&amp;gt;
          {{ row.name }}
        &amp;lt;/div&amp;gt;
        &amp;lt;div class="hubdb-description"&amp;gt;
          {{ row.description }}
        &amp;lt;/div&amp;gt;
     &amp;lt;/div&amp;gt;
     {% endfor %}
   &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Oct 2019 13:26:53 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Filtering-HubDB-elements-with-HTML-Checkboxes/m-p/297319#M13998</guid>
      <dc:creator>Aaaaandrei</dc:creator>
      <dc:date>2019-10-17T13:26:53Z</dc:date>
    </item>
    <item>
      <title>Re: Filtering HubDB elements with HTML Checkboxes</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Filtering-HubDB-elements-with-HTML-Checkboxes/m-p/298438#M14082</link>
      <description>&lt;P&gt;Hey &lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/77028"&gt;@Aaaaandrei&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I did something similar recently! I will have to get back to the code and follow up with the solutions I had come up with.&lt;/P&gt;
&lt;P&gt;If in the unlikely scenary you don't hear from me in the nexty few days please message me!&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2019 18:39:57 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Filtering-HubDB-elements-with-HTML-Checkboxes/m-p/298438#M14082</guid>
      <dc:creator>Kevin-C</dc:creator>
      <dc:date>2019-10-23T18:39:57Z</dc:date>
    </item>
    <item>
      <title>Re: Filtering HubDB elements with HTML Checkboxes</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Filtering-HubDB-elements-with-HTML-Checkboxes/m-p/298543#M14091</link>
      <description>&lt;P&gt;Thank you Kevin, looking forward to hearing from you!&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Andrei&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2019 09:04:37 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Filtering-HubDB-elements-with-HTML-Checkboxes/m-p/298543#M14091</guid>
      <dc:creator>Aaaaandrei</dc:creator>
      <dc:date>2019-10-24T09:04:37Z</dc:date>
    </item>
    <item>
      <title>Re: Filtering HubDB elements with HTML Checkboxes</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Filtering-HubDB-elements-with-HTML-Checkboxes/m-p/298800#M14106</link>
      <description>&lt;P&gt;Hey &lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/77028"&gt;@Aaaaandrei&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the reminder message.&lt;/P&gt;
&lt;P&gt;Below I've dropped in a basic template code that I built for a POC and learning experience for myself. You'' have to excuse the mess haha.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is purely a JS solution, I might have a HUBL excusive solution that I will continue to look for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have clarified any comments I had left in the code to help understand what is going on / the thought process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&amp;lt;!doctype html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8"&amp;gt;
    &amp;lt;title&amp;gt;{{ content.html_title }}&amp;lt;/title&amp;gt;
    &amp;lt;meta name="description" content="{{ content.meta_description }}"&amp;gt;
    {{ standard_header_includes }}
    
    &amp;lt;style&amp;gt;
      img {
        max-width: 200px;
      }

      .grid {
        display: grid;
        grid-gap: 0;
        grid-template-columns: repeat(auto-fill,minmax(220px,1fr));
      }

      ul {
        padding: 0px;
      }
    &amp;lt;/style&amp;gt;
    
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;form name="filters-form"&amp;gt;&amp;lt;/form&amp;gt;

    {% if dynamic_page_route_level == 0 %}
    &amp;lt;h1&amp;gt;Gallery - 0&amp;lt;/h1&amp;gt;&amp;lt;!-- For tracking purpose - remove for production --&amp;gt;
    &amp;lt;div class="grid"&amp;gt;

      {% set rows = hubdb_table_rows(1056514) %}
      {% for row in rows %}
      &amp;lt;a class="gallery-1 gallery-card" href="{{ request.path }}/{{ row.hs_path }}" data-filter="{{row.tags}}"&amp;gt;
        &amp;lt;img src="{{row.image.url}}" alt="" /&amp;gt;
        &amp;lt;h2&amp;gt;{{ row.hs_name }}&amp;lt;/h2&amp;gt;
        &amp;lt;p&amp;gt;{{row.tags}}&amp;lt;/p&amp;gt;
      &amp;lt;/a&amp;gt;
      {% endfor %}
    &amp;lt;/div&amp;gt;
    {% endif %}

    {% if dynamic_page_route_level == 1 %}
    &amp;lt;h1&amp;gt;Gallery - 1&amp;lt;/h1&amp;gt;&amp;lt;!-- For tracking purpose - remove for production --&amp;gt;
    &amp;lt;h2&amp;gt;{{dynamic_page_hubdb_row.hs_name}}&amp;lt;/h2&amp;gt;
    {% set rows = hubdb_table_rows(dynamic_page_hubdb_row.hs_child_table_id) %}
    &amp;lt;ul class="grid"&amp;gt;
      {% for row in rows %}
      &amp;lt;a class="gallery-1 gallery-card" href="{{ request.path }}/{{ row.hs_path }}" data-filter="{{row.tags}}"&amp;gt;
        &amp;lt;img src="{{row.image.url}}" alt="" /&amp;gt;
        &amp;lt;h2&amp;gt;{{ row.hs_name }}&amp;lt;/h2&amp;gt;
        &amp;lt;p&amp;gt;{{row.tags}}&amp;lt;/p&amp;gt;
      &amp;lt;/a&amp;gt;
      {% endfor %}
    &amp;lt;/ul&amp;gt;
    {% endif %}

    &amp;lt;script&amp;gt;
      $(document).ready(function() {
        var filterValues = [];

        // Get all filters and place into an array
        function getAllFilters() {
          var returnArray = [];
          $('.gallery-card').each( function() {
            var c = $(this).data('filter').split(' ');
            console.log(c);
            returnArray = returnArray.concat(c).unique(); ;
          });
          return returnArray;
        }
        // Prevent duplicates in the filters
        Array.prototype.unique = function() {
          var a = this.concat();
          for(var i=0; i&amp;lt;a.length; ++i) {
            for(var j=i+1; j&amp;lt;a.length; ++j) {
              if(a[i] === a[j])
                a.splice(j--, 1);
            }
          }
          return a;
        };

        // Populate the filters in to the form
        function popFilters(filArray) {
          for (i=0; i&amp;lt;filArray.length; i++) {
            var checkbox = '&amp;lt;label&amp;gt;&amp;lt;input type="checkbox" value="' + filArray[i] + '"&amp;gt;' + filArray[i] + '&amp;lt;/label&amp;gt;';
            $('[name="filters-form"]').append(checkbox);
          }
        }

        // Initialize the filters
        popFilters(getAllFilters());

        // Watch the filters form for changes
        $('form[name="filters-form"]').change(function () {
          // collect array of all "checked" checkboxes
          //var appliedFilters = getFiltersForm();
          // pass array into  filter function
          // function hides and shwos based on arry
          updateUi( getFiltersForm() );
        });

        // Collect the checked filters
        function getFiltersForm() {
          var filters = [];
          $('form[name="filters-form"] input[type="checkbox"]:checked').each( function() {
            var value =$(this).val();
            filters.push(value);
            console.log('filters = ' + filters);
          });
          return filters;
        }

        // Check if values are in an array
        function isInArray(needles, haystack){ 
          for(var i = 0 , len = needles.length; i &amp;lt; len; i++){
            if($.inArray(needles[i], haystack) == -1) return false;
          }
          return true;
        }

        // Update the gallery the UI
        function updateUi(b) {
          if (b.length &amp;lt;=0) {
            $('.gallery-card').show();
          } else {
            $('.gallery-card').each( function() {
              var a = $(this).data('filter').split(' ');
              console.log(isInArray(b,a));
              if ( isInArray(b,a) != true ) {
                $(this).hide();
              } else if ( isInArray(b,a) == true ) {
                $(this).show();
              }
            });
          }
        }

        // NEXT STEPS
        // Add querystring recognition and history API states

      });
    &amp;lt;/script&amp;gt;

    {{ standard_footer_includes }}
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;A few things to note:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;- &lt;/STRONG&gt;I'm pretty sure this POC uses an Additive Filtering model, it's been some time since I've been in it.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;-&lt;/STRONG&gt; This was also built for a multi level UI (terminology?), meaning it used nested database tables.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;-&lt;/STRONG&gt; This uses the html markup as the data source…&lt;EM&gt;slaps self on the wrist&lt;/EM&gt;. This would ideally use JSON produced by HUBL and opperate more like a true micro SPA.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;-&lt;/STRONG&gt; My POC did not have filter categories as your UI shows, but I'm positive with a little logic this can be solved easily.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;-&lt;/STRONG&gt; The plan for this piece when in production was to do 2 additional things that aren't included here: &lt;STRONG&gt;1)&lt;/STRONG&gt; Use the history api so the user can use the back and forward&amp;nbsp; buttons. &lt;STRONG&gt;2)&lt;/STRONG&gt; Use a query string to allow deep linking.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;EDIT:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;After rereading the initiasl post I'm not positive this was a solution for you, but I hope it at least gets you going in the right direction.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 13:07:13 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Filtering-HubDB-elements-with-HTML-Checkboxes/m-p/298800#M14106</guid>
      <dc:creator>Kevin-C</dc:creator>
      <dc:date>2019-10-25T13:07:13Z</dc:date>
    </item>
    <item>
      <title>Re: Filtering HubDB elements with HTML Checkboxes</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Filtering-HubDB-elements-with-HTML-Checkboxes/m-p/299576#M14159</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/73772"&gt;@Kevin-C&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Thanks a lot for sharing this!&lt;/P&gt;&lt;P&gt;While I can't directly apply this, it's really useful to look at your logic and try to extrapolate from here.&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Andrei&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2019 16:30:08 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Filtering-HubDB-elements-with-HTML-Checkboxes/m-p/299576#M14159</guid>
      <dc:creator>Aaaaandrei</dc:creator>
      <dc:date>2019-10-30T16:30:08Z</dc:date>
    </item>
  </channel>
</rss>

