Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Need Help With Tree View Actions

ds_tx

Coder
I have a Tree View created with HTML and CSS based on design from W3 Schools that I use in MediaWiki for Page navigation.
I load Tree View on Pages from a MediaWiki Template.
Javascript for Tree View is copied to MediaWiki:Common.js that loads for every Page.

By default, Tree View is closed when loaded on Page, and must be manually opened to active page, which is bold.
Is there a way to modify Javascript so that on Page load, Tree View opens to location of active Page within Tree View?
 
Using the example on W3Schools, because you didn't give us code, I just added
Code:
for (i = 0; i < toggler.length; i++) {
toggler[i].parentElement.querySelector(".nested").classList.toggle("active");
toggler[i].classList.toggle("caret-down");
};

So the whole page would be:
Code:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
ul, #myUL {
list-style-type: none;
}
#myUL {
margin: 0;
padding: 0;
}
.caret {
cursor: pointer;
-webkit-user-select: none; /* Safari 3.1+ */
-moz-user-select: none; /* Firefox 2+ */
-ms-user-select: none; /* IE 10+ */
user-select: none;
}
.caret::before {
content: "\25B6";
color: black;
display: inline-block;
margin-right: 6px;
}
.caret-down::before {
-ms-transform: rotate(90deg); /* IE 9 */
-webkit-transform: rotate(90deg); /* Safari */'
transform: rotate(90deg); 
}
.nested {
display: none;
}
.active {
display: block;
}
</style>
</head>
<element onload="myScript">
<h2>Tree View</h2>
<p>A tree view represents a hierarchical view of information, where each item can have a number of subitems.</p>
<p>Click on the arrow(s) to open or close the tree branches.</p>
<ul id="myUL">
<li><span class="caret">Beverages</span>
<ul class="nested">
    <li>Water</li>
    <li>Coffee</li>
    <li><span class="caret">Tea</span>
    <ul class="nested">
        <li>Black Tea</li>
        <li>White Tea</li>
        <li><span class="caret">Green Tea</span>
        <ul class="nested">
            <li>Sencha</li>
            <li>Gyokuro</li>
            <li>Matcha</li>
            <li>Pi Lo Chun</li>
        </ul>
        </li>
    </ul>
    </li> 
</ul>
</li>
</ul>
<script>
var toggler = document.getElementsByClassName("caret");
var i;
for (i = 0; i < toggler.length; i++) {
toggler[i].addEventListener("click", function() {
this.parentElement.querySelector(".nested").classList.toggle("active");
this.classList.toggle("caret-down");
});
}
for (i = 0; i < toggler.length; i++) {
toggler[i].parentElement.querySelector(".nested").classList.toggle("active");
toggler[i].classList.toggle("caret-down");
};
</script>
</body>
</html>
 
That forces Tree View completely open, each and every category, subcategory, etc. on each and every page.
You said you added JS, so I added your JS in addition to W3Schools JS.
I also replaced W3Schools JS with your JS, but both work the same - completely expand Tree View from top to bottom category.

ALSO - Carats completely disappear, so there's no way to open close categories, subcategories, etc.
I tried removing CSS but could never identify exactly what was causing isssue.
I put W3Schools code back the way it was, and it works now, but would like a way for users to know where in the Tree they are.

Here's what I'm looking for if possible (See W3Schools example) -

Beverages page opens with Beverages menu NOT expanded and Beverages active (bold, that's the way it is now).

Beverages menu expands to Beverages > Water Coffee Tea
Clicking on Coffee opens Coffee page with Beverages menu expanded only to subcategories Beverages > Water Coffee Tea with Coffee active (bolded)

From Coffee page, Tea subcategory exands to Beverages > Water Coffee Tea > BlackTea WhiteTea GreenTea
Clicking on Black Tea opens Black Tea page with menu expanded Beverages > Water Coffee Tea > BlackTea WhiteTea GreenTea, with BlackTea active (bolded)

Expanding Green Tea and clicking on Matcha opens Matcha page with Beverages menu expanded - Beverages > Water Coffee Tea > BlackTea WhiteTea GreenTea > Sencha Gyokuro Matcha PiLoChun with Matcha active (bolded)
 
Last edited:
The problem may be with the Script.
MediaWiki does not work with Body and Head HTML or Script tags.
I can only call a Template (like a php include so to speak) and Javascript.
My Template loads DIV with the nested list.
I can add JS to Common.js page/file that will load JS for every page.
And CSS to Common.css page/file that loads CSS for every page.
That's it.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom