Karthik
Karthik

Reputation: 381

JSF- How to get the previous selected row index from datatable?

I want to retain the value of previously selected row index to compare it with the currently selected rown index.How can I able to get the previous index in backing bean from a datatable in jsf ? Here is the sample code for getting the current index from datatable.

public void setindexvalue(DateSelectEvent event) {


        int index = Integer.parseInt(((String) event.getComponent()
                .getAttributes().get("index")).replace("_", ""));
        System.out.println("Index:\t" + index);
}

I am calling the above method in JSF XHTML using the below code :

    <p:ajax event="dateSelect"    listener="#{Bean.setindexvalue}" />
 <f:attribute name="index" value="_#{rowIndex}" />

The above code is inside a datatable for a specific cell and I want to call the "setindexvalue" each time when a row gets selected, but when I click the next row somehow,I want to retain the value of previously selected row index in backing bean.So that I can compare the current index with the previous index
How can I able to do it ?

Upvotes: 0

Views: 1117

Answers (1)

JGeo
JGeo

Reputation: 380

private int previous_index;

public void setindexvalue(DateSelectEvent event) {

        int index = Integer.parseInt(((String) event.getComponent()
                .getAttributes().get("index")).replace("_", ""));
        System.out.println("Index:\t" + index);

// Here you have your previous index stored in the variable and can do the comparison

        previous_index = index;
}

Upvotes: 2

Related Questions