Hi Developers,
I have really been working on trying to find a solution to filter my HubDB data by using checkboxes. I am trying to learn HubL so I wanted to try to find a way to do this using pure HubL, but in all the posts I have read it seems the best suggestions say to pull the data in and filter it with JQuery. So, now I am stuck and hope there is someone who can help explain my next steps. (I am just very rough with the syntax)
I began by pulling the checkbox options in from two different columns in HubDB.
<div class="resources-wrap">
<h3>Available Resources</h3>
<div class="subject-wrap">
<form>
<h3>Filter Resources by Subject</h3>
{# Loop the checkbox options for the subject_en column #}
{% set types = hubdb_table_column(1234567, "subject_en").options %}
{% for choice in types %}
{% set type_list = type_list~choice.id|list %}
<input type="checkbox" id="{{choice.name}}" name="re-subject2" value="{{choice.name|lower}}">
<label for="{{choice.name}}"> {{choice.name}}</label><br>
{% endfor %}
</form>
</div>
<div class="type-wrap">
<form>
<h3>Filter by Type</h3>
{# Loop the checkbox options for the type_en column #}
{% set types = hubdb_table_column(1234567, "type_en").options %}
{% for choice in types %}
{% set type_list = type_list~choice.id|list %}
<input type="checkbox" id="{{choice.name}}" name="re-type" value="{{choice.name|lower}}">
<label for="{{choice.name}}"> {{choice.name}}</label><br>
{% endfor %}
</form>
</div>
<br>
<hr>
Next I hand coded the results to filter (This is where I need help)
I want to loop through the columns and get a list of available data-categories (see how data category shows all the row values by name)
example: data-category=“articles arts language la-english africa”>
Q: How can I get this list of categories from multiple columns and put them in the data-category value?
<div class="resources">
<div class="resource" data-id="resource-card-1" data-category="articles arts language la-english africa">Resource Card 1</div>
<div class="resource" data-id="resource-card-2" data-category="blogposts geography literature culture africa la-french europe">Resource Card 2</div>
<div class="resource" data-id="resource-card-3" data-category="lessonplans geography spanish africa europe asia">Resource Card 3</div>
<div class="resource" data-id="resource-card-4" data-category="maps geography arts la-english europe">Resource Card 4</div>
<div class="resource" data-id="resource-card-5" data-category="podcasts geography stem la-french asia">Resource Card 5</div>
<div class="resource" data-id="resource-card-6" data-category="quizzes culture asia">Resource Card 6</div>
<div class="resource" data-id="resource-card-7" data-category="tools geography language explore-the-city australia">Resource Card 7</div>
<div class="resource" data-id="resource-card-8" data-category="videos arts la-french geography north-america">Resource Card 8</div>
<div class="resource" data-id="resource-card-9" data-category="viralvisits history literature la-french french north-america">Resource Card 9</div>
<div class="resource" data-id="resource-card-10" data-category="workshops arts la-french north-america">Resource Card 10</div>
<div class="resource" data-id="resource-card-11" data-category="maps arts spanish la-english north-america">Resource Card 11</div>
<div class="resource" data-id="resource-card-12" data-category="articles french stem la-english south-america">Resource Card 12</div>
<div class="resource" data-id="resource-card-13" data-category="tools explore-the-city la-french south-america">Resource Card 13</div>
<div class="resource" data-id="resource-card-14" data-category="videos geography la-french antarctica">Resource Card 14</div>
</div>
And finally, Here is the JQuery code that I have used to show and hide the Resource Cards.
var $filterCheckboxes = $('input[type="checkbox"]');
$filterCheckboxes.on('change', function() {
var selectedFilters = {};
$filterCheckboxes.filter(':checked').each(function() {
if (!selectedFilters.hasOwnProperty(this.name)) {
selectedFilters[this.name] = [];
}
selectedFilters[this.name].push(this.value);
});
// create a collection containing all of the filterable elements
var $filteredResults = $('.resource');
// loop over the selected filter name -> (array) values pairs
$.each(selectedFilters, function(name, filterValues) {
// filter each .resource element
$filteredResults = $filteredResults.filter(function() {
var matched = false,
currentFilterValues = $(this).data('category').split(' ');
// loop over each category value in the current .resource's data-category
$.each(currentFilterValues, function(_, currentFilterValue) {
// if the current category exists in the selected filters array
// set matched to true, and stop looping. as we're ORing in each
// set of filters, we only need to match once
if ($.inArray(currentFilterValue, filterValues) != -1) {
matched = true;
return false;
}
});
// if matched is true the current .resource element is returned
return matched;
});
});
$('.resource').hide().filter($filteredResults).show();
});
//thanks to billyonecan for this code at fiddle http://jsfiddle.net/n3EmN/171/
I feel I am so close to getting this to work. If you could kindly help me over this hurdle, or help guide me it would be greaty appreciated.