我无法从使用键码13默认输入键的数据表中传递正确的行索引。如果我构造一个commandButton并手动按下它,它将获得正确的输入。
在下面的示例中,您可以看到我试图通过10种不同方式实现此功能。我尝试从onkeypress = 13调用远程命令执行以下操作: - 将索引传递给支持bean - 始终使用索引0 - 使用绑定table.rowIndex将索引传递给支持bean - 始终使用索引0 - 传递当前行var - 始终使用列表中的最后一个条目 - 传递包装的ListDataModel a - 始终使用索引0
然后我尝试构建按钮,每个按钮都有一个独特的样式,并调用javascript功能点击正确的按钮。 alwasy使用索引0。
我也尝试过使用defaultCommand - 总是使用最后一个索引
所以,问题是,我该如何让它工作?
function convertHTMLtoPDF() {
// convert a specific message with an attached HTML file to PDF,
// and save it to the root directory of Google Drive
var threads = GmailApp.search('subject:(Sample HTML)');
var message = threads[0].getMessages()[0];
var attachmentName = message.getAttachments()[0].getName();
var attachment = message.getAttachments()[0].getAs("application/pdf");
DriveApp.createFile(attachment).setName(attachmentName);
}
支持Bean
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
>
<h:head>
</h:head>
<h:form id="formId" >
<h:body>
<script type="text/javascript">
function saveButton(myIndex) {
var id = '.saveButton' + myIndex;
var ele = $(id).get(0);
console.info("*** id " + id + " ele " + ele);
ele.click();
}
</script>
<p:panel id="messagesId">
<p:growl id="growlMessageId" showDetail="false" autoUpdate="true" for="growl"/>
<p:messages id="messageId" showDetail="false" autoUpdate="true" for="messages"/>
</p:panel>
<p:dataTable id="wordsId" var="word" value="#{wordIndexBean.words}"
rowKey="#{wordIndexBean.words}"
rowIndexVar="index"
editable="true" editMode="cell"
style="width:620px;height:400px"
emptyMessage="Your words go here"
binding="#{table}">
<p:column headerText="Modify">
<p:outputLabel value="#{word}"/>
</p:column>
<p:column headerText="Modify" width="275px">
<!--always returns last word in list-->
<p:remoteCommand name="remoteWordEnter" update="wordsId"
action="#{wordIndexBean.modifyWordAction(word)}"/>
<!--always finds first/0 word in model-->
<p:remoteCommand name="remoteWordEnterModel" update="wordsId"
action="#{wordIndexBean.modifyWordActionModel}"/>
<!--always uses index 0-->
<p:remoteCommand name="remoteIndexEnterIndex" update="wordsId"
action="#{wordIndexBean.modifyWordActionIndex(index)}"/>
<!--always uses first index 0-->
<p:remoteCommand name="remoteIndexEnterIndex2" update="wordsId"
action="#{wordIndexBean.modifyWordActionIndex(table.rowIndex)}"/>
<!--
<p:inputText id="inputId" value="#{wordIndexBean.modifyWord}" style="width:96%"
onkeypress="if (event.keyCode == 13) { remoteWordEnterIndex2(); return false; }"/>
-->
<!-- Following test calls javascript function. JS gets correct index but button always calls with 0 index -->
<p:inputText id="inputId2" value="#{wordIndexBean.modifyWord}" style="width:96%"
onkeypress="if (event.keyCode == 13) { saveButton(#{index});}"/>
</p:column>
<p:column width="20px">
<f:facet name="header">
<h:outputText value="index"/>
</f:facet>
<h:outputText value="#{index}" style="font-weight:bold;"/>
</p:column>
<p:column width="20px">
<f:facet name="header">
<h:outputText value="action"/>
</f:facet>
<!--<p:defaultCommand target="save"/> -->
<!-- DefaultCommand always uses last index, using button manually works -->
<h:commandButton id="save" styleClass="saveButton{index}" value="Save #{index}"
action="#{wordIndexBean.modifyWordActionIndex(index)}"/>
<h:commandButton id="save2" styleClass="save2Button{index}" value="Save2 #{table.rowIndex}"
action="#{wordIndexBean.modifyWordActionIndex(table.rowIndex)}"/>
</p:column>
</p:dataTable>
</h:body>
</h:form>
</html>