简体   繁体   English

像 p:dataTable 这样的迭代组件中的 p:remoteCommand 仅适用于最后一行

[英]p:remoteCommand inside iterating component like p:dataTable only works for last row

i was trying to print the value entered in the inputText but it only shows 0 except for the last row it shows the right value!我试图打印在 inputText 中输入的值,但它只显示 0 除了最后一行它显示正确的值! here's a minimal code (i didn't include all the fields and the getters/setters)这是一个最小的代码(我没有包含所有字段和 getter/setter)

@ManagedBean(name="medicament")
@ViewScoped
public class MedicamentBean {
  private List<Medicament> medicaments;
  private String libelle;
  private int qte_vente;

  public void test() {
    System.out.println(this.qte_vente);
}
}

html: html:

<h:form>
<p:dataTable value ="#{medicament.medicaments}" var ="m">
   <p:column headerText="libelle">      
      <h:outputText value = "#{m.libelle}"/>
   </p:column>
<p:column headerText="qte">
    <h:inputText value ="#{medicament.qte_vente}" onkeyup="myCommand();"/>
    <p:remoteCommand name="myCommand" actionListener="#{medicament.test()}"  style="display: none;" 
 />
   </p:column>

</p:dataTable>
</h:form>

Open the webpage in webbrowser.在浏览器中打开网页。 Rightclick and choose View Page Source .右键单击并选择View Page Source Look carefully at the generated HTML output which you see there.仔细查看您在那里看到的生成的 HTML 输出。 When you have 10 table rows in eg p:datatable , it should look something like this当您在例如p:datatable有 10 个表行时,它应该看起来像这样

<table>
    <tr><td><script>function myCommand() { /* ... */ }</script></td></tr>
    <tr><td><script>function myCommand() { /* ... */ }</script></td></tr>
    <tr><td><script>function myCommand() { /* ... */ }</script></td></tr>
    <tr><td><script>function myCommand() { /* ... */ }</script></td></tr>
    <tr><td><script>function myCommand() { /* ... */ }</script></td></tr>
    <tr><td><script>function myCommand() { /* ... */ }</script></td></tr>
    <tr><td><script>function myCommand() { /* ... */ }</script></td></tr>
    <tr><td><script>function myCommand() { /* ... */ }</script></td></tr>
    <tr><td><script>function myCommand() { /* ... */ }</script></td></tr>
    <tr><td><script>function myCommand() { /* ... */ }</script></td></tr>
</table>

(When you use a different iterating component like a p:datagrid or even something like a ui:repeat it looks different but the generic 'repeating' is the same) (当您使用不同的迭代组件,如p:datagrid甚至类似ui:repeat它看起来不同,但通用的“重复”是相同的)

Guess which one gets invoked when you execute myCommand() in JavaScript ...猜猜在 JavaScript 中执行myCommand()时会调用哪个...

Right, that just doesn't work.是的,那是行不通的。

It would have worked with only one <p:remoteCommand> outside the <p:dataTable> to which you pass a parameter , which is a generic solution for all sorts of iterating components.它只与<p:dataTable> <p:remoteCommand>之外的一个<p:remoteCommand>一起工作,您将参数传递给它,这是各种迭代组件的通用解决方案。

But in your case you are in fact overcomplicating things.但是在您的情况下,您实际上使事情过于复杂。 Just use <p:ajax> instead.只需使用<p:ajax>代替。

<h:inputText ...>
    <p:ajax event="keyup" listener="#{medicament.test()}" />
</h:inputText>

That's all.就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM