metafizzy / isotope

:revolving_hearts: Filter & sort magical layouts
https://isotope.metafizzy.co
11.03k stars 1.42k forks source link

How to use URL hash with selects #1635

Closed ItsBenedict closed 1 year ago

ItsBenedict commented 1 year ago

Hello there and thanks for the plugin!

I'm a JS beginner but with the help of the examples and the documentation my filter is working nicely with WordPress and Custom Posttypes.

Now I need to extend the filters to work with URL hashes. I took a look at this example: https://cdpn.io/pen/debug/vErxXj#filter=.metal

Unfortunately I am using select-filters and with my basic JS knowledge I don't know how to re-write my code in order to achieve this. This is my current code:

JS:

( function ( $ ) {

    var $grid = $('.grid').isotope({

        itemSelector: '.grid-item',
            masonry: {
                columnWidth: '.grid-item',
                percentPosition: true,
                gutter: 30
            }
    });

    // store filter for each group
      var filters = {};

      $('.filters').on('change', '.filters-select', function(){
        var $this = $(this);

        var filterGroup = $this.attr('data-filter-group');

        filters[ filterGroup ] = $this.val();

        var filterValue = concatValues( filters );

        $grid.isotope({ filter: filterValue });

        console.log(filterGroup, filterValue);
      });

      // flatten object by concatting values
      function concatValues( obj ) {
        var value = '';
        for ( var prop in obj ) {
          value += obj[ prop ];
        }
        return value;
      }

} ( jQuery ) )

HTML:

<div class="filters">
    <div class="filters-group">
        <select class="filters-select button-group js-radio-button-group" data-filter-group="locations">
            <option value="*">All Locations</option>
            <?php
                $locations = get_categories('taxonomy=location&type=magazines'); 
                foreach ($locations as $location) {         
                    echo '<option value=".'.$location->slug.'">';
                    echo explode(' ', $location->name)[0] . ' ';
                    echo '</option>';                      
                }
            ?>   
       </select>
        <select class="filters-select button-group js-radio-button-group" data-filter-group="cats">
            <option value="*">All Topics</option>
             <?php
                $topics = get_categories('taxonomy=topics&type=magazines'); 
                foreach ($topics as $topic) {
                    echo ' <option value=".'.$topic->slug.'">'.$topic->name.'</option>';
                }
            ?>       
       </select>
    </div>    
</div>

Thanks for any help! :)

thesublimeobject commented 1 year ago

@ItsBenedict — It's hard to tell for sure, but I assume you checkout this example for Filtering Hashes? I"m not sure what you mean by "using selects"? Do you just mean that the HTML element you're using is a <select> element?

ItsBenedict commented 1 year ago

Yes, I've looked at the example but my code is different since I am using