[英]During a onchange event, how to change value and displayed value of PrimeFaces SelectOneMenu via JavaScript?
I am using PrimeFaces 5.0 (unable to upgrade due to other issues). 我正在使用PrimeFaces 5.0(由于其他问题而无法升级)。 I have a SelectOneMenu.
我有一个SelectOneMenu。 I bound an onchange event to it, so that when user changes it, I can use confirm() to prompt user to confirm the action.
我将一个onchange事件绑定到它,以便用户更改它时,可以使用Confirm()提示用户确认操作。 When user clicks no, I need to put the old value back.
当用户单击“否”时,我需要恢复旧值。
The menu looks like this: 菜单如下所示:
<p:selectOneMenu
id="referenceTable"
widgetVar="referenceTable"
style="width:45%"
value="#{maintRefTabUi.selectedReferenceTable}"
onchange="changeReferenceTableDropdown();"
>
...snip...
The function looks like this: 该函数如下所示:
function changeReferenceTableDropdown() {
if (DirtyFlag.isDirty) {
if (confirm('You have unsaved changes. Do you want to discard your changes and switch to another reference table?')) {
DirtyFlag.resetDirtyFlag();
changeReferenceTable();
} else {
// Pick one below
// Attempt 1: The .value property is reverted, but displayed value isn't
document.forms['tableSelectForm'].reset();
// Attempt 2: Same as attempt 1
PF('referenceTable').value = PF('referenceTable').preShowValue.val();
// Attempt 3: The dropdown box is reverted, but onchange got fired twice
PF('referenceTable').selectValue(PF('referenceTable').preShowValue.val());
// Attempt 4: .value and visually reverted, but if you click the dropdown box, the selected value jumps back to the discarded value
PF('referenceTable').revert();
}
} else {
changeReferenceTable();
}
}
I am unable to find anything regarding .selectValue() and onchange() event firing twice. 我找不到有关.selectValue()和onchange()事件触发两次的任何信息。 Because JavaScript setting .value won't fire onchange(), .selectValue() firing onchange() might be a bug.
因为JavaScript设置.value不会触发onchange(),所以.selectValue()触发onchange()可能是一个错误。
Is there a way to supress onchange()? 有没有办法抑制onchange()? I also tried onchange=null;
我也尝试过onchange = null; and putting it back afterwards, but it didn't work (onchange still gets fired).
并将其放回原处,但没有成功(仍会触发变更)。
Or is there a way to make SelectOneMenu update its display according to .value? 还是有一种方法可以使SelectOneMenu根据.value更新其显示?
I ended up doing this: 我最终这样做:
In the setter associated with the SelectOneMenu, I saved the old value in another member variable. 在与SelectOneMenu关联的设置器中,我将旧值保存在另一个成员变量中。
In the onchange() event of the SelectOneMenu, if user cancelled then I call a new method in the bean to restore the old value, then update the SelectOneMenu. 在SelectOneMenu的onchange()事件中,如果用户取消了,那么我在Bean中调用一个新方法来恢复旧值,然后更新SelectOneMenu。
More trips to server instead of blocking it on client-side, but at least it gets the job done. 服务器上有更多的行程,而不是在客户端进行阻止,但至少可以完成工作。
why do you not use the Primefaces confirm dialog? 为什么不使用Primefaces确认对话框?
http://www.primefaces.org/showcase/ui/overlay/confirmDialog.xhtml http://www.primefaces.org/showcase/ui/overlay/confirmDialog.xhtml
I could imagine, that your problem will be obsolete because you must not rely on javascript code. 我可以想象,您的问题将过时,因为您不能依赖javascript代码。
I hope this helps! 我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.