How can I use a list in ListShuttle
khodeprasad Oct 22, 2011 10:05 AMHi,
Can we display a dropdown along with some label in a list shuttle? if yes can you please tell me how we can use it...
Here how i tried to get the problem solved, but i dont know where I am going wrong...
Initially it is displaying as expected as shown in the attachment and when i click copy button selecting option n try to get that option in other side of the list shuttle it is displaying the error message ""Component j_id_jsp_1759698581_1pc4:studentsList has invalid value expression" and all the list which were populated with some values are becoming empty n populating empty dropdown in the list shuttle....
In converter i tried to read the value of the list n convert it into appropriate list n it says "Cannot cast from String to List"
Even I defined a list with in the converter n insteading of converting it into string i coppied all the list items into the list available in the converted and tried to use that list. Still no luck...
Pls point me where i am going wrong.... n can you explain me is there any other way to achieve this....
Here is the code what i tried...
Configure.jsp
--------------------
<rich:listShuttle id="studentsList" sourceValue="#{student.list}" targetValue="#{student.newList}" var="item" listsHeight="150"
sourceListWidth="200" targetListWidth="200" targetRequired="true" converter="studentMappingConverter">
<rich:column breakBefore="true">
<h:outputText value="#{item.studentName} " />
</rich:column>
<rich:column>
<a4j:region>
<h:selectOneMenu id="stops" value="#{item.stopSelected}">
<f:selectItems value="#{item.stopsList}" />
</h:selectOneMenu>
</a4j:region>
</rich:column>
<a4j:support event="onlistchanged" reRender="config" />
</rich:listShuttle>
</a4j:region>
Converter Code:
-------------------------------
public class StudentRouteMappingConverter implements javax.faces.convert.Converter
{
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
StudentRouteMappingBean student = new StudentRouteMappingBean();
int index = value.indexOf(':');
student.setStudentName(value.substring(0, index));
student.setRollNo(Integer.parseInt(value.substring(index+1, value.indexOf('$'))));
index = value.indexOf('$');
student.setStudentId(Integer.parseInt(value.substring(index+1, value.indexOf('^'))));
index = value.indexOf('^');
student.setStopName(value.substring(index+1, value.indexOf('#')));
index = value.indexOf('#');
student.setStopSelected(Integer.parseInt(value.substring(index+1, value.indexOf('!'))));
index = value.indexOf('!');
//List<SelectItem> stopsList = (List<SelectItem>)((Object)value.substring(index+1)); // It says cannot cast from string to list
//student.setStopsList(stopsList);
student.setStopsList(tempList);
/*for(int i=0; i<tempList.size(); i++)
{
SelectItem option = new SelectItem(tempList.get(index).getValue(), tempList.get(index).getLabel());
student.getStopsList().add(option);
}*/
return student;
}
public String getAsString(FacesContext context, UIComponent component,Object value)
{
StudentRouteMappingBean item = (StudentRouteMappingBean) value;
String tempValue = item.getStudentName().trim() + ":" + item.getRollNo() + "$" + item.getStudentId() + "^" + item.getStopName() + "#"
+ item.getStopSelected() + "!" + item.getStopsList();
for(int index=0; index<item.getStopsList().size(); index++)
{
SelectItem option = new SelectItem(item.getStopsList().get(index).getValue(), item.getStopsList().get(index).getLabel());
tempList.add(option);
}
return tempValue;
}
private List<SelectItem> tempList = new ArrayList<SelectItem>();
// Trying to use a temp list so that the list can be used in both methods
}
Bean File is :
-------------------
public class StudentRouteMappingBean {
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public int getStopSelected() {
return stopSelected;
}
public void setStopSelected(int stopSelected) {
this.stopSelected = stopSelected;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStopName() {
return stopName;
}
public void setStopName(String stopName) {
this.stopName = stopName;
}
public List<SelectItem> getStopsList() {
return stopsList;
}
public void setStopsList(List<SelectItem> stopsList) {
this.stopsList = stopsList;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + rollNo;
result = prime * result + ((stopName == null) ? 0 : stopName.hashCode());
result = prime * result + stopSelected;
result = prime * result + ((stopsList == null) ? 0 : stopsList.hashCode());
result = prime * result + studentId;
result = prime * result + ((studentName == null) ? 0 : studentName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
StudentRouteMappingBean other = (StudentRouteMappingBean) obj;
if (rollNo != other.rollNo)
return false;
if (stopName == null) {
if (other.stopName != null)
return false;
} else if (!stopName.equals(other.stopName))
return false;
if (stopSelected != other.stopSelected)
return false;
if (stopsList == null) {
if (other.stopsList != null)
return false;
} else if (!stopsList.equals(other.stopsList))
return false;
if (studentId != other.studentId)
return false;
if (studentName == null) {
if (other.studentName != null)
return false;
} else if (!studentName.equals(other.studentName))
return false;
return true;
}
private int studentId = 0;
private int rollNo = 0;
private int stopSelected = 0;
private String studentName = "";
private String stopName = "";
private List<SelectItem> stopsList = new ArrayList<SelectItem>();
}
Thanks in advance....