admin管理员组

文章数量:1310450

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 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. :)

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./#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. :)

Share Improve this question asked Jul 9, 2018 at 17:11 acidrums4acidrums4 2551 gold badge4 silver badges15 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7
<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 ponent first not your list.

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./ajax/libs/uikit/3.0.0-rc.8/css/uikit.min.css" />

<!-- UIkit JS -->
<script src="https://cdnjs.cloudflare./ajax/libs/uikit/3.0.0-rc.8/js/uikit.min.js"></script>
<script src="https://cdnjs.cloudflare./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>

I use the following code to read the hash from URL and trigger the active UIKit filter and also reflect the change to URL if user click other filter button:

document.addEventListener("DOMContentLoaded", function(event) {
    var hash = document.location.hash.substr(1);  // Remove the leading "#" from hash
    console.log("Hash: " + hash);

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

        var activeFilter = Array.from(document.querySelectorAll('[uk-filter-control]')).find(function(el) {
            var rawAttr = el.getAttribute('uk-filter-control');
            if (rawAttr === "") return false;

            try {
                var parsedAttr = JSON.parse(rawAttr);
                
                return parsedAttr.filter && parsedAttr.filter.toLowerCase().includes(`[data-tag~="${hash.toLowerCase()}"]`);
            } catch (e) {
                console.error("JSON parse failed:", e);
                return false;
            }
        });

        if (activeFilter) {
            UIkit.filter(filterBar, {
                target: filter,
                selActive: activeFilter
            });
        }
    }
    
    Array.from(document.querySelectorAll('[uk-filter-control]')).forEach(function(el) {
        el.addEventListener('click', function() {
            var rawAttr = el.getAttribute('uk-filter-control');

            if (rawAttr === "") {
                window.location.hash = "";
                return;
            }
            
            try {
                var parsedAttr = JSON.parse(rawAttr);
                
                if (parsedAttr.filter) {
                    var match = parsedAttr.filter.match(/\[data-tag~="([^"]+)"\]/);
                    
                    if (match && match[1]) {
                        window.location.hash = match[1];
                    }
                }
            } catch (e) {
                console.error("JSON parse failed:", e);
            }
        });
    });
});

本文标签: