从数据库动态添加可嵌套div内容后出现问题。
nestable with static data按预期工作。我可以从nestable2添加项目,并查看序列化输出中的更改。
nestable with dynamic data 虽然拖放中的拖放似乎仍然有效,但是从nestable2失去了拖放功能。但是,更改不会反映在序列化输出中。
此外,似乎嵌套的Serialized Output的动态数据版本不完整。比较两个链接之间的可嵌套序列化输出显示具有动态数据链接的JSON对象的嵌套在金属类别菜单之后结束,尽管整个菜单显示在该可嵌套div和tshoot innerHTML中的页面上。 / p>
有人能指出我正确的方向来解决这个问题吗?
谢谢!
问题太模糊了?好的,让我添加更多细节。
这是一些从JSON构建可嵌套菜单的代码:
... function buildItem(item){
var html = "<li class='dd-item' data-id='" + item.id + "' id='" + item.id + "'>";
html += "<div class='dd-handle'>" + item.id + "</div>";
if (item.children) {
html += "<ol class='dd-list'>";
$.each(item.children, function (index, sub) {
html += buildItem(sub);
});
html += "</ol>";
}
html += "</li>";
return html;
}
$。each(JSON.parse(obj),function(index,item){
$('#nestable2 ul').append(buildItem(item));
}); ...
我将硬编码版本中的列表与动态版本进行了比较,并注意到良好的硬编码版本以ol标签开头,其中动态版本以ul标签开头(无法在此帖子中为某些人提供html)原因)。所以我将代码更改为
...
function buildItem(item){
var html = "<li class='dd-item' data-id='" + item.id + "'>";
html += "<div class='dd-handle'>" + item.id + "</div>";
if (item.children) {
html += "<ol class='dd-list'>";
$.each(item.children, function (index, sub) {
html += buildItem(sub);
});
html += "</ol>";
}
html += "</li>";
return html;
}
$。each(JSON.parse(obj),function(index,item){
$('#nestable2 ol').append(buildItem(item));
}); ...
并使用ol标记使nestable2 div开始。
这种工作,但现在我得到额外的孩子(例如,塑料,阀门,加热和冷却,库存夹具和杂项附加到金属。 阀门,加热和冷却,库存夹具和杂项附加到塑料和库存夹具,其他附加到加热和冷却。
很明显if(item.children)循环存在问题。
有没有人看到这个问题,也许是一个建议的解决方案?
这是有效的JSON:
[{&#34; ID&#34;:&#34;金属及#34;&#34;的子&#34;:[{&#34; ID&#34;:&#34; Propress&#34 ;,&#34;的子&#34;:[{&#34; ID&#34;:&#34;弯头&#34;&#34;的子&#34;:[{&#34; ID&#34; :&#34; 1/2 90&#34;},{&#34; id&#34;:&#34; 1/2 45&#34;},{&#34; id&#34;:&#34 ; 3/4 90&#34;},{&#34; id&#34;:&#34; 3/4 45&#34;},{&#34; id&#34;:&#34; 1/2 LS 90&#34;},{&#34; id&#34;:&#34; 1/2 ST 45&#34;}]},{&#34; id&#34;:&#34; Tees&#34 ;,&#34;孩子&#34;:[{&#34; id&#34;:&#34; 1/2 x 1/2 x 3/4&#34;},{&#34; id&#34 ;:&#34; 1/2&#34;},{&#34; id&#34;:&#34; 3/4 x 1/2 x 1/2&#34;}]},{&#34 ; ID&#34;:&#34;接箍&#34;}]},{&#34; ID&#34;:&#34;铜&#34;&#34;的子&#34;:[{&# 34; id&#34;:&#34; Elbows&#34;,&#34; children&#34;:[{&#34; id&#34;:&#34; 1/2 90&#34;},{ &#34; id&#34;:&#34; 1/2 45&#34;},{&#34; id&#34;:&#34; 3/4 90&#34;},{&#34; id&#34;:&#34; 3/4 45&#34;},{&#34; id&#34;:&#34; 1/2 LS 90&#34;},{&#34; id&#34 ;:&#34; 1/2 ST 45&#34;}]},{&#34; id&#34;:&#34; Tees&#34;,&#34;儿童&#34;:[{&# 34; id&#34;:&#34; 1/2 x 1/2 x 3/4&#34;},{&#34; id&#34;:&#34; 1/2&#34;},{ &#34; ID&#34;:&#34; 3/4 x 1/2 x 1/2&#34;}]},{&#34; id&#34;:&#34; Couplings&#34;}]},{&#34; id&#34;:&#34 ;黑色&#34;&#34;的子&#34;:[{&#34; ID&#34;:&#34;弯头&#34;&#34;的子&#34;:[{&#34; id&#34;:&#34; 1/2 90&#34;},{&#34; id&#34;:&#34; 1/2 45&#34;},{&#34; id&#34; :&#34; 3/4 90&#34;},{&#34; id&#34;:&#34; 3/4 45&#34;},{&#34; id&#34;:&#34 ; 1/2 LS 90&#34;},{&#34; id&#34;:&#34; 1/2 ST 45&#34;}]},{&#34; id&#34;:&#34 ; T恤&#34;,&#34;孩子&#34;:[{&#34; id&#34;:&#34; 1/2 x 1/2 x 3/4&#34;},{&#34 ; id&#34;:&#34; 1/2&#34;},{&#34; id&#34;:&#34; 3/4 x 1/2 x 1/2&#34;}]}, {&#34; ID&#34;:&#34;接箍&#34;}]},{&#34; ID&#34;:&#34;黄铜&#34;&#34;子&#34 ;: [{&#34; id&#34;:&#34; Elbows&#34;,&#34; children&#34;:[{&#34; id&#34;:&#34; 1/2 90&#34 ;},{&#34; id&#34;:&#34; 1/2 45&#34;},{&#34; id&#34;:&#34; 3/4 90&#34;},{ &#34; id&#34;:&#34; 3/4 45&#34;},{&#34; id&#34;:&#34; 1/2 LS 90&#34;},{&#34 ; id&#34;:&#34; 1/2 ST 45&#34;}]},{&#34; id&#34;:&#34; Tees&#34;,&#34; children&#34;: [{&#34; id&#34;:&#34; 1/2 x 1/2 x 3/4&#34;},{&#34; id&#34;:&#34; 1/2&#34 ;},{&#34; ID&#34;:&#34; 3/4 x 1/2 x 1/2&#34;}]},{&#34; id&#34;:&#34; Couplings&#34;}]},{&#34; id&#34;:&#34 ;铸铁&#34;,&#34;儿童&#34;:[{&#34; id&#34;:&#34;肘&#34;,&#34;儿童&#34;:[{&#34; ; id&#34;:&#34; 1/2 90&#34;},{&#34; id&#34;:&#34; 1/2 45&#34;},{&#34; id&#34 ;:&#34; 3/4 90&#34;},{&#34; id&#34;:&#34; 3/4 45&#34;},{&#34; id&#34;:&# 34; 1/2 LS 90&#34;},{&#34; id&#34;:&#34; 1/2 ST 45&#34;}]},{&#34; id&#34;:&# 34; Tees&#34;,&#34; children&#34;:[{&#34; id&#34;:&#34; 1/2 x 1/2 x 3/4&#34;},{&# 34; id&#34;:&#34; 1/2&#34;},{&#34; id&#34;:&#34; 3/4 x 1/2 x 1/2&#34;}]} ,{&#34; ID&#34;:&#34;接箍&#34;}]},{&#34; ID&#34;:&#34; Megapress&#34;&#34;的子&#34; :[{&#34; id&#34;:&#34; Elbows&#34;,&#34; children&#34;:[{&#34; id&#34;:&#34; 1/2 90&# 34;},{&#34; id&#34;:&#34; 1/2 45&#34;},{&#34; id&#34;:&#34; 3/4 90&#34;}, {&#34; id&#34;:&#34; 3/4 45&#34;},{&#34; id&#34;:&#34; 1/2 LS 90&#34;},{&# 34; id&#34;:&#34; 1/2 ST 45&#34;}]},{&#34; id&#34;:&#34; Tees&#34;,&#34; children&#34; :[{&#34; id&#34;:&#34; 1/2 x 1/2 x 3/4&#34;},{&#34; id&#34;:&#34; 1/2&# 34;},{&#34; ID&#34 ;: &#34; 3/4 x 1/2 x 1/2&#34;}]},{&#34; id&#34;:&#34; Couplings&#34;}]}]},{&#34 ; ID&#34;:&#34;塑料&#34;&#34;的子&#34;:[{&#34; ID&#34;:&#34;管&#34;&#34;的子&# 34;:[{&#34; ID&#34;:&#34; PVC&#34;},{&#34; ID&#34;:&#34; PEX&#34;},{&#34; ID& #34;:&#34; AQUATHERM&#34;}]},{&#34; ID&#34;:&#34; PVC&#34;&#34;的子&#34;:[{&#34; id&#34;:&#34; Elbows&#34;,&#34; children&#34;:[{&#34; id&#34;:&#34; 1/2 90&#34;},{&# 34; id&#34;:&#34; 1/2 45&#34;},{&#34; id&#34;:&#34; 3/4 90&#34;},{&#34; id&# 34;:&#34; 3/4 45&#34;},{&#34; id&#34;:&#34; 1/2 LS 90&#34;},{&#34; id&#34;: &#34; 1/2 ST 45&#34;}]},{&#34; id&#34;:&#34; Tees&#34;,&#34;孩子&#34;:[{&#34; id&#34;:&#34; 1/2 x 1/2 x 3/4&#34;},{&#34; id&#34;:&#34; 1/2&#34;},{&# 34; id&#34;:&#34; 3/4 x 1/2 x 1/2&#34;}]},{&#34; id&#34;:&#34;法兰&#34;},{ &#34; ID&#34;:&#34;接箍&#34;},{&#34; ID&#34;:&#34;帽&#34;},{&#34; ID&#34;:& #34;联盟&#34;},{&#34; ID&#34;:&#34;减速齿轮&#34;},{&#34; ID&#34;:&#34;适配器&#34;},{ &#34; ID&#34;:&#34;阀门与#34;}]},{&#34; ID&#34;:&#34; PEX&#34;&#34;的子&# 34;:[{&#34; ID&#34;:&#34;弯头&#34;&#34;的子&#34;:[{&#34; ID&#34;:&#34; 1/2 90&#34;},{&#34; id&#34;:&#34; 1/2 45&#34;},{&#34; id&#34;:&#34; 3/4 90&#34; },{&#34; id&#34;:&#34; 3/4 45&#34;},{&#34; id&#34;:&#34; 1/2 LS 90&#34;},{ &#34; id&#34;:&#34; 1/2 ST 45&#34;}]},{&#34; id&#34;:&#34; Tees&#34;,&#34; children&# 34;:[{&#34; id&#34;:&#34; 1/2 x 1/2 x 3/4&#34;},{&#34; id&#34;:&#34; 1 / 2&#34;},{&#34; id&#34;:&#34; 3/4 x 1/2 x 1/2&#34;}]},{&#34; id&#34;:&# 34;法兰&#34;},{&#34; ID&#34;:&#34;接箍&#34;},{&#34; ID&#34;:&#34;帽&#34;},{& #34; ID&#34;:&#34;联盟&#34;},{&#34; ID&#34;:&#34;减速齿轮&#34;},{&#34; ID&#34;:&# 34;适配器&#34;},{&#34; ID&#34;:&#34;阀门与#34;}]},{&#34; ID&#34;:&#34; AQUATHERM&#34;,& #34;儿童&#34;:[{&#34; ID&#34;:&#34;弯头&#34;&#34;的子&#34;:[{&#34; ID&#34;:&# 34; 1/2 90&#34;},{&#34; id&#34;:&#34; 1/2 45&#34;},{&#34; id&#34;:&#34; 3 / 4 90&#34;},{&#34; id&#34;:&#34; 3/4 45&#34;},{&#34; id&#34;:&#34; 1/2 LS 90&# 34;},{&#34; id&#34;:&#34; 1/2 ST 45&#34;}]},{&#34; id&#34;:&#34; Tees&#34;,& #34;子&#3 4;:[{&#34; id&#34;:&#34; 1/2 x 1/2 x 3/4&#34;},{&#34; id&#34;:&#34; 1 / 2&#34;},{&#34; id&#34;:&#34; 3/4 x 1/2 x 1/2&#34;}]},{&#34; id&#34;:&# 34;法兰&#34;},{&#34; ID&#34;:&#34;接箍&#34;},{&#34; ID&#34;:&#34;帽&#34;},{& #34; ID&#34;:&#34;联盟&#34;},{&#34; ID&#34;:&#34;减速齿轮&#34;},{&#34; ID&#34;:&# 34;适配器&#34;},{&#34; ID&#34;:&#34;阀门与#34;}]}]},{&#34; ID&#34;:&#34;阀门与#34; },{&#34; id&#34;:&#34;加热和冷却&#34;,&#34;儿童&#34;:[{&#34; id&#34;:&#34; Radiant&#34 ;,&#34;的子&#34;:[{&#34; ID&#34;:&#34;管与#34;&#34;的子&#34;:[{&#34; ID&#34; :&#34; 1/2 PEX&#34;},{&#34; id&#34;:&#34; 3/4 PEX&#34;}]},{&#34; id&#34;:& #34;流形&#34;},{&#34; ID&#34;:&#34;头及#34;},{&#34; ID&#34;:&#34;阀门与#34;},{ &#34; ID&#34;:&#34;接头&#34;}]},{&#34; ID&#34;:&#34;恒温器&#34;},{&#34; ID&#34; :&#34; Waterheaters&#34;},{&#34; ID&#34;:&#34;泵/阀门&#34;},{&#34; ID&#34;:&#34;锅炉&#34 ;},{&#34; id&#34;:&#34; MiniSplits&#34;}]},{&#34; id&#34;:&#34; In Stock Fixtures&#34;},{&# 34; ID&#34;:&#34;弥SC&#34;}]
在新的序列化输出中看到的JSON具有: ...... {&#34; id&#34;:&#34; 1/2 ST 45&#34;},{&#34; id&#34;:&#34;塑料&#34;,&#34;儿童&#34;:[{&#34; ID&#34;:&#34;管&#34; ... 而有效的JSON有: ...... {&#34; id&#34;:&#34; 1/2 ST 45&#34;}]},{&#34; id&#34;:&#34; Tees&#34;,&# 34;孩子&#34;:[{&#34; id&#34;:&#34; 1/2 x 1/2 x 3/4&#34;} ..
请注意,新的序列化输出缺少&#39;]}&#39;在{&#34; id&#34;:&#34; 1/2 ST 45&#34;}
之后TIA。