带有的不同 div 标签 id
我试图根据支持 bean 的“deviceSel”中包含的项目数量动态生成 div 标签。对于每个 div 标签,我通过调用 JS 文件中存在的“generateSparkLine”函数(“Reports_Cap”)来填充它。我需要传递 iv 标签的 id 以便该函数填充相应“div”标签中的数据。
在我当前的代码中,div 是使用相同的 id 创建的,因此,单击时会填充相同的部分。有人可以帮我找到一种从 JSF 文件动态分配 id 到 div 标签的方法吗?另外,div 标签不允许我使用“onload”事件,这就是我使用“onclick”的原因。我该如何修改它,以便在页面打开时填充 div 标签?
<div>
<ui:repeat var="deviceSel" value="#{reportViewproto.selectedDevices}">
<h:outputText value="#{deviceSel}" />
<div id="chartDiv" style="width:600px; height:400px;" onclick="Reports_Cap.generateSparkLine('chartDiv', 'generateSparkLine')"></div>
<br />
</ui:repeat>
</div>
I am trying to generate div tags dynamically depending on the number of items, contained in the 'deviceSel' from the backing beans. For each of the div tag, I am populating it by calling the 'generateSparkLine' function that is present in a JS file - 'Reports_Cap'. I need to pass the id of the iv tag for the function to populate the data in the corresponding 'div' tag.
In my current code, the divs get created with the same id and hence, the same portion gets populated onclick. Could someone help me with a way to dynamically assign ids to the div tags from the JSF file? Also, div tag is not letting me use the "onload" event and that is the reason I am using the "onclick". How could I go about modifying it so that I could get the div tags populated when the page opens up?
<div>
<ui:repeat var="deviceSel" value="#{reportViewproto.selectedDevices}">
<h:outputText value="#{deviceSel}" />
<div id="chartDiv" style="width:600px; height:400px;" onclick="Reports_Cap.generateSparkLine('chartDiv', 'generateSparkLine')"></div>
<br />
</ui:repeat>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几种方法。
如果 JS
generateSparkLine()
函数在您的控制之下,并且您实际上使用id
通过 ID 从文档中获取元素,然后去掉 id 属性并将 onclick 替换为这样你就不需要 ID 了。您已经将该元素作为函数中的第一个参数。
或者如果它不在您的控制之下和/或您不使用它通过 ID 获取元素并且您正在使用 Facelets 2.x,则引入一个通过
的varStatus
属性循环计数器,并用它作为 ID 值的后缀。如果您仍在使用 Facelets 1.x,那么最好的选择是 JSTL
,它也以相同的方式支持varStatus
属性,唯一的区别是它在视图构建时运行,而不是在视图渲染时运行,因此您将无法在另一个UIData
组件中使用它。Several ways.
If the JS
generateSparkLine()
function is under your control and you're actually using theid
to get the element from the document by ID, then just get rid ofid
attribute and replace the onclick byThis way you don't need the ID. You already have the element as 1st argument in the function.
Or if it is not under your control and/or you don't use it to obtain the element by ID and you're using Facelets 2.x, then introduce a loop counter by
varStatus
attribute of<ui:repeat>
and suffix the ID values with it.If you are still on Facelets 1.x, your best bet is JSTL
<c:forEach>
which also supports avarStatus
attribute the same way, with the only difference that it runs during view build time, not during view render time and thus you won't be able to use it in anotherUIData
component.