Getting value from table filter in ADF
Scenario: In many case we may need to override the default filter of af:table. Here I am going to show you how you can override the default table filter and getting the entered values from table filter along with attribute names.
Solution : Suppose you have a af:table which is build on EmployeesVO. Now go to you table source code in jspx/jsff and override the default queryListener and create a binding of your table to your bean as shown below:
<af:table value="#{bindings.EmployeesVO1.collectionModel}" var="row"
rows="#{bindings.EmployeesVO1.rangeSize}"
emptyText="#{bindings.EmployeesVO1.viewable ? 'No data to display.' : 'Access Denied.'}"
fetchSize="#{bindings.EmployeesVO1.rangeSize}"
rowBandingInterval="0"
filterModel="#{bindings.EmployeesVO1Query.queryDescriptor}"
queryListener="#{backingBeanScope.TableFilterTestBean.queryTable}"
filterVisible="true" varStatus="vs"
selectionListener="#{bindings.EmployeesVO1.collectionModel.makeCurrent}"
rowSelection="multiple" id="t1" styleClass="AFStretchWidth"
columnStretching="last" contentDelivery="immediate"
binding="#{backingBeanScope.TableFilterTestBean.employeeTable}">
Now go to your bean and write the following code inside your bean as shown below:
public void queryTable(QueryEvent queryEvent) {
FilterableQueryDescriptor filterQueryDes =
(FilterableQueryDescriptor)employeeTable.getFilterModel();
Map filterCriteria = filterQueryDes.getFilterCriteria();
Iterator it = filterCriteria.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println("Attribute Name====>" + pair.getKey() + " " +
"Attribute Value======>" + pair.getValue());
}
invokeQueryEventMethodExpression("#{bindings.EmployeesVO1Query.processQuery}",queryEvent);
}
private void invokeQueryEventMethodExpression(
String expression, QueryEvent queryEvent) {
FacesContext fctx = FacesContext.getCurrentInstance();
ELContext elctx = fctx.getELContext();
ExpressionFactory efactory =fctx.getApplication().getExpressionFactory();
MethodExpression me =efactory.createMethodExpression(elctx, expression, Object.class, new Class[] { QueryEvent.class });
me.invoke(elctx, new Object[] { queryEvent });
}
private RichTable employeeTable;
public void setEmployeeTable(RichTable employeeTable) {
this.employeeTable = employeeTable;
}
public RichTable getEmployeeTable() {
return employeeTable;
}
Now run your page and enter some value in table filter as shown below:
and click on enter. You will get all your value which you have entered in filter and if there is corresponding result you will get that filtered result in your table also.
In console you can see your output as follows:
Attribute Name====>ManagerId Attribute Value======>20
Attribute Name====>Email Attribute Value======>SKING
Attribute Name====>JobId Attribute Value======>AD_RES
Attribute Name====>HireDate Attribute Value======>Tue Jun 17 00:00:00 IST 2003
Attribute Name====>Salary Attribute Value======>24000
Attribute Name====>FirstName Attribute Value======>Steven
Attribute Name====>PhoneNumber Attribute Value======>515.123.4567
Attribute Name====>CommissionPct Attribute Value======>0.5
Attribute Name====>DepartmentId Attribute Value======>90
Attribute Name====>LastName Attribute Value======>King
Attribute Name====>EmployeeId Attribute Value======>100
Happy coding :)
Solution : Suppose you have a af:table which is build on EmployeesVO. Now go to you table source code in jspx/jsff and override the default queryListener and create a binding of your table to your bean as shown below:
<af:table value="#{bindings.EmployeesVO1.collectionModel}" var="row"
rows="#{bindings.EmployeesVO1.rangeSize}"
emptyText="#{bindings.EmployeesVO1.viewable ? 'No data to display.' : 'Access Denied.'}"
fetchSize="#{bindings.EmployeesVO1.rangeSize}"
rowBandingInterval="0"
filterModel="#{bindings.EmployeesVO1Query.queryDescriptor}"
queryListener="#{backingBeanScope.TableFilterTestBean.queryTable}"
filterVisible="true" varStatus="vs"
selectionListener="#{bindings.EmployeesVO1.collectionModel.makeCurrent}"
rowSelection="multiple" id="t1" styleClass="AFStretchWidth"
columnStretching="last" contentDelivery="immediate"
binding="#{backingBeanScope.TableFilterTestBean.employeeTable}">
Now go to your bean and write the following code inside your bean as shown below:
public void queryTable(QueryEvent queryEvent) {
FilterableQueryDescriptor filterQueryDes =
(FilterableQueryDescriptor)employeeTable.getFilterModel();
Map filterCriteria = filterQueryDes.getFilterCriteria();
Iterator it = filterCriteria.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println("Attribute Name====>" + pair.getKey() + " " +
"Attribute Value======>" + pair.getValue());
}
invokeQueryEventMethodExpression("#{bindings.EmployeesVO1Query.processQuery}",queryEvent);
}
private void invokeQueryEventMethodExpression(
String expression, QueryEvent queryEvent) {
FacesContext fctx = FacesContext.getCurrentInstance();
ELContext elctx = fctx.getELContext();
ExpressionFactory efactory =fctx.getApplication().getExpressionFactory();
MethodExpression me =efactory.createMethodExpression(elctx, expression, Object.class, new Class[] { QueryEvent.class });
me.invoke(elctx, new Object[] { queryEvent });
}
private RichTable employeeTable;
public void setEmployeeTable(RichTable employeeTable) {
this.employeeTable = employeeTable;
}
public RichTable getEmployeeTable() {
return employeeTable;
}
Now run your page and enter some value in table filter as shown below:
and click on enter. You will get all your value which you have entered in filter and if there is corresponding result you will get that filtered result in your table also.
In console you can see your output as follows:
Attribute Name====>ManagerId Attribute Value======>20
Attribute Name====>Email Attribute Value======>SKING
Attribute Name====>JobId Attribute Value======>AD_RES
Attribute Name====>HireDate Attribute Value======>Tue Jun 17 00:00:00 IST 2003
Attribute Name====>Salary Attribute Value======>24000
Attribute Name====>FirstName Attribute Value======>Steven
Attribute Name====>PhoneNumber Attribute Value======>515.123.4567
Attribute Name====>CommissionPct Attribute Value======>0.5
Attribute Name====>DepartmentId Attribute Value======>90
Attribute Name====>LastName Attribute Value======>King
Attribute Name====>EmployeeId Attribute Value======>100
Happy coding :)
Comments
Post a Comment