简体   繁体   English

Dojo破坏DropDownButton上的MenuItem

[英]Dojo destroy MenuItems on DropDownButton

I have the following markup: 我有以下标记:

<button dojoType="dijit.form.DropDownButton" dojoAttachPoint="labels" label="Labels">
    <div dojoType="dijit.Menu" dojoAttachPoint="labelsMenu"></div>
</button>

I am adding MenuItems programatically and it works fine for the first time. 我以编程方式添加MenuItems,并且第一次可以正常工作。 But when I want to refresh I get an error: Tried to register widget with id==16 but that id is already registered . 但是,当我想刷新时,出现错误: Tried to register widget with id==16 but that id is already registered I have tried the following code to clear but it's not working: 我已经尝试清除以下代码,但无法正常工作:

var labels = dijit.findWidgets(this.labels);
dojo.forEach(labels, function (l) {
    l.destroyRecursive();
});
dojo.empty(dojo.byId(this.labels));

I have also try the same thing for labelsMenu to empty it but no luck. 我也尝试过将labelsMenu清空,但是没有运气的方法。 Is there any other way to get rid of all children when reloading data or am missing something? 重新加载数据或丢失某些东西时,还有其他方法可以摆脱所有子项吗?

I have solved it and here's what I did: var menuChildren = dijit.byId(this.labelsMenu).getChildren(); if (menuChildren.length > 0){ dojo.forEach(menuChildren, function(mc){ mc.destroyRecursive(); }); } 我已经解决了,这就是我所做的: var menuChildren = dijit.byId(this.labelsMenu).getChildren(); if (menuChildren.length > 0){ dojo.forEach(menuChildren, function(mc){ mc.destroyRecursive(); }); } var menuChildren = dijit.byId(this.labelsMenu).getChildren(); if (menuChildren.length > 0){ dojo.forEach(menuChildren, function(mc){ mc.destroyRecursive(); }); }

In your code you call dojo.empty on the labels. 在代码中,您可以在标签上调用dojo.empty。 dojo.empty() empties the element in the DOM but keeps the original element. dojo.empty()清空DOM中的元素,但保留原始元素。 So try calling dojo.empty on the dijit menu instead. 因此,请尝试改为在dijit菜单上调用dojo.empty。

dojo.empty(dojo.byId("labelsMenu"));

For reference, in a fully baseless application , the dom-construct module is used. 作为参考,在完全无基础的应用程序中 ,使用dom-construct模块。

require(["dojo/dom-construct"], function(domConstruct){
  // Empty node's children byId:
  domConstruct.empty("someId");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM