Jeff S
Jeff S

Reputation: 109

How to filter an image gallery using url parameters

I'm creating a commerce site for my portfolio and I have a page with gallery of product cards that I would like to add filters for. The cards were generated dynamically using a json file.

I can figure this out using just javascript and CSS but in reviewing other sites I noticed that then you trigger their filters, parameters are appended to or removed from the url.

Here's my javascript for this page. As you can see I already use a function that takes you to an individual item by appending to the url, but I can't figure out how this is done for filters.

// ========== Load Gallery Images ========================================
$.getJSON("../../json/guitars.json", function(data) {

  var guitar_data = '';

  for(var i in data) {
    // data
    var image1 = data[i].img1
    var alt = data[i].alt
    var title = data[i].title // use for sort-by
    var price = data[i].price

    // filters
    var id = data[i].product_id
    var category = data[i].category
    var brand = data[i].brand
    var condition = data[i].condition

    guitar_data += '<div class="gallery-card">'
    guitar_data += `<img class="more-info img-fluid" src="../${image1}" alt="${alt}">`
    guitar_data += `<h6>${title}</h6>`
    guitar_data += `<p><strong>${price}</strong></p>`
    guitar_data += '</div>'
  }

  $('#img-gallery').html(guitar_data)
})
// ========== Load Gallery Images End ====================================

$(document).ready(function() {
  // ======== More Info ==================================================
  $(".more-info").on("click", function(e) {

    const product_id = $(e.target).attr('product_id')

    $.getJSON("../../json/guitars.json", function(json) {
      for(let i in json) {
        if (product_id === json[i].product_id ) {
          window.location.href = "./product.html?product=" + product_id
        }
      }
    })
  })
  // ======== More Info End ===============================================

Here is one of my filters

<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
                    <div class="card-body" style="border:none;">
                      <ul style="list-style:none;padding-left:5px;">
                        <li>
                          <input type="checkbox" name="brand" value="fender">
                          <label style="padding-left:10px;" for="">Fender</label>
                        </li>
                        <li>
                          <input type="checkbox" name="brand" value="gibson">
                          <label style="padding-left:10px;" for="">Gibson</label>
                        </li>
                        <li>
                          <input type="checkbox" name="brand" value="gretsch">
                          <label style="padding-left:10px;" for="">Gretsch</label>
                        </li>
                      </ul>
                    </div>
                  </div>

I'm not asking for anyone for code, but rather direction. I'm a bit lost at the moment.

Upvotes: 0

Views: 799

Answers (1)

Niloct
Niloct

Reputation: 10015

You can wrap the related html layout in a FORM with method="GET", and you call form's submit() at every filter click.

In this way every selected field would update the URL parameters i.e. querystring.

Upvotes: 1

Related Questions