[英]dialog'dynamic content shows only once after first button click
I am trying to show a list into a dialog after a button click, the content of the list is retrieved from the server, my list shows when i click the button for the first time but then the dialog is shown empty. 单击按钮后,我试图在对话框中显示列表,从服务器检索列表的内容,当我第一次单击按钮时,列表显示,但是对话框显示为空。
the code of onclick is working an addElement runs every time the button is clicked. onclick的代码正在工作,每次单击按钮时都会运行addElement。
<ons-button modifier = "small" style="margin-left: 25%;"
onclick="menu.setMainPage('default.html',{closeMenu: true});
ons.createDialog('dialog.html').then(function(dialog) {dialog.show({callback : function(){addPresentation();}});});
console.log('log');
"
>
new presentation
</ons-button>
<ons-template id="dialog.html">
<ons-dialog var = "dialog" id = "dialog" cancelable>
<ons-scroller style="height: 200px; width: 100%">
<div class="dialog-content" id="diagcontent" >
</div>
</ons-scroller>
</ons-dialog>
</ons-template>
I even tried doing this way : 我什至尝试这样做:
<ons-button modifier = "small" style="margin-left: 25%;"
onclick="addElement();
ons.createDialog('dialog.html').then(function(dialog) {dialog.show();});
"
>
Again it gives the same result. 再次给出相同的结果。
Js method : js方法:
function addPresentation(){
if (window.XMLHttpRequest){
var xmlHTTP = new XMLHttpRequest();
xmlHTTP.open("GET", addpresentation, true);
xmlHTTP.send();
xmlHTTP.onreadystatechange = function() {
var script = document.createElement('script');
script.innerHTML = "ons.compile(document.getElementById('dialog')); console.log('script);";
document.getElementById('dialog').appendChild(script);
if (xmlHTTP.readyState == 4 && xmlHTTP.status == 200)
{//document.getElementById("diagcontent").innerHTML = "";
var list = document.createElement('ons-list');
var json = JSON.parse(xmlHTTP.responseText);
console.log("addPresentation : " + json);
for (var i=0;i<json.length;i++){
var btn = document.createElement('ons-button');
btn.setAttribute('modifier',"quiet");
btn.innerHTML = json[i].name;
var id = json[i].id_presentation
var name = json[i].name;
btn.setAttribute('onclick',"addElement(\""+id+"\",\""+name+"\");dialog.hide();");
var listitem = document.createElement('ons-list-item');
listitem.appendChild(btn);
list.appendChild(listitem);
}
document.getElementById("diagcontent").appendChild(list);
}
}
}
}
The ons.createDialog
method will create a dialog and append it to the DOM. ons.createDialog
方法将创建一个对话框并将其附加到DOM。 It will not be destroyed when you hide it, you also need to call dialog.destroy()
. 隐藏它时不会销毁它,还需要调用
dialog.destroy()
。
document.getElementById('diagcontent')
will only return the first item with the ID diagcontent
that's why it only works the first time. document.getElementById('diagcontent')
将仅返回ID为diagcontent
的第一项,这就是为什么它仅在第一时间有效。
I think you should create the dialog once and just use the dialog.show()
method when the button is clicked. 我认为您应该一次创建对话框,并且在单击按钮时仅使用
dialog.show()
方法。
You can create it every time the button is clicked but in that case you must be sure that two dialogs cannot be present in the DOM at the same time since you're using an element with an ID property and IDs are supposed to be unique. 您可以在每次单击按钮时创建它,但是在这种情况下,必须确保不能同时在DOM中出现两个对话框,因为您使用的是具有ID属性的元素,并且ID应该是唯一的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.