JSF selectBooleanCheckbox always checked - jsf

Good afternoon in my timezone.
<ui:repeat id="situacoes-edit-list" var="situacao" varStatus="loop" value="#{cc.attrs.managedBean.situacoesEditDTO}">
<tr id="sitEdit#{situacao.situacaoId}" class="situations">
<td colspan = "2">#{situacao.situacaoNome}</td>
</tr>
<ui:repeat id="justificacoes-list" var="justificacao" varStatus="innerLoop" value="#{situacao.justificacoes}">
<tr id="jusEdit#{justificacao.justificacaoId}" class="justifications">
<td>
<h:selectBooleanCheckbox binding="#{chkJust}" id="chk-just-#{chkJust.clientId}" value="#{justificacao.selected}" />
</td>
<td>#{justificacao.selected}</td>
</tr>
</ui:repeat>
</ui:repeat>
In the Bean the the get and set methods are the follow:
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
The resulting HTML
<tr id="jusEdit6" class="justifications">
<td><input id="container-edit-form:intervencaoJustificacoes:situacoes-edit-list:0:justificacoes-list:0:chk-just-j_id3" type="checkbox" name="container-edit-form:intervencaoJustificacoes:situacoes-edit-list:0:justificacoes-list:0:chk-just-j_id3" checked="checked" />
</td>
<td>false</td>
</tr>
<tr id="jusEdit2" class="justifications">
<td><input id="container-edit-form:intervencaoJustificacoes:situacoes-edit-list:0:justificacoes-list:1:chk-just-j_id3" type="checkbox" name="container-edit-form:intervencaoJustificacoes:situacoes-edit-list:0:justificacoes-list:1:chk-just-j_id3" checked="checked" />
</td>
<td>true</td>
</tr>
As you see the <td>#{justificacao.selected}</td> returns the correct value but the value="#{justificacao.selected}" does not produce the correct behaviour.
Why is this happening ?
I am using Mojarra 2.0.1
Thanks in advance
Best regards

Related

Selenium Python select and click multiple checkbox elements by input type

Trying to select and click multiple checkboxes on a page. they don't have a class, ID or names that match. All they have in common is their input type (checkbox). I'm able to select individual checkboxes using XPATH but its not practical as there's at least 100 checkboxes on the page.
Here's a section of the HTML. I've been testing this code on an off-line version of the site.
<body>
<tbody>
<tr>
<td class="borderBot"><b>Activity</b></td>
<td class="borderBot">
<table cellpadding="3" cellspacing="3" width="100%">
<tbody>
<tr>
<td width="33%">
<input type="checkbox" name="competency1Activity" value="1" />
Plan (ie Interpreted diag etc)
</td>
<td width="33%">
<input type="checkbox" name="competency1Activity" value="2" />
Carry Out (ie conducted work)
</td>
<td width="33%">
<input type="checkbox" name="competency1Activity" value="4" />
Complete (ie Compliance etc)
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td class="borderBot"><b>Supervision</b></td>
<td class="borderBot">
<table cellpadding="3" cellspacing="3" width="100%">
<tbody>
<tr>
<td width="33%">
<input
type="checkbox"
name="competency1Supervision"
value="1"
/>
Direct
</td>
<td width="33%">
<input
type="checkbox"
name="competency1Supervision"
value="2"
/>
General
</td>
<td width="33%">
<input
type="checkbox"
name="competency1Supervision"
value="4"
/>
Broad
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td class="borderBot"><b>Support</b></td>
<td class="borderBot">
<table cellpadding="3" cellspacing="3" width="100%">
<tbody>
<tr>
<td width="33%">
<input type="checkbox" name="competency1Support" value="1" />
Constant
</td>
<td width="33%">
<input type="checkbox" name="competency1Support" value="2" />
Intermittent
</td>
<td width="33%">
<input type="checkbox" name="competency1Support" value="4" />
Minimal
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td class="borderBot"><b>Materials</b></td>
<td class="borderBot">
<table cellpadding="3" cellspacing="3" width="100%">
<tbody>
<tr>
<td width="50%">
<input type="checkbox" name="competency1Extended" value="1" />
Insulation failure
</td>
<td width="50%">
<input type="checkbox" name="competency1Extended" value="2" />
Incorrect connections
</td>
</tr>
<tr>
<td width="50%">
<input type="checkbox" name="competency1Extended" value="4" />
Circuits-wiring; eg. open short
</td>
<td width="50%">
<input type="checkbox" name="competency1Extended" value="8" />
Unsafe condition
</td>
</tr>
<tr>
<td width="50%">
<input
type="checkbox"
name="competency1Extended"
value="16"
/>
Apparatus/component failure
</td>
<td width="50%">
<input
type="checkbox"
name="competency1Extended"
value="32"
/>
Related mechanical failure
</td>
</tr>
<tr>
<td width="50%">
<input
type="checkbox"
name="competency1Extended"
value="64"
/>
Read/interpret drawings/plans
</td>
<td width="50%">
<input
type="checkbox"
name="competency1Extended"
value="128"
/>
Other elec app and circuit faults
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<input
type="hidden"
id="competency1ExtendedCount"
name="competency1ExtendedCount"
value="8"
/>
</tbody>
</body>
Try 1 - this selects and clicks the first checkbox but none of the others
checkboxes = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']").click()
Try 2 - Thought this would work but I cant get the syntax right
checkboxes = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']")
for checkbox in checkboxes:
checkboxes.click()
time.sleep(.3)
Try 3 - able to select first checkbox (of 3) with this name
checkboxes = driver.find_element("name", "competency1Ativity").click()
find_element() will return only the first matching element, where as you need to identify all of them. So you need to use find_elements() method.
Solution
You can identify all the checkbox and store the elements within a list and then click on them one by one using either of the following locator strategies:
Using CSS_SELECTOR:
from selenium.webdriver.common.by import By
checkboxes = driver.find_elements(By.CSS_SELECTOR, "td input[type='checkbox']")
for checkbox in checkboxes:
checkbox.click()
Using XPATH:
from selenium.webdriver.common.by import By
checkboxes = driver.find_elements(By.XPATH, "//td//input[#type='checkbox']")
for checkbox in checkboxes:
checkbox.click()
Your option 2 was nearly correct, however it should have been find_elements() not find_element()
Since find_element() just returns an webelement where as find_elements() returns list of elements.
code:
checkboxes = driver.find_elements(By.CSS_SELECTOR, "input[type='checkbox']")
for checkbox in checkboxes:
checkbox.click()
time.sleep(0.5)

How to dynamically hide and show html <tr>

How to add code in OnInitializedAsync to show/hide the in C# razor pages?
<div class="form-row">
<table border="0" cellpadding="2" style="border-style:solid;border-width:2px">
<tbody>
<tr id="rowId" style="display: none;">
<td>
<label>test</label>
</td>
</tr>
</tbody>
</table>
</div>
#code {
protected override void OnInitializedAsync()
{
// code to show <tr> with id="rowId"
}
}
With the following code, when the component is initialized the table row is hidden. When the button is pressed it becomes visible.
<div class="form-row">
<table border="0" cellpadding="2" style="border-style:solid;border-width:2px">
<tbody>
#if (_rowVisible)
{
<tr id="rowId">
<td>
<label>test</label>
</td>
</tr>
}
</tbody>
</table>
</div>
<button #onclick="OnHideRowBtnClicked">Hide row</button>
#code {
private bool _rowVisible;
protected override void OnInitializedAsync()
{
// set _rowVisible to true if row should be visible or false if row should be hidden
_rowVisible = true;
}
private void OnHideRowBtnClicked()
{
_rowVisible = false;
}
}
Another approach using the style attribute:
<div class="form-row">
<table border="0" cellpadding="2" style="border-style:solid;border-width:2px">
<tbody>
<tr id="rowId" style="#_rowStyle">
<td>
<label>test</label>
</td>
</tr>
</tbody>
</table>
</div>
<button #onclick="OnHideRowBtnClicked">Hide row</button>
<button #onclick="OnShowRowBtnClicked">Show row</button>
#code {
private string? _rowStyle;
protected override void OnInitializedAsync()
{
}
private void OnHideRowBtnClicked()
{
_rowStyle = "display: none;";
}
private void OnShowRowBtnClicked()
{
_rowStyle = null;
}
}

Customized rendering of elements in datatable

I have n elements in an arraylist, I want to display all those in a table of (n/3) X 3 format (by single iteration) with respective number of radio buttons like,
<table>
<tbody>
<tr>
<td><input type="radio" name="category1" id="category1" value="1"></td>
<td><input type="radio" name="category1" id="category1" value="2"></td>
<td><input type="radio" name="category1" id="category1" value="3"></td>
</tr>
<tr>
<td><input type="radio" name="category1" id="category1" value="4"></td>
<td><input type="radio" name="category1" id="category1" value="5"></td>
<td><input type="radio" name="category1" id="category1" value="6"></td>
</tr>
...
<tr>
<td><input type="radio" name="category1" id="category1" value="n"></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
Please help
You may just use h:panelGrid and c:forEach
<h:panelGrid columns="3">
<c:forEach items="#{myBean.list}" var="item">
<h:outputText value="#{item}" />
</c:forEach>
</h:panelGrid>
It will render a <table />
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
...
</tbody>

JSF UIData doesn't render a UIColumn children

I try understand how JSF UIData () works programaticaly in JSF. I have following code:
UIInput input = new UIInput();
UIInput input2 = new UIInput();
UIOutput header = new UIOutput();
header.setValue("Header");
UIColumn column = new UIColumn();
column.setHeader(header);
column.getChildren().add(input);
column.getChildren().add(input2);
UIData table = new UIData();
table.getChildren().add(column);
this code render html like:
<table>
<thead>
<tr>
<th scope="col">Header</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
where is my two inputs?? i expect something like:
.....
<tbody>
<tr>
<td>
<input type="text" />
</td>
</tr>
<tr>
<td>
<input type="text" />
</td>
</tr>
</tbody>
......
i know what i do wrong. I have to set a collection of UIData for rows of table. When i set
UIData table = new UIData();
ArrayList list = new ArrayList();
list.add("A");
list.add("B");
table.setValue(list);
than i get two rows with my inputs

JSF SelectOneMenu selected item

I can't get the selected item from a SelectOneMenu. I supply an ArrayList to the menu and want the user to select one of it. I put the menu into a form, so I have a commandButton which I intended to use to perform the selection. This implementation gives me this error : Cannot convert user3 of type class java.lang.String to class java.util.ArrayList when I select from the menu "user3", so it actually performs the selection correctly. The error refers to this line
<h:selectOneMenu value="#{user.myUsers}"
Here is the part of my xhtml which generates the selectOneMenu.
<h:panelGrid columns="3">
<h:form>
<h:selectOneMenu value="#{user.myUsers}">
<f:selectItems value="#{user.myUsers }"/>
</h:selectOneMenu>
<h:commandButton value="#{msgs.remove_user}" action="#{user.select }" ></h:commandButton>
</h:form>
<h:outputText value="#{ user.select}"></h:outputText>
</h:panelGrid>
And here is my UserBean:
#ManagedBean(name="user")
#SessionScoped
public class UserBean implements Serializable {
private String selected;
public ArrayList<String> getMyUsers()throws Exception
{
ArrayList<String> ret;
MySQLConnection conn = new MySQLConnection();
try{
ret = conn.getMyUsers(name);
}finally
{
conn.closeConnection();
}
return ret;
}
public String getSelect() throws Exception
{
if (this.selected==null) return this.getMyUsers().get(0);
return this.selected;
}
public void setSelect(String s)
{
this.selected = s;
}
}
Your arraylist is mapped to
<f:selectItems value="#{user.myUsers}"/>,
and after selection you are trying to put the selected value to the same list:
<h:selectOneMenu value="#{user.myUsers}">
You should have some object (or string in your case) in your managed bean linked with your view and fill it by selected item of myUsers. For example:
private String selectedUser; // + appropriate getter and setter
and <h:selectOneMenu> should look like:
<h:selectOneMenu value="#{user.selectedUser}">
selected item sould be stored in selectedUser to the end of jsf lifecycle
<table class="tableClass" id="productDescriptionTable">
<thead>
<tr class="trPDClass">
<th class="thPDClass"></th>
<th class="thPDClass">Feature</th>
<th class="thPDClass">SubFeature</th>
`enter code here`<th class="thPDClass">Type</th>
<th class="thPDClass">Sub-Feature Value</th>
`enter code here`<th class="thPDClass">Is Active</th>
<th class="thPDClass">Deleted</th>
</tr>
</thead>
<tbody>
<tr class="trPDClass">
<td class="tdPDClass" style="width: 30;" ><input type="checkbox" /></td>
<td class="tdPDClass"><input type="text" id="0PDfeature" name="PDfeature" /></td>
<td class="tdPDClass"><input type="text" id="0PDsubFeature" name="PDsubFeature" /></td>
<td class="tdPDClass"><input type="text" id="0PDtype" name="PDtype" /></td>
<td class="tdPDClass">
<div id="0PDsubFeatureValueDiv" name="PDsubFeatureValueDiv"></div>
</td>
<td class="tdPDClass">
<table class="radioClass">
<tr>
<td width="24%"><input type="radio" name="PDisActive" id="PDisActiveY" value="Y" /></td>
<td width="40%">Yes</td>
<td width="20%"><input type="radio" name="PDisActive" id="PDisActiveN" value="N" /></td>
<td width="16%">No</td>
</tr>
</table>
</td>
<td class="tdPDClass">
<table class="radioClass">
<tr>
<td width="24%"><input type="radio" name="PDdeleted" id="0PDdeletedY" value="Y" /></td>
<td width="40%">Yes</td>
<td width="20%"><input type="radio" name="PDdeleted" id="0PDdeletedN" value="N" /></td>
<td width="16%">No</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>

Resources