Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set selected row from backing bean in primefaces datatable

I am using primefaces datatable with clickable rows and I need to find way how to set selected row from backing bean.

There is my datatable definition:

<p:dataTable id="cablePathTable" var="cablePath" value="#{commonTableBean.cableLazyModel}" rows="100"
         selectionMode="single" selection="#{commonTableBean.selectedCablePathTblRow}" 
         rowIndexVar="rowIndex"  widgetVar="datatableVar"
         emptyMessage="---">  
<p:ajax event="rowSelect" process="@this" update=":form:portFieldset" />

<p:column headerText="No">  
    <h:outputText value="#{cablePath.column1}" />  
</p:column>
<p:column headerText="Port A">  
    <h:outputText value="#{cablePath.column4}" />  
</p:column>
<p:column headerText="Port B">  
    <h:outputText value="#{cablePath.column5}" />  
</p:column>

I have tried following approach, but with no success. In backing bean I have added method:

public void test(){
    DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent("form:cablePathTable");
    dataTable.setRowIndex(2);
}

And I have added test butoon to the XHTML page:

<p:commandButton process="@this" update=":form:cablePathTable" value="set2row" action="commonTableBean.test"/>

But nothig is changed on the datatable, selection didn't change...

Please any ideas how to solve this problem?

like image 297
Karlik_B Avatar asked Jun 18 '12 17:06

Karlik_B


1 Answers

Just set the value behind selection="#{commonTableBean.selectedCablePathTblRow}".

public void test(){
    selectedCablePathTblRow = cablepath;
}
like image 184
BalusC Avatar answered Jan 07 '23 03:01

BalusC