UIkit 3's Filter component: Set active filter with URL hash - unable to change active filter with JS

£可爱£侵袭症+ 提交于 2020-04-16 05:08:49

问题


I'm trying to pass a hash to an URL to set a UIkit filter.

<div uk-filter="target:.js-filter">
    <ul>
        <li class="uk-active" uk-filter-control><a href="#">All</a></li>
        <li uk-filter-control="filter:[data-color='blue'];"><a href="#"></a></li>
        <li uk-filter-control="filter:[data-color='white'];"><a href="#"></a></li>
    </ul>
    <ul class="js-filter">
        <li data-color="blue"></li>
        <li data-color="white"></li>
    </ul>
</div>

So, for example, if I go to http://example.com/#white the thing shows only the white items (and the "white" filter control is active).

I couldn't find any clear example about how to achieve this with UIkit 3 (This one is about UIkit 2). Documentation seems unclear for a noob like me, for it's unclear what target and selActive options are refering to. However, I'm trying with the following:

<script>
document.addEventListener("DOMContentLoaded", function(event){
    hash = document.location.hash;
    console.log("Hash: " + hash);

    if ( hash !== "" ) {
        var filter = document.querySelector('.js-filter');

        UIkit.filter( filter, {
            target: 'li [data-color:' + hash + ']',
            selActive: true,
        });
    }
});
</script>

But this throws a "Uncaught TypeError: Cannot read property 'children' of null" error.

Any help would be appreciated. :)


回答1:


<div uk-filter="target:.js-filter">
<ul id="filter-bar">
    <li class="uk-active" uk-filter-control><a href="#">All</a></li>
    <li uk-filter-control="filter:[data-color='blue'];"><a href="#"></a></li>
    <li uk-filter-control="filter:[data-color='white'];"><a href="#"></a></li>
</ul>
<ul class="js-filter">
    <li data-color="blue"></li>
    <li data-color="white"></li>
</ul>

      document.addEventListener("DOMContentLoaded", function(event){
        hash = document.location.hash;
        console.log("Hash: " + hash);

        if ( hash !== "" ) {
            var filter = document.querySelector('.js-filter');
            var filterBar= document.querySelector('#filter-bar');
            var activeFilter= document.querySelector('li [data-color:' + hash + ']');
            UIkit.filter( filterBar, {
                target: filter,
                selActive: activeFilter,
            });
        }
    });

I'm hear too late) But try this code. You need to pass your filter component first not your list.




回答2:


The easiest way to achieve this is to check for hash in url and then find the href tag which filters the content ..then just programaticaly click it.
Complicated (and more proper) way would be to trigger the change like in your code, but as the default filter is applied on document load, you need to make sure to call your script's filter after UIKit's filter.

window.addEventListener("load", function(event) {
  //hash = document.location.hash;
  //console.log("Hash: " + hash);

  //Let's assume the document.location.hash returns #white
  hash = '#white';

  if (hash !== "") {
    //get the color subnav button
    color = document.querySelector('a[href="'+hash+'"]');
    //trigger the click event
    color.click();
  }
});
body {
  background-color: #eee;
}
<!-- UIkit CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-rc.8/css/uikit.min.css" />

<!-- UIkit JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-rc.8/js/uikit.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-rc.8/js/uikit-icons.min.js"></script>


<div data-uk-filter=".js-filter">

  <ul class="uk-subnav uk-subnav-pill">
    <li uk-filter-control><a href="#">All</a></li>
    <li uk-filter-control="[data-color='white']"><a href="#white">White</a></li>
    <li uk-filter-control="[data-color='blue']"><a href="#blue">Blue</a></li>
    <li uk-filter-control="[data-color='black']"><a href="#black">Black</a></li>
  </ul>

  <ul class="js-filter uk-child-width-1-2 uk-child-width-1-3@m uk-text-center" uk-grid>
    <li data-color="white">
      <div class="uk-card uk-card-default uk-card-body">Item</div>
    </li>
    <li data-color="blue">
      <div class="uk-card uk-card-primary uk-card-body">Item</div>
    </li>
    <li data-color="white">
      <div class="uk-card uk-card-default uk-card-body">Item</div>
    </li>
    <li data-color="white">
      <div class="uk-card uk-card-default uk-card-body">Item</div>
    </li>
    <li data-color="black">
      <div class="uk-card uk-card-secondary uk-card-body">Item</div>
    </li>
    <li data-color="black">
      <div class="uk-card uk-card-secondary uk-card-body">Item</div>
    </li>
    <li data-color="blue">
      <div class="uk-card uk-card-primary uk-card-body">Item</div>
    </li>
    <li data-color="black">
      <div class="uk-card uk-card-secondary uk-card-body">Item</div>
    </li>
    <li data-color="blue">
      <div class="uk-card uk-card-primary uk-card-body">Item</div>
    </li>
    <li data-color="white">
      <div class="uk-card uk-card-default uk-card-body">Item</div>
    </li>
    <li data-color="blue">
      <div class="uk-card uk-card-primary uk-card-body">Item</div>
    </li>
    <li data-color="black">
      <div class="uk-card uk-card-secondary uk-card-body">Item</div>
    </li>
  </ul>

</div>


来源:https://stackoverflow.com/questions/51250765/uikit-3s-filter-component-set-active-filter-with-url-hash-unable-to-change-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!