I want to load data to datatable when commandButton pressed in primefaces. When page load first, the datatable must be empty but after clicked the commandButton, the datatable must be loaded! How can I do. I'm newbie at the primefaces.
2 Answers
Don't initialize your dataTable value when the page loads at first. After that you can update p:dataTable
by updating it's id. Just add
<p:dataTable id="dataTableId" ...>
<p:commandButton update="dataTableId" ..>
to your commandButton. In addition you need to use either an
action="#{bean.addStuffToDataTable}"
or
actionListener="#{bean.addStuffToDataTable}"
as an attribute in your p:commandButton in order to populate data in your dataTable. Be sure to read Differences between action and actionListener before you use one of these attributes.
-
I think it will be good if I give extra information. Now, I have DataTable which has checkboxs to select any record.DataTable selection property set to "sucBean.selectedSuc". selectedSuc is an array of "Suc" object and at first it is null.So when i want to use for load data to Gmap, Eclipse gives me "java.lang.NullPointerException". What can i do for this situtation ? – yns89 Oct 18 '12 at 13:16
-
Don't set it to *null*, return an empty array maybe? If that doesn't help, provide source code including the NullPointerException. – Manuel Oct 18 '12 at 13:20
-
@Manuel How do i not initialize a dataTable on page load ? – Telson Alva Aug 02 '13 at 14:59
-
Simply by not initializing it? Just return an empty list. When the button is pressed you can initialize the list with values. – Manuel Aug 02 '13 at 15:02
From what you described in your question, here is a sample (This is just a working sample of what @Manuel described). Since the List on the backing bean is empty, at initial render, the datatable will be empty. When you click the commandbutton, it will fire the doPopulateList() method. This method will then populate the list. Finally, the update attribute will cause the datatable to reload the list from the backing bean. This time the List will be populated with 4 records.
<h:body>
<h:form>
<p:dataTable id="fooTable" value="#{dataTableBean.dataTableList}" var="record">
<p:column><h:outputText value="#{record}"/></p:column>
</p:dataTable>
<p:commandButton value="Populate DataTable" actionListener="#{dataTableBean.doPopulateList()}" update="fooTable"/>
</h:form>
</h:body>
DataTableSample backing bean:
@ManagedBean
@ViewScoped
public class DataTableBean implements Serializable {
List<String> dataTableList = new ArrayList<String>();
/**
* @return the dataTableList
*/
public List<String> getDataTableList() {
return dataTableList;
}
/**
* @param dataTableList the dataTableList to set
*/
public void setDataTableList(List<String> dataTableList) {
this.dataTableList = dataTableList;
}
public void doPopulateList(){
dataTableList.add("Record1");
dataTableList.add("Record2");
dataTableList.add("Record3");
dataTableList.add("Record4");
}
}

- 1,008
- 3
- 18
- 32