Sun Java Studio Creator Table component has more capabilities than being exposed by the Table Layout dialog. One of the features is the ability to add a button panel to the header or the footer of the table component. This tip shows how to achieve this simple task. I have created a sample project, which extends the Multi row selectable Table Component tip project and adds button panels to the header and footer of the table component. User can select one or more rows from the table and then click the delete button in the header or footer to delete the rows. This sample project can be downloaded from here.
Also, I have included Java Script to the Table Component to pop up a confirmation dialog asking if the rows should be deleted. Also the dialog would warn that the action can not be undone. Also the Java Script would dynamically enable or disable the buttons based on the selection of the rows.
Steps:
<!-- Actions (Top) --><f:facet name="actionsTop">
<ui:button action="#{Page1.button1_action}"
binding="#{Page1.button1}" disabled="true" id="button1"
onClick="if (confirmDeleteSelectedRows() == false) return false"
text="Delete"/>
</f:facet>
<!-- Actions (Bottom) -->
<f:facet name="actionsBottom">
<ui:button action="#{Page1.button2_action}"
binding="#{Page1.button2}" disabled="true" id="button2"
onClick="if (confirmDeleteSelectedRows() == false) return false"
text="Delete"/>
</f:facet>
function confirmDeleteSelectedRows() {
var table = document.getElementById("form1:table1");
return table.confirmDeleteSelectedRows();
}
function disableActions() {
// Disable table actions by default.
var table = document.getElementById("form1:table1");
var selections = table.getAllSelectedRowsCount();
var disabled = (selections > 0) ? false : true;
// Set disabled state for top actions.
document.getElementById("form1:table1:button1").setDisabled(disabled);
// Set disabled state for bottom actions.
document.getElementById("form1:table1:button2").setDisabled(disabled);
}
public String button1_action() {
int selectedRows = getTableRowGroup1().getSelectedRowsCount();
RowKey[] selectedRowKeys = getTableRowGroup1().getSelectedRowKeys();
for(int i=0; i< selectedRowKeys.length; i++){
if (tripDataProvider.canRemoveRow(selectedRowKeys[i])){
tripDataProvider.removeRow(selectedRowKeys[i]);
info("Row " + selectedRowKeys[i].getRowId() + " Removed");
}else{
info("Can not remove row " + selectedRowKeys[i].getRowId());
}
}
tripDataProvider.commitChanges();
if(selectedRowKeys.length < 1){
info("No rows selected for deletion");
}
return null;
}
That is it. We are done. Deploy the application. Initially the buttons will be disabled. Select a row and the buttons will be enabled. Click on the button and the confirmation dialog (shown below) should popup.