Expand JsTree Options: View Additional Choices

Can a "show more" option/link be implemented in jsTree? I am looking to display only a portion of the child elements with an option to expand and show all. Despite searching online, I have not been able to find a solution. Any assistance or suggestions would be greatly appreciated.

For example, let's consider that parent-1 has 10 child nodes and the Main Root consists of 5 parent nodes.

Main Parent
    - Parent - 1
        - child-1
        - child-2
        - child-3
        show 7 more parent-1 elements //LINK
    - Parent - 2
        - child-1
        - child-2
    - Parent - 3
        - child-1
        - child-2
    show 2 more Main Parent elements //LINK

The desired outcome is illustrated in the following image:

https://i.sstatic.net/DUSWC.png

Is it feasible to achieve this? Are there existing plugins that facilitate this functionality within jsTree? Alternatively, are there other tree visualization libraries that offer support for such features?

Answer №1

You can possibly utilize classes/childElement count to achieve this. Depending on your display needs, you can use ids/classes to keep certain elements visible or toggle show/hide options. This concept can be applied to child elements (such as list items in a list) and their parent elements, making it easily adaptable once the initial function is created.

Below is an example using a list to illustrate the concept (written in plain javascript and open for adjustments...). While Jquery may make the code shorter, knowing vanilla JavaScript is always advantageous in my opinion.

var x = document.getElementById("show");
//var root = document.getElementsByTagName('body')[0];
var count = x.childElementCount;
var btn1 = document.getElementById("ulmore");
btn1.innerHTML = "+ Show all " + count + " li children";
document.getElementById("ulmore").addEventListener("click", showmore);

function showmore() {
  //var d = document.getElementsByClassName("tg1").length;
  //use d if you want to show "show d more children" instead of the full amount of children
  var el = document.getElementsByTagName("li");

  var i;
  for (i = 0; i < el.length; i++) {
    if (el[i].classList.contains("tg1")) {
      //el[i].style.display = "block"; works
      el[i].classList.toggle('more');
      if (el[i].classList.contains("more")) {
        btn1.innerText = "Hide children";
      } else {
        btn1.innerText = "+ Show all " + count + " li children";
      }
    }
  }
}
body {
  padding: 1em;
  font-size: 10pt;
}

ul {
  list-style: none;
  width: 10em;
  padding: 0px;
}

li {
  text-align: left;
  line-height: 1.5;
  background: lightblue;
  border: 1px solid #444;
  margin: 1px 0px;
  padding: 2px;
}

span {
  display: inline-block;
}

div {
  display: inline-block;
  width: 100%;
  float: left;
  margin: 1em 2em 1em 0em;
}

.tg1 {
  background: lightsteelblue;
  display: none;
}

.more {
  background: lightsteelblue;
  display: block;
}

/*.tog2 {
  display: none;
}

.grow {
  background: yellow;
  display: inline-block;
}*/
<body>
  <div id="ulbit">
    <h4>Demo List</h4>
    <ul id="show">
      <li>One</li>
      <li>Two</li>
      <li class="tg1">Three</li>
      <li class="tg1">Four</li>
      <li class="tg1">Five</li>
      <li class="tg1">Six</li>
      <li class="tg1">Seven</li>
      <li class="tg1">Eight</li>
    </ul>
    <span id="ulmore"></span>
  </div>

<!--  <div class="tog2">
    Div 2 hello
  </div>
  <div class="tog2">
    Div 3
  </div>
  <span id="divmore"></span>-->
</body>

For a practical demonstration with body 'parent' elements included, here's a fiddle link within the code. (Note: even a <br> tag [when used between divs] will be considered a top-level element.. Counting by tagName (or className) would likely be most helpful if you need to count various types of 'parent' elements (instead of just body children). Examples of both methods are provided in the fiddle)

I hope this explanation proves useful

Answer №2

If you haven't already, consider utilizing the "before_open.jstree" event to customize how the tree is displayed. Here's an example showcasing part of the demo page from the jstree site:

<!DOCTYPE html>
<html lang="en>
<!-- Rest of the HTML code here -->

Give it a try and see if it suits your needs!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Change the color of the navbar after scrolling past 70% of the viewport height using

I'm exploring the idea of changing the color scheme of my navbar when it reaches 70% of the viewport height (vh). My current thought is to use an @media query to apply an invert filter to the nav container and logo. The syntax might look something li ...

Bluebird refuses to execute the then() function, despite the code that was functional before

I am in the process of constructing a node framework for my upcoming projects, with a focus on easy management. The framework already includes a configuration module. Recently, I implemented an error handler and made updates to the Config module to incorp ...

Having trouble with connecting to Gmail while using Nodemailer? Facing a Connection Timeout

Seeking assistance with sending emails through nodemailer. See below for the code snippet. var transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: '<<a href="/cdn-cgi/l/email-protecti ...

Having trouble getting JQuery UI Tabs to display properly in an HTML5 environment?

I've been working with JQuery UI to create a tabbed layout. Here's the code that I have so far: <!doctype html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="css/jquery-ui.min.css"/> <link rel ...

Information is only displayed within the Node Request function and is not accessible to

I am currently developing a small web scraping tool using Node (Express) to search URLs from a list. However, I'm encountering an issue with accessing the results of the search outside of the request callback function in a forEach loop. Can anyone hel ...

What are some ways to optimize this scrolling event in jQuery that seems to be triggering excessively?

Although this code gets the job done, it is quite basic and has some issues with scrolling that need to be optimized. Can someone provide assistance in improving the scrolling event? Example: http://codepen.io/rootion/pen/gpZZpG Structure and desired out ...

Observing the completion of a subscriber function

Is there a more streamlined way to determine if the subscriber has finished executing or return something and catch it up-stream? Consider the following code snippets: this._subscriptions.push(this._client .getCommandStream(this._command) // R ...

Utilize identical code across various jQuery instances

I'm having trouble using the same script in two different places with another div. Below that, I need to use the same script for two different types of filters. I tried copying the script and changing the class name, but it's still not working. ...

Let's dive into the process of creating an express.js server!

I am facing an error on Replit while setting up my Express server. I am a beginner in learning Express, so some things are still unclear to me. /home/runner/log-in-API/node_modules/express/lib/router/index.js:513 this.stack.push(layer); ^ ...

Tips for changing the first letter to uppercase in a p5.js variable

I'm currently working on developing a weather forecasting website using the p5.js framework in JavaScript. One issue I am facing is that the API I am utilizing only provides weather descriptions in lowercase format, whereas I want them to be displayed ...

Creating a dynamic name in TypeScript for an object to be utilized in an onClick event

If I have a TypeScript object declared in my view like this: <script type="text/javascript"> var myTSObject = Module.CustomClass('#someId'); myISObject.bind(); </script> Now, if I need to manage a click event from within the ...

How to send form group in Angular when the enter key is pressed

When I press the submit button on a form, it sends a request to the database to filter data in a grid. However, I also want the form to submit when the enter key is pressed. HTML <form [formGroup]="bmForm" (keyup.enter)="onSearchClic ...

Displaying a variable in HTML using PHP before it's actually declared

I have a webpage named search.php My goal is to customize the title/tab tag of the page to display the number of items found along with "Mysearch". Here's what I want it to look like: <head> <title><?php echo $number_count; ?> it ...

steps for aligning an enlarged div in the middle upon hovering?

This situation presents a unique challenge. Below is the HTML code. <div class="foo"> <div class="bar"> <div class="a"> <h2>Luigi's Mansion1</h2> <small> ...

Utilizing Material UI Grid spacing in ReactJS

I'm encountering an issue with Material UI grid. Whenever I increase the spacing above 0, the Grid does not fit the screen properly and a bottom slider is visible, allowing me to move the page horizontally slightly. Here is the simplified code snippe ...

What circumstances result in async methods throwing errors and how can you effectively handle them?

According to the documentation for Node.js: Sometimes, even with asynchronous methods in the Node.js API, exceptions may be thrown using the throw mechanism. To handle such exceptions, you need to use try/catch blocks. There isn't a complete list o ...

How can I hide selected options in GraphQL playground?

I'm facing an issue in the playground with my view. When I try to add another option to select, pressing CTRL + space shows me every possible option, including the ones already selected. Is there a way to prevent this behavior? I only want to see the ...

The .htaccess directive aimed at eliminating .html from the URL appears to be malfunctioning

Struggling to remove the .html extension from your page URLs? I've been trying various methods with the .htaccess file, but nothing seems to be working. I created an .htaccess file and placed it in the public_html directory. The code snippet I'm ...

What could be the reason behind receiving an "undefined" message when attempting to access db.collection in the provided code snippet?

var express = require('express'); var GoogleUrl = require('google-url'); var favicon = require('serve-favicon'); var mongo = require('mongodb').MongoClient; var app = express(); var db; var googleUrl = new GoogleUrl( ...

Is there a way to restrict my input to only 10 numbers without allowing any characters?

Is there a way to restrict the phone number input to only allow 10 numbers and exclude any other characters? Currently, setting the maxLength attribute limits the characters to 10 but allows additional characters as well. If I change the type attribute to ...