p customers table table.addStyleClass(
"simple"); table.setAttribute(
"width",
"550px"); table.getForm().setButtonAlign(Form.ALIGN_RIGHT); table.addColumn(
new Column(
"id")); FieldColumn column =
new FieldColumn(
"name", new TextField()); column.getField().setRequired(
true); table.addColumn(column); column =
new FieldColumn(
"investments",
new InvestmentSelect()); column.getField().setRequired(
true); table.addColumn(column); column =
new FieldColumn(
"holdings",
new NumberField()); column.setAttribute(
"style",
"{text-align:right;}"); table.addColumn(column); column =
new FieldColumn(
"active",
new Checkbox()); column.setAttribute(
"style",
"{text-align:center;}"); table.addColumn(column); table.getForm().add(
new Submit(
"ok",
" OK ",
this,
"onOkClick")); table.getForm().add(
new Submit(
"cancel",
this,
"onCancelClick")); }
public void onInit() {
// Populate table before it is processed List customers = getCustomerService().getCustomersSortedByName(NUM_ROWS); table.setRowList(customers); }
public boolean onOkClick() {
if (table.getForm().isValid()) { getDataContext().commitChanges(); }
return true; }
public boolean onCancelClick() { getDataContext().rollbackChanges(); List customers = getCustomerService().getCustomersSortedByName(NUM_ROWS); table.setRowList(customers); table.setRenderSubmittedValues(
false);
return true; } } Note in this example the
onCancelClick() button rolls back the changes made to the rowList objects, by reloading their values from the database and having the FormTable not render the submitted values.
Combining Form and FormTable
By default FormTable will create an internal Form to submit its values.
If you would like to integrate FormTable with an externally defined Form, use the {@link FormTable#FormTable(java.lang.String,org.apache.click.control.Form) constructor}which accepts a Form.
Example usage:
private Form form; private FormTable formTable; public void onInit() { // LIMITATION: Form only processes its children when the Form is submitted. // Since FormTable sorting and paging is done via GET requests, // the Form onProcess method won't process the FormTable. // To fix this we override the default Form#onProcess behavior and check // if Form was submitted. If it was not we explicitly process the FormTable. form = new Form("form") { public boolean onProcess() { if (isFormSubmission()) { // Delegate to super implementation return super.onProcess(); } else { // If form is not submitted, explicitly process the table return formTable.onProcess(); } } }; formTable = new FormTable("formTable", form); formTable.setPageSize(10); form.add(formTable); ... }
@see FieldColumn
@see Form
@see Table