2016-04-25 112 views
-2
<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'的文字爲'無效顏色'。我怎樣才能做到這一點?動態/選擇性地更新按鈕

+0

Java代碼無法編譯。 – Unknown

+0

@未知雅可能是可能的,我只是試圖解釋這裏的問題。 – nikhilsuri

回答

0

您可以嘗試使用方法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

+0

但是我怎樣才能知道「updatable_component_id」的值是否爲動態生成的ID。 – nikhilsuri

+0

在'''中使用'inputText' id(mytext),你可以在bean中使用它。你也可以爲'dataTable'設置屬性'id',然後不會生成表的id。然後你可以寫「table_id:input_text_id」。 – HAYMbl4

+0

但是這樣所有的id(myText)組件都會被更新。所以這意味着所有五個文本將被更新,但我想只更新包含字母'r'的三種顏色。文本'黃色'和'藍色'也將被分配id(myText),但我不想更新。 – nikhilsuri