gpt4 book ai didi

javascript - knockout : Make nested sortable automatically expand when adding a child

转载 作者:行者123 更新时间:2023-12-03 08:04:55 25 4
gpt4 key购买 nike

在附加的示例中,我有一个能够显示树结构的嵌套排序。

目标是在添加新子项时使结构扩展以使更改可见。

添加新项目时,函数会自动扩展结构,但它仅在添加第二个子项后才扩展,它应该在添加第一个子项后立即扩展。

模板可能有问题,或者一个简单的 jQuery+CSS 技巧可以解决问题,但我找不到合适的。

function Node(data) {
var self = this;
typeof data != 'undefined' ? self.id = data.id : self.id = '1';
self.parent = ko.observable();
self.children = ko.observableArray();
self.addNode = function() {
var child = new Node({
'id': self.id + '.' + (self.children().length + 1)
});
child.parent(self);
self.children.push(child);
return child;
}
};
var tree = new Node();
var child1 = tree.addNode();
var child2 = tree.addNode();
var viewModel = function() {
this.tree = ko.observable(tree);
this.addChild = function(node, event) {
var self = this;
node.addNode()
var $parent = $(event.target).parent().parent();
if ($parent.prop('tagName') == 'LI') {
if (!$parent
.hasClass('mjs-nestedSortable-expanded')) {
$parent
.addClass('mjs-nestedSortable-expanded');
}
if ($parent
.hasClass('mjs-nestedSortable-collapsed')) {
$parent
.removeClass('mjs-nestedSortable-collapsed');
}

}
}
};
ko.applyBindings(new viewModel());

$('.sortable')
.nestedSortable({
startCollapsed: true
});
ol.sortable,
ol.sortable ol {
margin: 0 0 0 25px;
padding: 0;
list-style-type: none;
}
ol.sortable {
margin: 4em 0;
}
.sortable li {
margin: 5px 0 0 0;
padding: 0;
}
.sortable li div {
border: 1px solid #d4d4d4;
cursor: move;
}
.sortable .disclose {
cursor: pointer;
width: 10px;
display: none;
}
.sortable li.mjs-nestedSortable-collapsed>ol {
display: none;
}
.sortable li.mjs-nestedSortable-branch>div>.disclose {
display: inline-block;
}
.sortable li.mjs-nestedSortable-collapsed>div>.disclose>span:before {
content: '+ ';
}
.sortable li.mjs-nestedSortable-expanded>div>.disclose>span:before {
content: '- ';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script>
<script src="https://cdn.rawgit.com/furf/jquery-ui-touch-punch/master/jquery.ui.touch-punch.min.js"></script>
<script src="https://cdn.rawgit.com/mjsarfatti/nestedSortable/master/jquery.mjs.nestedSortable.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<div class="dd" data-bind="template: {name:'nodeTemplate', data: tree}"></div>
<script id='nodeTemplate' type='text/html'>
<div>
<span class="disclose"><span></span></span>
<span data-bind="text: id"></span>
<a href="" data-bind="click: $root.addChild">Add child</a>
</div>
<ol class="sortable ui-sortable" data-bind="foreach: { data: children, as: 'child' }">
<!-- ko if: child.children().length > 0 -->
<li class="mjs-nestedSortable-collapsed mjs-nestedSortable-branch" data-bind="template: {name:'nodeTemplate', data: child}, attr: { 'data-id': child.id }">
</li>
<!-- /ko -->
<!-- ko if: child.children().length == 0 -->
<li class="mjs-nestedSortable-leaf" data-bind="template: {name:'nodeTemplate', data: child}, attr: { 'data-id': child.id}">
</li>
<!-- /ko -->
</ol>
</script>

最佳答案

我不知道为什么,但在第一次调用 AddChild 时,您丢失了对父元素的引用。您可以替换 knockout 代码:

var $parent = $(event.target).parent().parent();

jQuery 解决方法:

var $parent = $('.dd').find('*').filter(function() {
return $(this).text() === node.id;
}).parent().parent();

修改后的代码片段:

function Node(data) {
var self = this;
typeof data != 'undefined' ? self.id = data.id : self.id = '1';
self.parent = ko.observable();
self.children = ko.observableArray();
self.addNode = function() {
var child = new Node({
'id': self.id + '.' + (self.children().length + 1)
});
child.parent(self);
self.children.push(child);
return child;
}
};
var tree = new Node();
var child1 = tree.addNode();
var child2 = tree.addNode();
var viewModel = function() {
this.tree = ko.observable(tree);
this.addChild = function(node, event) {
var self = this;
node.addNode()

var $parent = $('.dd').find('*').filter(function() {
return $(this).text() === node.id;
}).parent().parent();

if ($parent.prop('tagName') == 'LI') {
if (!$parent
.hasClass('mjs-nestedSortable-expanded')) {
$parent
.addClass('mjs-nestedSortable-expanded');
}
if ($parent
.hasClass('mjs-nestedSortable-collapsed')) {
$parent
.removeClass('mjs-nestedSortable-collapsed');
}

}
}
};
ko.applyBindings(new viewModel());

$('.sortable')
.nestedSortable({
startCollapsed: true
});
ol.sortable,
ol.sortable ol {
margin: 0 0 0 25px;
padding: 0;
list-style-type: none;
}
ol.sortable {
margin: 4em 0;
}
.sortable li {
margin: 5px 0 0 0;
padding: 0;
}
.sortable li div {
border: 1px solid #d4d4d4;
cursor: move;
}
.sortable .disclose {
cursor: pointer;
width: 10px;
display: none;
}
.sortable li.mjs-nestedSortable-collapsed>ol {
display: none;
}
.sortable li.mjs-nestedSortable-branch>div>.disclose {
display: inline-block;
}
.sortable li.mjs-nestedSortable-collapsed>div>.disclose>span:before {
content: '+ ';
}
.sortable li.mjs-nestedSortable-expanded>div>.disclose>span:before {
content: '- ';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script>
<script src="https://cdn.rawgit.com/furf/jquery-ui-touch-punch/master/jquery.ui.touch-punch.min.js"></script>
<script src="https://cdn.rawgit.com/mjsarfatti/nestedSortable/master/jquery.mjs.nestedSortable.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<div class="dd" data-bind="template: {name:'nodeTemplate', data: tree}"></div>
<script id='nodeTemplate' type='text/html'>
<div>
<span class="disclose"><span></span></span>
<span data-bind="text: id"></span>
<a href="" data-bind="click: $root.addChild">Add child</a>
</div>
<ol class="sortable ui-sortable" data-bind="foreach: { data: children, as: 'child' }">
<!-- ko if: child.children().length > 0 -->
<li class="mjs-nestedSortable-collapsed mjs-nestedSortable-branch" data-bind="template: {name:'nodeTemplate', data: child}, attr: { 'data-id': child.id }">
</li>
<!-- /ko -->
<!-- ko if: child.children().length == 0 -->
<li class="mjs-nestedSortable-leaf" data-bind="template: {name:'nodeTemplate', data: child}, attr: { 'data-id': child.id}">
</li>
<!-- /ko -->
</ol>
</script>

关于javascript - knockout : Make nested sortable automatically expand when adding a child,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34424283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com