I need af:selectOneChoice populated with values from the backing bean and one the value from the list (say index=5) should be selected by default. We are using Oracle Adf 10.*
Can somebody help me on this ?
Thanks
For populating the list values, you can use:
<af:selectOneChoice value="val3" label="XXXX" id="soc1" >
<f:selectItems value="#{YourBean.values}" id="si1"/>
</af:selectOneChoice>
In YourBean.java , you will have a method like:
public List<SelectItem> getValues() {
if (list == null) {
list = new ArrayList<SelectItem>();
list(new SelectItem("val1","Label 1"));
list(new SelectItem("val2","Label 2"));
list(new SelectItem("val3","Label 3"));
}
return list;
}
This way you will see "Label 3" as default value in your choice list.
For setting default value, you can use this:
<af:selectOneChoice label="My Field"
id="MyField" value="#{bindings.MyAttribute.inputValue}"
autoSubmit="true"
binding="#{MyBean.myField}">
<f:selectItems value="#{bindings.MyAttribute.items}"
id="MyFieldItems"/>
</af:selectOneChoice>
Notice that SelectOneChoice field has a binding to a java bean. We will use setter method to set default value.
public void setMyField(RichSelectOneChoice myField) {
// since getter/setter methods are called multiple times on
// page load, we need to prevent setting attribute value more than once
if(myField != null && myField.getValue() == null){
ADFUtils.setBoundAttributeValue("MyAttribute", "SomeValue");
}
this.myField = myField;
}
For setting default index (for example, first from the list) we can use a similar approach:
public void setMyField(RichSelectOneChoice myField) {
if(myField != null && myField.getValue() == null){
// default value will be 1st from the list
myField.setValue(0);
}
this.myField = myField;
}
Related
This is not exactly an question, better to say - it's an answer with a little question: is there a better sollution? :)
Suppose you have a grid which contains some rows.
In each row you want to put a radio button, and only one radio button can be selected in the grid (each row has one radio button).
This radio button should be used not only to display selected row, but also to select a row by clicking radio button.
Selection is based on a table's field, which contains 1 for selected and 0 or null for all other rows.
Here is a short guide of achieving it:
(the obvious part)
add a column with a radiobutton:
<af:column headerText="#{bindings.DocsignersliteView2.hints.Isactual.label}"
id="c14" align="center">
<af:selectBooleanRadio label="Label 1"
id="sbr1"
value="#{row.Isactual}"
group="Isactual"
autoSubmit="true">
</af:selectBooleanRadio>
</af:column>
where Isactual - is the field in the table, which containts 1 for selected item and null for all others
Method onActual should be implemened to change values of the field Isactual in the table.
it appears to not work. Now it shows an actual row, but doesn't work when I select another radio button.
Naturally I tried to create ValueChangeListener, but never succeded - it didn't work correctly.
Ok, let's try to invert "onClickListener", as selectBooleanRadio doesn't posess such an attribute:
let's add clientlistener, javascript and server listener:
<af:resource type="javascript">
function onClickRadio(actionEvent) {
var comp = actionEvent.getSource();
AdfCustomEvent.queue(comp, "IsactualClickEvent",
{},
true);
actionEvent.cancel();
}
</af:resource>
<af:selectBooleanRadio label="Label 1"
id="sbr1"
value="#{row.Isactual}"
group="Isactual"
autoSubmit="true">
<af:clientListener type="click" method="onClickRadio"/>
<af:serverListener type="IsactualClickEvent"
method="#{viewScope.signerBean.onActual}"/>
</af:selectBooleanRadio>
it almost works, but there is one more 'tiny' detail: a field with 1 and 0 values (or 1 and null) - is not exactly the same as boolean, so this selectBooleanRadio needs a converter.
So we should create a class implementing converter:
public class BooleanDecimalConverter implements Converter {
public BooleanDecimalConverter() {
super();
}
#Override
public Object getAsObject(FacesContext facesContext, UIComponent uIComponent, String string) {
if (string == null || string.isEmpty())
return null;
return "true".equals(string) ? BigDecimal.valueOf(1) : BigDecimal.valueOf(0);
}
#Override
public String getAsString(FacesContext facesContext, UIComponent uIComponent, Object object) {
if (object == null)
return null;
String result;
if (object instanceof Boolean){
result = object.toString();
}
else if (object instanceof BigDecimal) {
BigDecimal num = ((BigDecimal)object);
result = num.compareTo(BigDecimal.valueOf(1)) == 0? "true" : "false";
}
else {
result = "false";
}
return result;
}
}
then register the custom converter in the application's JSF configuration file (faces-config.xml):
<converter>
<converter-id>custom.BooleanDecimalConverter</converter-id>
<converter-class>common.utils.converters.BooleanDecimalConverter</converter-class>
</converter>
and at last we can add a converter attribute to af:selectBooleanRadio:
<af:selectBooleanRadio label="Label 1"
id="sbr1"
value="#{row.Isactual}"
group="Isactual"
converter="custom.BooleanDecimalConverter"
autoSubmit="true">
<af:clientListener type="click" method="onClickRadio"/>
<af:serverListener type="IsactualClickEvent"
method="#{viewScope.signerBean.onActual}"/>
</af:selectBooleanRadio>
What we have now:
A grid with rows, radio buttons show an "active" row and allow user to change "active" row immediately as he clicks the radio button.
i am New to ADF, i want display/enable the input text box when checkbox is checked and i should disable when it is unchecked below is the check box ADF code,
ADF Code:
<af:selectBooleanCheckbox label="Apply WITSML Filter" id="sbc11"
autoSubmit="true" contentStyle="margin-left:10px;" valueChangeListener="#{pageFlowScope.welljobs_bean.applyWITSMLFilterIndicator}"/>
Bean:
private transient RichSelectBooleanCheckbox applyWITSMLFilterIndicator;
public void setapplyWITSMLFilterIndicator(RichSelectBooleanCheckbox applyWITSMLFilterIndicator) {
this.applyWITSMLFilterIndicator= applyWITSMLFilterIndicator;
}
public RichSelectBooleanCheckbox getapplyWITSMLFilterIndicator() {
return applyWITSMLFilterIndicator;
}
The input text i want to show:
<af:inputText id="it140" autoComplete="off"
binding="#{pageFlowScope.welljobs_bean.applyWITSMLFilterIndicator.curvesFilter}"
dimensionsFrom="content" editable="inherit" rendered="true"/>
Bean:
private transient RichInputText curvesFilter;
public void setCurvesFilter(RichInputText curvesFilter) {
this.curvesFilter = curvesFilter;
}
public RichInputText getCurvesFilter() {
return curvesFilter;
}
Can anybody please help?
it is also giving me javax.faces.FacesException: javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property 'curvesFilter'. Exception
You can do this with EL Expression, partial trigger/autosubmit and ValueChangeEvent.
You want to save the boolean value of the checkedbox inside your bean so you can render or disable the inputText when this value change inside your valueChangeEventListener.
You then want to refresh the inputText so it will display it's new render/disable value by adding the following property to the inputText parent :
partialTriggers="sbc11"
partialTriggers refresh the whole content of a container when an action occur on the element id you give him.
Assuming you want to disable/enable the inputText :
Bean :
public boolean checkboxIsChecked = false; //or private with getter and setter
public void checkBoxValueChange(ValueChangeEvent ve){
this.checkboxIsChecked = ve.getNewValue();
}
Jsf :
<af:selectBooleanCheckbox label="Apply WITSML Filter" id="sbc11"
autoSubmit="true" contentStyle="margin-left:10px;" valueChangeListener="#
{pageFlowScope.welljobs_bean.checkBoxValueChange}"/>
...
<af:inputText id="it140" autoComplete="off"
binding="#{pageFlowScope.welljobs_bean.applyWITSMLFilterIndicator.curvesFilter}"
dimensionsFrom="content" disabled="#{pageFlowScope.welljobs_bean.checkboxIsChecked}"/>
don't forget to add the partialTriggers="CHECKBOXID" to the inputText parent container
For official example see documentation : https://docs.oracle.com/cd/E16764_01/web.1111/b31973/af_lifecycle.htm#CIAHCFJF
I have list of check boxes inside rich:dataTable and I want to check all the boxes at once with a single check box from header column.
<rich:column id="includeInWHMapping" >
<f:facet name="header">
<h:selectBooleanCheckbox value="#{checkallbox.selectAll}">
<f:ajax actionListener="#{checkallbox.selectAllBox}" render="selectedForWHProcess" />
</h:selectBooleanCheckbox>
</f:facet>
<h:selectBooleanCheckbox id="selectedForWHProcess" value="#{checkallbox.checked[data]}">
<f:ajax actionListener="#{checkallbox.selectAllRows}"/>
</h:selectBooleanCheckbox></rich:column>
Code in checkallbox Bean:
private Map<StandardStructure, Boolean> checked = new HashMap<StandardStructure, Boolean>();
private boolean selectAll;
public boolean isSelectAll() {
return selectAll;
}
public void setSelectAll(boolean selectAll) {
this.selectAll = selectAll;
}
public Map<StandardStructure, Boolean> getChecked() {
return checked;
}
public void setChecked(Map<StandardStructure, Boolean> checked) {
this.checked = checked;
}
public void selectAllBox(ValueChangeEvent e){
boolean newSelectAll = (Boolean) e.getNewValue();
Iterator<StandardStructure> keys = checked.keySet().iterator();
while(keys.hasNext()){
StandardStructure ss = keys.next();
checked.put(ss, newSelectAll);
}
}
When I check the h:selectBooleanCheckBox of header column nothing happens. What am I missing here? Should I have to implement Map for "selectAll" property too?
Thanks.
First of all. f:ajax doesn't have actionListener, it has a listener. Read the docs here. Second thing, you can use valueChangeListener on h:selectBooleanCheckbox and only there. Third, listener inside rows boxes is wrong. Basically, it looks like you need to read this topic.
Now, here is the working example:
<h:form>
<rich:dataTable value="#{checkallbox.allValues}" var="data" id="dataTable">
<rich:column>
<f:facet name="header">
<h:selectBooleanCheckbox value="#{checkallbox.selectAll}"
valueChangeListener="#{checkallbox.selectAllBox}">
<f:ajax render="dataTable" />
</h:selectBooleanCheckbox>
</f:facet>
<h:selectBooleanCheckbox value="#{checkallbox.checked[data]}">
<f:ajax />
</h:selectBooleanCheckbox>
</rich:column>
<!-- other columns -->
</rich:dataTable>
</h:form>
Other possible problems with your code (since you've shared just a part).
The data table needs to be in form, since you're executing ajax inside.
Your keys in map are objects. You have to make sure that equals method is good. In 95% of case the default is not, especially if they are #Entity.
You have to make sure that the map is populated with false at the beginning. I use #PostConstruct:
#PostConstruct
protected void performPostConstructAction() {
for (StandardStructure s : getAllValues()) {
checked.put(s, false);
}
}
Thanks Emil! I solved it.
public void selectAllBox(){
if(!selectAll){
for(StandardStructure item : ssTable.getDto()){
checked.put(item, true);
}
}else{
for(StandardStructure item : ssTable.getDto()){
checked.put(item, false);
}
}
}
I'm need primefaces tree in "checkbox" selection mode, but only one and only one node can be selected at a time. By default, primefaces tree selects all descendants when a node is selected and that is actually what I want to change.
Can anybody help me figure it out, please?
I finally found a way to realize this by looking at the Javascript source code of the tree. You can e.g. create a singleton that caches the previously selected node. By using the "onNodeClick" attribute of the tree you can call a Javascript function that unselects the previous node before the widget internally sets the new selected node (and potentially posts the new selection when using ajax events).
Update:
I modified the Facelet and the Javascript to search the tree for a preselected node on initialization. Be aware that the preselected node has to be visible to make it work correctly. See this answer for details about expanding the parent nodes.
Bean:
#Named
#ViewScoped
public class BackingBean implements Serializable {
private TreeNode rootTreeNode;
// IMPORTANT: Use array!
private TreeNode[] selected;
public TreeNode getRootTreeNode() {
if (rootTreeNode == null) {
rootTreeNode = new DefaultTreeNode("Root", null);
// init child tree nodes
}
return rootTreeNode;
}
public TreeNode[] getSelected() {
return selected;
}
public void setSelected(TreeNode[] selected) {
this.selected = selected;
}
}
Facelet:
<p:tree id="tree"
onNodeClick="TREE_SELECTION.updateSelection(node, event)"
propagateSelectionDown="false" propagateSelectionUp="false"
selection="#{backingBean.selected}" selectionMode="checkbox"
value="#{backingBean.rootTreeNode}"
var="data"
widgetVar="treeWidget">
<p:treeNode>
<h:outputText value="#{dataWrapper.label}" />
</p:treeNode>
</p:tree>
Javascript:
<script type="text/javascript">
// singleton for tree selection
var TREE_SELECTION = {
init: function (treeWidgetVar) {
this.treeWidget = PF(treeWidgetVar);
this.selectedNode = this.treeWidget.jq.find('.ui-treenode-selected');
},
treeWidget: null,
selectedNode: null,
updateSelection: function (node, event) {
// get the checkbox state of the clicked node
var checkbox = node.find('> .ui-treenode-content > .ui-chkbox'),
checked = checkbox.find('> .ui-chkbox-box > .ui-chkbox-icon').hasClass('ui-icon-check');
if (checked) {
// checkbox is going to be unchecked
this.selectedNode = null;
return;
}
// check for previously selected node
if (this.selectedNode !== null) {
// get the checkbox of the previously selected node
checkbox = this.selectedNode.find('> .ui-treenode-content > .ui-chkbox');
// update tree
this.treeWidget.uncheck(checkbox);
this.treeWidget.removeFromSelection(this.treeWidget.getRowKey(this.selectedNode));
}
// cache selected node
this.selectedNode = node;
}
};
// initialize singleton when document is loaded
$(function () {
TREE_SELECTION.init('treeWidget');
});
</script>
I've a listener on cellClick, I get the selected Record but I can't find a way to understand if this record is checked
Method ListGrid.isSelected(ListGridRecord) returns true if row is selected, not if is checked
My Code:
listGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
listGrid.addCellClickHandler(new CellClickHandler() {
#Override
public void onCellClick(CellClickEvent event) {
if(event.getColNum() == 0 && idMenu != null){
boolean isChecked = event.getRecord().???;
if(isChecked)
....
else
....
}
I've tried also with event.getRecord().getAttributeAsBoolean("_checkField") with no success...
I found a simply solution...
My task is solved using a special boolean field in the DataSource named, for example, "checked"
In ListGrid I've a field "checked", and with a RecordClickHandler I can manage check or uncheck event.
DataSource code:
DataSourceBooleanField checkField = new DataSourceBooleanField("checked");
ListGrid code:
listGrid.addRecordClickHandler(new RecordClickHandler() {
#Override
public void onRecordClick(RecordClickEvent event) {
Record rec = event.getRecord();
boolean checked = rec.getAttributeAsBoolean("checked");
if(checked){
...
}else{
...
}
rec.setAttribute("checked", !checked);
catPgrid.saveAllEdits();
catPgrid.refreshFields();
}
});
ListGridField checkField = new ListGridField("checked", "Sel");
Maybe getSelectedRecords() method would help you!
Here is an API reference: http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/widgets/grid/ListGrid.html#getSelectedRecords()
Definitely this will provide all records which are selected (using checkbox) but there should be some values which you could use for identifying each record uniquely!