JsTree:显示更多选项

Jey*_*ari 15 html javascript css jquery jstree

有什么办法可以进入show more选项/链接jsTree吗?我只想显示部分孩子,并且有一个链接可以扩展以显示所有元素。我尝试了一些Google搜索,但找不到解决方案。任何帮助/提示都将是有用的。

假设parent-1有10个子节点,而Main Root有5个父节点。

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
Run Code Online (Sandbox Code Playgroud)

我正在尝试实现以下结果

在此处输入图片说明

这可能吗?是否有可用的插件?是否有其他选择jsTree可以支持这一点?

Rac*_*len 6

您可以可行地使用classes / childElement count来工作。根据要显示的内容,可以使用id / classs保留要保持可见的元素,或者可以选择显示/隐藏。相同的原理可以应用于子元素(例如列表中的列表项)和更高的父元素,因此一旦拥有第一个功能,就可以轻松对其进行调整。

我在这里包括了一个列表示例,以使您有个主意(它是纯JavaScript的,您可以稍作调整。。)Jquery可以使js更短,但是了解我认为是香草一直很好。

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";
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)
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;
}*/
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

这是代码中包含小提琴链接(带有主体“父”元素)。(注意:即使在<br>[divs之间使用]时,一个标签也将被视为顶级元素。.我想如果要计算不同类型的“父级”,按tagName(或className)进行计数将是最有用的。元素(相对于计数body的子代)。小提琴中包含了两个示例(body的子代和getElementsByTagName)。

希望这可以帮助


小智 5

您是否尝试过使用“ before_open.jstree”事件向树显示所需的方式?请参见示例(我使用jstree网站中的演示页面的一部分):

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>jstree basic demos</title>
	<style>
	html { margin:0; padding:0; font-size:62.5%; }
	body { max-width:800px; min-width:300px; margin:0 auto; padding:20px 10px; font-size:14px; font-size:1.4em; }
	h1 { font-size:1.8em; }
	.demo { overflow:auto; border:1px solid silver; min-height:100px; }
	</style>
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
</head>
<body>
	<h1>Interaction and events demo</h1>
	<button id="evts_button">select node with id 1</button> <em>either click the button or a node in the tree</em>
	<div id="evts" class="demo"></div>

	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
	
	<script>
	
	// interaction and events
	$('#evts_button').on("click", function () {
		var instance = $('#evts').jstree(true);
		instance.deselect_all();
		instance.select_node('1');
	});
	$('#evts')
		.on("changed.jstree", function (e, data) {
			if(data.selected.length) {
				alert('The selected node is: ' + data.instance.get_node(data.selected[0]).text);
			}
		})
		.on("before_open.jstree", function (e, data,a,b,c,d) {
			$('#' + data.node.id + ' ul li:nth-child(n + 2)').hide();
			var str = '<li class="jstree-node  jstree-leaf jstree-last" role="treeitem">press to show ' + $('#' + data.node.id + ' ul li:nth-child(n + 2)').length + ' more items</li>';
			var li = $(str).on('click', function(a,b,c,d) {$(a.target).parent().find('li').show(); $(a.target).hide()});
			$('#' + data.node.id + ' ul').append(li);
		})
		.jstree({
			'core' : {
				'multiple' : false,
				'data' : [
					{ "text" : "Root node", "children" : [
							{ "text" : "Child node 1", "id" : 1 },
							{ "text" : "Child node 2" }
					]}
				]
			}
		});
	</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

希望对您有所帮助。