Xpages 如何在CSJS运行时确定字段ID?
这是一个场景: Xpage上有两个字段需要通过Ajax调用填充。 Ajax调用返回的是一个json结构。 在旧式web开发中,我们使用prototype.js进行此操作:Xpages 如何在CSJS运行时确定字段ID?,xpages,Xpages,这是一个场景: Xpage上有两个字段需要通过Ajax调用填充。 Ajax调用返回的是一个json结构。 在旧式web开发中,我们使用prototype.js进行此操作: $H( json ).each(function(pair){ try { $( pair.key ).value = pair.value } catch(err) { } }); 这里的假设是FieldID等于json键 { "fieldID1":"value1",
$H( json ).each(function(pair){
try {
$( pair.key ).value = pair.value
}
catch(err) { }
});
这里的假设是FieldID等于json键
{
"fieldID1":"value1",
"fieldID2":"value2"
}
Xpages CSJS需要在脚本中显示字段ID占位符,以便能够转换为字段在Xpages上的实际ID:
$("#{id:fieldID1}").value = json.fieldID1;
$("#{id:fieldID2}").value = json.fieldID2;
如何使用以下内容确定CSJS运行时中的实际字段ID:
$H( json ).each(function(pair){
try {
$("#{id:"+pair.key+"}").value = pair.value
}
catch(err) { }
});
我们的实际表单有+10个字段需要填充,根据具体情况,这些字段是动态“加载”的,因此我们在表单上有2…n个字段需要用ajax/json填充。好的,问题是XPage在提供给浏览器时不知道id。它是使用pair.key计算的。Marky Roden在这方面写了几篇非常好的文章,我认为你可能会找到适合你的解决方案。见: 及 HTH;-)
/John以下是SSJS中getComponent()方法的CSJS实现:
/**
* getComponent
*
* CSJS implementation of SSJS's getComponent method
*
* @param serverId ComponentId to search for
* @param fromObj DOM object to start with
* @author Sven Hasselbach
* @version 0.3
*/
function getComponent( serverId, fromObj ){
var found = false;
var obj = fromObj;
var id = "";
while( obj ){
try{
id = obj.id.split(":").pop();
}catch(e){}
if( id == serverId )
return obj;
ret = findComponent( serverId, obj );
if( ret )
return ret;
obj = obj.parentNode;
}
}
/**
* findComponent
*
* searches the component tree for a specific
* server id
*/
function findComponent( searchId, parentNode ){
var obj;
var id = "";
if( parentNode == null )
return;
if( parentNode.hasChildNodes() == false )
return;
for( p in parentNode.childNodes ){
obj = parentNode.childNodes[p];
try{
id = obj.id.split(":").pop();
if( id == searchId )
return obj;
ret = findComponent( searchId, obj );
if( ret )
return ret;
}catch(e){}
}
}
这允许您在组件树中搜索特定的服务器id。
要使用它,您必须首先定义一个起始组件,因为树是从节点到顶部搜索的
要获取起始节点,请执行以下操作:
var obj = XSP.getElementById( "view:_id1:layoutMain:callbackMiddle:dataViewAllReportsByStatus:1_sumLink" );
然后搜索另一个组件布局左f.e.:
是的,您需要在SSJS中使用getClientId(“controlname”)来获取正确的id。在jQuery中,您可能需要转义:在id中最终(dojo.byId起作用)我的问题是在运行时而不是在设计时确定组件的id。可能吗?Marky的例子是如何使用jQuery确定组件的ID,但它仍处于设计阶段,因为Xpages必须知道组件的ID才能转换它……我在答案中添加的第一个链接为您提供了一些关于该问题的良好背景知识,以及一些解决方案的想法。一个想法是使用包含字段名的类名。然后,您可以在运行时使用jQuery来查找使用类名的字段。另一个想法是使用#{…}语法在加载时使用字段名和“true”标识符构建一个“hashmap”。然后您将使用字段名作为键来查找此“表”中的真实id。。。不一定优雅——但它可以工作;-)/约翰正是我要找的!万分感谢!
var ret = getComponent("layoutLeft", obj );
alert( ret.id )