javascript 如何找到一个widgetVar?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17370915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find a widgetVar?
提问by Mateus Viccari
If i have a primefaces component, like
如果我有一个primefaces组件,比如
<p:selectOneMenu id="myComponent">
...
</p:selectOneMenu>
In the html, it will generate something like this:
在 html 中,它将生成如下内容:
<div id="myFormName:myComponent" widgetVar="lollipop">
...A lot of things in here...
</div>
<script id="myFormName:myComponent_s">
$(function(){PrimeFaces.cw('SelectOneMenu','lollipop',.......
</script>
Inside the script tag, you can notice the widget var name(if i dont provide it in the component, it will be generated). I want to know how can i get the widget var element, or if this is not possible, how can i get that "" tag so i can get the name of this widget var.
在脚本标签内,您可以注意到小部件 var 名称(如果我不在组件中提供它,它将被生成)。我想知道如何获得小部件 var 元素,或者如果这不可能,我怎样才能获得“”标签,以便获得该小部件 var 的名称。
------ EDIT ------ I will try to explain why i need this. i have this function:
------ 编辑------ 我会试着解释为什么我需要这个。我有这个功能:
function simulaTabManoBrow(event){
var focusedComponent=document.activeElement;
if(event.keyCode==13){
//Cancel th edefault enter event(submit the form)
event.preventDefault();
event.stopPropagation();
event.returnValue = false;
event.cancelBubble = true;
if((focusedComponent.tagName.toLowerCase()=="input" && focusedComponent.type.toLowerCase()=="button") || focusedComponent.tagName.toLowerCase()=="button"){
//If the focused component is a button, click the button.
focusedComponent.click();
}else{
//Press the tab key programatically
$.emulateTab();
verifyOneMenu(campoFocado);
}
}
}
This function is executed on the body's onkeydown event. The goal of this is to replace the default behavior of the enter key by the tab key. The only problem of this is, when the focused component is a selectOneMenu and the user hits enter, it correctly behaves like the tab key, but the selectOneMenu previously focused is opened(because this is the default behavior of the component).
该函数在 body 的 onkeydown 事件上执行。这样做的目的是用 tab 键替换 enter 键的默认行为。唯一的问题是,当聚焦的组件是 selectOneMenu 并且用户按下 Enter 键时,它的行为正确地类似于 tab 键,但之前聚焦的 selectOneMenu 被打开(因为这是组件的默认行为)。
So, what i am trying to do, is to call the close() method of the selectOneMenu widget var of that previously focused component.
所以,我想要做的是调用之前关注的组件的 selectOneMenu 小部件 var 的 close() 方法。
回答by Hatem Alimam
You can get the widgetVar
object by id using this handy function:
您可以widgetVar
使用这个方便的函数通过 id获取对象:
Function
功能
function getWidgetVarById(id) {
for (var propertyName in PrimeFaces.widgets) {
if (PrimeFaces.widgets[propertyName].id === id) {
return PrimeFaces.widgets[propertyName];
}
}
}
Usage
用法
getWidgetVarById('myFormName:myComponent');
Example
例子
getWidgetVarById('dialogId').show();
See More:
查看更多:
回答by Danubian Sailor
The algorithm to generate the widget var is quite easy:
生成小部件 var 的算法非常简单:
- Take the element's id
- Convert the colons
:
to underlines_
- Append
widget_
on start
- 获取元素的 id
- 将冒号转换
:
为下划线_
widget_
开始时追加
So for example, if your element's id is main:personal:age
, the widget is will be widget_main_personal_age
.
例如,如果您的元素的 id 是main:personal:age
,则小部件将是widget_main_personal_age
。
The JSF id of the component is the same as the id attribute of corresponding html tag.
组件的JSF id 与对应的html 标签的id 属性相同。
回答by J. Van
In PrimeFaces 6.1 you can figure out the widgetVar assigned by the following rule. It's similar to the examples shown above, but in later versions of PrimeFaces they appear to be including the form id.
在 PrimeFaces 6.1 中,您可以计算出由以下规则分配的 widgetVar。它类似于上面显示的示例,但在 PrimeFaces 的更高版本中,它们似乎包含表单 id。
For example:
例如:
"widget_" + <form id> + "_form_" + <selectBooleanCheckbox id>
Note:In my case, the ids have dashes. Dashes are converted to underscores. As shown above, it's probably the same conversion of colons too.
注意:就我而言,ID 有破折号。破折号转换为下划线。如上所示,它也可能是相同的冒号转换。
For example, you have:
例如,您有:
<h:form id='my-page'> and <p:selectBooleanCheckbox id='the-checkboxes'>
The widgetVar would be:
widgetVar 将是:
"widget_my_page_form_the_checkboxes"
View your page source to verify it.
查看您的页面来源以进行验证。
Now get the object and give it a click like so...
现在获取对象并像这样点击它......
P('widget_my_page_form_the_checkbox').click();
回答by Jasper de Vries
Since PrimeFaces 5.3 you can simply do:
从 PrimeFaces 5.3 开始,您可以简单地执行以下操作:
PrimeFaces.getWidgetById(domElementId);