-1
<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”); 
                        } 
             } 
      } 
}

The above code will execute update on all 5 texts on click of the button though it will change the text only for texts containing letter 'r' so rest of two updates are just waste. But i want to update only texts having letter 'r' in them to 'Invalid Color'. How can i do that?

nikhilsuri
  • 43
  • 1
  • 5

1 Answers1

1

You can try update your component from bean in the method myBean.checkColor. For this you need use following structure:

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

In your code it is should look like this:

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

Community
  • 1
  • 1
HAYMbl4
  • 1,450
  • 2
  • 15
  • 29
  • but how will i come to know the value of "updatable_component_id" as the ids are dynamically generated. – nikhilsuri Apr 26 '16 at 03:38
  • you use `inputText` id (mytext) in `` you can use it in bean. Also you can set property `id` for `dataTable` then then the id for table will not generates. Then you can write "table_id:input_text_id". – HAYMbl4 Apr 26 '16 at 05:55
  • But this way all the components with id (myText) will be updated. so it means all five texts will be updated but i want to update only 3 colors which contain letter 'r'. Text 'Yellow' and 'blue' will also be assigned id (myText) but i dont want to update that. – nikhilsuri Apr 26 '16 at 08:03
  • id - `myText` have only `` component if look into your question! And components can not have same `id`! – HAYMbl4 Apr 26 '16 at 09:15
  • Yes, you are right components can not have same id. since `` is in dataTable and `myList` has 5 elements, so 5 `` will be generated with id's something like `dataTableId:1:mytext` and `dataTableId:2:myText` etc so how will i know which generated id belongs to which text? – nikhilsuri Apr 26 '16 at 09:49
  • ooou.. I did not pay attention that `outputText` work with table `var`. Is it work? You should use `p:column` for print some info in table column. Is your table have only one column with color? I don't see problem do update for table in your situation – HAYMbl4 Apr 26 '16 at 10:03
  • Actually it's a sample of the problem. The basic problem is that I want to selectively update components at runtime but since the id is dynamically generated in dataTable, so I don't know which id is associated with which DOM component. Is there a way to find out that? – nikhilsuri Apr 27 '16 at 04:23