我看了一下 jQuery Nestable,并得出了这个解决方案:
$.fn.nestableShow = function() {
return this.each(function () {
var $parents = $(this).parents();
$parents.each(function (i) {
var list = $(this).data("nestable");
if (list) {
$parents.slice(0, i).filter(list.options.itemNodeName).each(function(){
list.expandItem($(this));
});
return false;
}
});
});
};
完成上述操作后,您可以编写以下代码:
$('#step-123').nestableShow();
...并且它将扩展该元素的父元素,使其在树中可见。
一个简单的演示:
// decorate as collapsable tree, and collapse it.
$('.dd').nestable({}).data("nestable").collapseAll();
// Plug-in for exanding the tree to make a given element visible.
$.fn.nestableShow = function() {
return this.each(function () {
var $parents = $(this).parents();
$parents.each(function (i) {
var list = $(this).data("nestable");
if (list) {
$parents.slice(0, i).filter(list.options.itemNodeName).each(function(){
list.expandItem($(this));
});
return false;
}
});
});
};
$('#find').click(function() {
$('#step-123').nestableShow();
});
$('#find2').click(function() {
$('#step-124').nestableShow();
});
.special { color: red }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Nestable/2012-10-15/jquery.nestable.min.js"></script>
<button id="find">find element 123</button>
<button id="find2">find element 124</button>
<br>
<div class="dd" id="nestable">
<ol class="dd-list">
<li class="dd-item"><span>Item 1</span></li>
<li>
<span>Item 2</span>
<ol class="dd-list">
<li class="dd-item"><span>Item 3</span></li>
<li class="dd-item"><span>Item 4</span></li>
<li class="dd-item">
<span>Item 5</span>
<ol class="dd-list">
<li class="dd-item"><span>Item 6</span></li>
<li class="dd-item special" id="step-123"><span>Here is step-123!</span></li>
<li class="dd-item"><span>Item 8</span></li>
</ol>
</li>
<li class="dd-item"><span>Item 9</span></li>
<li class="dd-item"><span>Item 10</span></li>
</ol>
</li>
<li>
<span>Item 11</span>
<ol class="dd-list">
<li class="dd-item"><span>Item 12</span></li>
<li class="dd-item"><span>Item 13</span></li>
<li class="dd-item">
<span>Item 14</span>
<ol class="dd-list">
<li class="dd-item"><span>Item 15</span></li>
<li class="dd-item special" id="step-124"><span>Here is step-124!</span></li>
<li class="dd-item"><span>Item 17</span></li>
</ol>
</li>
<li class="dd-item"><span>Item 18</span></li>
<li class="dd-item"><span>Item 19</span></li>
</ol>
</li>
<li class="dd-item">
<span>Item 20</span>
</li>
<li class="dd-item">
<span>Item 21</span>
</li>
</ol>
</div>