We will try to accomplish a frequent requirement, which is deleting multiple rows from a table. Now a delete operation is available out of the box on a Data Control; but that can only be used to delete 1 row at a time.
Deleting multiple rows can be done in 2 simple ways.
Create an Application named "DeleteMultipleRows"; give any suitable Package Prefix
Keep pressing Next with default values to create Default "Model" and "ViewController" projects in the application. Right Click Model and chose New-> BC4J -> Select the HR DB Connection on XE.
In the next screen chose Employees table as shown below
In the next screen (Updatable View Objects) shuttle the EO to the right and change the name to "EmployeesVO". Press Next. Don't do anything in Read-Only View Objects. Press Next. In the Application Module section chose the default Values of Package and AM Name (although you can change it as well).
Now lets validate if our Application Module is created correctly by running the Application Module. In ADF, this is a very nice feature which will allow the user to see the values shown by the DB through AMs.
Create a Page DeleteEmployeesPG.jspx in "ViewController" project and drop the EmployeeVO1 from DataControl as a table as shown.
To display Button which will be used to Delete a row, surround the table by "panelCollection". You can easily do it in the source of the page by dragging the panelCollection on the top of "table" tag and then manually cut and paste the </panelCollection> after the table as shown in 2 screenshots below.
(Actually you can first put a panelCollection and then insert a table; its just that I have this bad habit of doing things in a bad way)
Drag toolbar from Component palette and drop it in toolbar facet of panelCollection as shown below. This is done to hold Delete Button.
Drag a button inside toolbar as shown below.
On this button, we will add an Action Listener which is a Java method to delete Rows programatically.
Select the Button Click the down arrow and chose Edit. In the Managed Bean select New and chose values as shown below:
Give method name as delSelectedEmp as shown below
This will create a Java Class with a method named delSelectedEmp which will be called upon Button click.
Now 2 major things are left
a) creating a check-box in the table
The table is based on EmployeeVO; and in order to capture the value of each instance of Check-box in the row we need to create a Transient attribute in the EmployeeVO as shown below:
On the page, add a column from Component Palette and drop at the beginning of the first column as shown below.
Modify the width to 20.
Insert a Check-Box inside the column by Dragging the "Delete-Box" attribute from Data Control and choosing "Single Selection"-> "ADF Select Boolean Checkbox". This will result in a Dialog Box to open. Enter Y for "Selected State Value" and N for "Unselected State Value" as shown below:
Insert an Output Text to the right of the delete button (Inside the Toolbar) which will indicate the No of records deleted. Select OutputBox and add binding. Chose Managed Bean as DeleteEmployee and Click on New and give "deletedEmpCount" in the Property. It should look like below:
The source will look like the below. Note that its bind to a method deletedEmpCount
Deleting multiple rows can be done in 2 simple ways.
- Create a Check Box inside a column to determine if a row is selected to delete. Create a Command Button which will handle delete operation programatically using an Action Listener.
- Create a multiple row selectable table. Create a Command Button which will call a Java Program to identify the selected Rows and delete then programatically.
Create an Application named "DeleteMultipleRows"; give any suitable Package Prefix
Keep pressing Next with default values to create Default "Model" and "ViewController" projects in the application. Right Click Model and chose New-> BC4J -> Select the HR DB Connection on XE.
In the next screen chose Employees table as shown below
In the next screen (Updatable View Objects) shuttle the EO to the right and change the name to "EmployeesVO". Press Next. Don't do anything in Read-Only View Objects. Press Next. In the Application Module section chose the default Values of Package and AM Name (although you can change it as well).
Now lets validate if our Application Module is created correctly by running the Application Module. In ADF, this is a very nice feature which will allow the user to see the values shown by the DB through AMs.
Create a Page DeleteEmployeesPG.jspx in "ViewController" project and drop the EmployeeVO1 from DataControl as a table as shown.
To display Button which will be used to Delete a row, surround the table by "panelCollection". You can easily do it in the source of the page by dragging the panelCollection on the top of "table" tag and then manually cut and paste the </panelCollection> after the table as shown in 2 screenshots below.
(Actually you can first put a panelCollection and then insert a table; its just that I have this bad habit of doing things in a bad way)
Drag toolbar from Component palette and drop it in toolbar facet of panelCollection as shown below. This is done to hold Delete Button.
Drag a button inside toolbar as shown below.
On this button, we will add an Action Listener which is a Java method to delete Rows programatically.
Select the Button Click the down arrow and chose Edit. In the Managed Bean select New and chose values as shown below:
Give method name as delSelectedEmp as shown below
This will create a Java Class with a method named delSelectedEmp which will be called upon Button click.
Now 2 major things are left
a) creating a check-box in the table
The table is based on EmployeeVO; and in order to capture the value of each instance of Check-box in the row we need to create a Transient attribute in the EmployeeVO as shown below:
On the page, add a column from Component Palette and drop at the beginning of the first column as shown below.
Modify the width to 20.
Insert a Check-Box inside the column by Dragging the "Delete-Box" attribute from Data Control and choosing "Single Selection"-> "ADF Select Boolean Checkbox". This will result in a Dialog Box to open. Enter Y for "Selected State Value" and N for "Unselected State Value" as shown below:
Insert an Output Text to the right of the delete button (Inside the Toolbar) which will indicate the No of records deleted. Select OutputBox and add binding. Chose Managed Bean as DeleteEmployee and Click on New and give "deletedEmpCount" in the Property. It should look like below:
The source will look like the below. Note that its bind to a method deletedEmpCount
Add the Commit Operation to the Binding; so that we can commit after removing the records from the Iterator.
Goto Bindings tab on "DeleteEmployeesPG.jspx" -> Press "Add" button under bindings -> Chose "Action" -> Press "Ok". In the resulting window select the "AppModuleDataControl" and in Operation Select "Commit" ->Press"Ok".
b) writing the code to delete Rows in method "delSelectedEmp".
You can navigate to this method by going to the Source of the page and Ctrl + Double click on "delSelectedEmp". Or Search for DelEmp.java in Application navigator and open the java file. Find "delSelectedEmp" inside this class.
Now, Add the following code in "delSelectedEmp"
// Add event code here... BindingContext context = BindingUtils.getBindingContext(); DCBindingContainer bindings = (DCBindingContainer)context.getCurrentBindingsEntry(); DCIteratorBinding iterator; iterator = (DCIteratorBinding)bindings.findIteratorBinding("EmployeesVO1Iterator"); RowSetIterator rowSetIterator= iterator.getViewObject().createRowSetIterator(null); int i = 0; int recDeleted = 0; while (rowSetIterator.hasNext()) { Row r = rowSetIterator.next(); String firstName = (String)r.getAttribute("FirstName"); String EmployeeId = (String)r.getAttribute("EmployeeId").toString(); System.out.println("i ="+i ); System.out.println("EmployeeId ="+EmployeeId ); System.out.println("firstName ="+firstName ); if(null != r.getAttribute("DeleteBox") && r.getAttribute("DeleteBox").equals("Y"))// && ("Y".equalsIgnoreCase((String)row.getAttribute("SelectTr")) )) { r.remove(); recDeleted++; } i++; } getDeletedEmpCount().setValue("Records Deleted: "+recDeleted); OperationBinding comt = (OperationBinding)bindings.getOperationBinding("Commit"); comt.execute();We are done. Run this page.
No comments:
Post a Comment