noisegrrrl
noisegrrrl

Reputation: 159

updating datatable using AJAX not working

I'm retrieving the list of files in a folder when a button is clicked. The function that retrieves the folder works, and if I load it before the table is rendered the values are shown as they should. However if I try to populate the table with a commandbutton calling a function initializing the list of values, no changes are made to the datatable. Here's my code:

<h:form id="resultform"> 
    <p:dataTable id="resulttable" value="#{ViewBean.resultList}" var="result">
        <p:column>
            <f:facet name="header">
                <h:outputText value="GFEXP_NOM_BDOC" />
            </f:facet>
            <h:outputText value="#{result.name}" />
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="GFEXP_DATE_MODIF" />
            </f:facet>
            <h:outputText value="#{result.date}" />
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="GFEXP_PATH" />
            </f:facet>
            <h:outputText value="#{result.path}" />
        </p:column>
        <f:facet name="footer">
            <p:commandButton value="REFRESH exploitation" action="#{ViewBean.refresh}" update=":resultform:resulttable" ajax="true" />
        </f:facet>
    </p:dataTable>
</h:form>

Here's the backing bean code:

@ManagedBean(name = "ViewBean")
@ViewScoped
public class ViewBean implements Serializable {

    private static final long serialVersionUID = 9079938138666904422L;

    @ManagedProperty(value = "#{LoginBean.database}")
    private String database;

    @ManagedProperty(value = "#{ConfigBean.envMap}")
    private Map<String, String> envMap;

    private List<Result> resultList;

    public void initialize() {
        setResultList(Database.executeSelect(database));

    }

    public void refresh() {
        List<File> fileList = readDirectory();
        List<Result> tmpList = new ArrayList<Result>();
        for (File f : fileList) {
            Result result = new Result(f.getName().substring(0,
                    f.getName().lastIndexOf(".")), String.valueOf(f
                    .lastModified()), f.getPath().substring(0,
                    f.getPath().lastIndexOf("\\") + 1));
            tmpList.add(result);
        }
        setResultList(tmpList);
    }
//GETTERS AND SETTERS

Upvotes: 1

Views: 2014

Answers (1)

BalusC
BalusC

Reputation: 1108632

It look like that you're abusing preRenderView to initialize the state of a view scoped bean. However, as its name says, it runs right before the rendering of the view. You should instead be using the @PostConstruct annotation for that. When put on a method, then that method will be invoked after bean's construction and all managed property and dependency injection. It will not be invoked on all subsequent postback requests on the same view.

@PostConstruct
public void initialize() {
    resultList = Database.executeSelect(database);
}

Don't forget to remove the <f:event> altogether.

See also:

Upvotes: 2

Related Questions