【问题标题】:Dynamically/Selectively update elements on click of button单击按钮时动态/选择性地更新元素
【发布时间】:2016-04-25 13:59:22
【问题描述】:
<p:dataTable value=”#{myBean.myList}” var=”item”>
        <h:outputText id=”mytext” value=”#{item.valueText}”/>
</p:dataTable>

//Item class

Class Item 
{
    String valueText;
    Item(String valueText) 
    {
           this.valueText = valueText;
    }
}

//myList has 5 elements. 

Item(“red”); 
Item(“orange”); 
Item(“yellow”); 
Item(“green”); 
Item(“blue”);`

//Button  
<p:commandButton value=’submit’ actionListener=”#{myBean.checkColor}” update=”myText”/>` // This will update all the five texts.

//MyBean Class 

Class MyBean
{ 
     List<Item> myList; 
     public void checkColor()
     { 
            Iterator itr = myList.iterator();      
             while(itr.hasNext())
             { 
                        Item item = itr.getNext(); 
                       if(item.getValueText().contains(‘r’))
                        { 
                               item.setValueText(“Invalid Color”); 
                        } 
             } 
      } 
}

上面的代码将在单击按钮时对所有 5 个文本执行更新,尽管它只会更改包含字母 'r' 的文本的文本,因此其余两个更新只是浪费。 但我只想将其中包含字母“r”的文本更新为“无效颜色”。我该怎么做?

【问题讨论】:

  • 你的java代码无法编译。
  • @Unknown Ya 这可能是可能的,我只是想在这里解释问题。

标签: jsf primefaces


【解决方案1】:

您可以尝试在方法myBean.checkColor 中从bean 更新您的组件。为此,您需要使用以下结构:

RequestContext.getCurrentInstance().update("updatable_component_id");

在你的代码中应该是这样的:

class MyBean {
    private List<Item> myList;

    public void checkColor() {
        Iterator itr = myList.iterator();

        While(itr.hasNext()) {
            Item item = itr.getNext();
            If(item.getValueText().contains("r")) {
                Item.setValueText("Invalid Color");
                RequestContext.getCurrentInstance().update("updatable_component_id");
            }
        }
    }
}

about update from bean

【讨论】:

  • 但是由于 id 是动态生成的,我如何知道“updatable_component_id”的值。
  • 您在&lt;p:commandButton value="submit" actionListener="#{myBean.checkColor}" update="myText"/&gt; 中使用inputText id (mytext) 您可以在bean 中使用它。您也可以为dataTable 设置属性id,然后不会生成表的ID。然后你可以写“table_id:input_text_id”。
  • 但是这样所有 id (myText) 的组件都会被更新。所以这意味着所有五个文本都将被更新,但我只想更新包含字母“r”的 3 种颜色。文本“黄色”和“蓝色”也将被分配 id (myText),但我不想更新它。
  • id - myText 只有 &lt;h:outputText id="mytext" value="#{item.valueText}"/&gt; 组件,如果查看您的问题!并且组件不能有相同的id
  • 是的,你是对的,组件不能有相同的 id。因为&lt;h:outputText id="mytext" value="#{item.valueText}"/&gt; 在dataTable 中并且myList 有5 个元素,所以会生成5 个&lt;h:outputText id="mytext" value="#{item.valueText}"/&gt;,ID 类似于dataTableId:1:mytextdataTableId:2:myText 等,所以我怎么知道哪个生成的ID 属于哪个文本?跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-20
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
  • 2018-05-21
  • 1970-01-01
  • 2022-10-05
相关资源
最近更新 更多