Hi i tried the following functionality using primefaces.
In my xhtml i have a input field which takes input and saves into db.After completion of save i display the saved data in data table of primefaces.
Upto now works fine.now i add single selection to datatable .when user click on the row i update the same input column with selected data.
For this i use the following code
<h:form id="form">
<p:growl id="messages" />
<p:panel id="panel" header="Currency">
<h:panelGrid columns="2" id="currencyGrid">
<p:outputLabel value="Currency :"/>
<p:inputText value="#{currencyBean.currencyMaster.currDesc}"/>
</h:panelGrid>
<p:commandButton value="Add" action="#{currencyBean.saveCurrency}"
update="currencytable">
</p:commandButton>
<p:dataTable id="currencytable" var="cur"
value="#{currencyBean.currencyList}" widgetVar="dlg"
selection="#{currencyBean.currencyMaster}"
selectionMode="single" rowKey="#{cur.currId}">
<p:ajax event="rowSelect" listener="#{currencyBean.onRowSelect}"
update=":form:currencyGrid" />
<p:column headerText="ID" style="font-size:12px;">
<h:outputText value="#{cur.currId}" />
</p:column>
<p:column headerText="Description" style="font-size:12px;">
<h:outputText value="#{cur.currDesc}" />
</p:column>
</p:dataTable>
</h:form>
And My Bean code is:
#managedBean
#viewScoped
#component
public class CurrencyBean implements Serializable{
private static final long serialVersionUID = 1L;
#Autowired
private CurrencyMaster currencyMaster;
private List<CurrencyMaster> currencyList;
public CurrencyMaster getCurrencyMaster() {
return currencyMaster;
}
public void setCurrencyMaster(CurrencyMaster currencyMaster) {
this.currencyMaster = currencyMaster;
}
public void saveCurrency(){
try{
currencyService.saveCurrency(currencyMaster);
}catch(Exception e){
e.printStackTrace();
}
}
#PostConstruct
public void currencyList(){
setCurrencyList(currencyService.getCurrencyList());
}
public void onRowSelect(SelectEvent event) {}
public void onRowUnselect(UnselectEvent event) {}
}
So when i click on currrencytable row the input field populated properly and update operation also.
The problem is when i directly enter the data into input column my globalMaster property is going to be null.
And the following error is occured;
attempt to create saveOrUpdate event with null entity
I change the scopes of the bean but i did not find the solution.
And i find more solutions for the above exception but my problem is the new values are does not set to currencymaster.
Is there any mistake i done in this context.please guide me..
Related
I have a table p:dataTable. When I select an item, it's refresh another p:dataTable with associed values.
It's works fine. But when I select an item in the second table, the page (xhtml) don't get the value from the bean.
In debug mode I see that the value is updated on bean, but acessing #{bean.value} is always null.
Where is the codes:
<h:form id="form">
<h:panelGrid columns="2">
<!-- FRIST TABLE -->
<p:dataTable id="alunos" var="aluno" emptyMessage="Nenhum aluno cadastrado" value="#{alunos.usuarios}" selectionMode="single" selection="#{alunos.usuarioSelecionado}" rowKey="#{aluno.id}">
<f:facet name="header">
Alunos
</f:facet>
<p:ajax event="rowSelect" listener="#{alunos.onRowSelect}" update=":form:disci" />
<p:column headerText="Nome" width="60%">
<h:outputText value="#{aluno.nome}" />
</p:column>
<p:column headerText="Curso">
<h:outputText value="Default" />
</p:column>
</p:dataTable>
<!-- FUNCTIONS FOR FRIST TABLE THAT ARE WORKING -->
<p:column rowspan="4">
<p:commandButton value="Adicionar" update="alunos" icon="ui-icon-plus" actionListener="#{alunos.addUser()}"/>
<p:commandButton value="Editar" icon="ui-icon-pencil" actionListener="#{alunos.editarUser()}"/>
<p:commandButton value="Remover" icon="ui-icon-close" actionListener="#{alunos.apagaUser()}"/>
</p:column>
</h:panelGrid>
<p:outputLabel for="disci" value="Disciplinas que o aluno cursa" />
<!-- SECOND TABLE - Updated when an iten is selected in frist table (working)-->
<p:dataTable id="disci" var="disciplina" emptyMessage="Selecione um aluno" value="#{alunos.disciplinas}" selectionMode="single" selection="#{alunos.disciplinaSelecionada}" rowKey="#{disciplina.id}">
<p:ajax event="rowSelect" listener="#{alunos.onRowSelectDisciplina}" oncomplete="PF('dDialog').show()"/>
<p:column headerText="Nome" width="80%">
<h:outputText value="#{disciplina.nome}" />
</p:column>
</p:dataTable>
<!-- Dialog to show informations - ALWAYS NULL -->
<p:dialog header="Info" widgetVar="dDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="false">
<p:outputPanel id="detail" style="text-align:center;">
<p:panelGrid columns="2" columnClasses="label,value">
<h:outputText value="Id:" />
<h:outputText value="#{alunos.disciplinaSelecionada.id}" />
<h:outputText value="Nome" />
<h:outputText value="#{alunos.disciplinaSelecionada.nome}" />
</p:panelGrid>
</p:outputPanel>
</p:dialog>
</h:form>
And the Bean:
#ManagedBean(name = "alunos")
#ViewScoped
public class Alunos extends NovoUsuario {
private Disciplina disciplinaSelecionada;
public Alunos() {
super(TiposUsuario.ALUNO);
}
//Method called when an iten is selected in SECOND TABLE
//In debug, the Object Disciplina is correct, as selected in table.
public void onRowSelectDisciplina(SelectEvent event) {
this.disciplinaSelecionada = (Disciplina) event.getObject();
}
//getters and setters
}
extended class:
public class NovoUsuario implements Serializable{
private List<Usuario> usuarios;
protected Usuario usuarioSelecionado;
TiposUsuario tipo;
private String nome;
private String senha;
protected List<Disciplina> disciplinas;
//Constructor
public NovoUsuario(TiposUsuario tipo) {
this.tipo = tipo;
}
//Initialize FRIST TABLE
#PostConstruct
public void init() {
UsuarioCRUD usuarioCRUD = new UsuarioCRUD();
this.usuarios = usuarioCRUD.listarTipoUsuario(tipo);
}
//Method used in FRIST TABLE
public void onRowSelect(SelectEvent event) {
disciplinas = getDisciplinaUsuario();
}
//Method that get data to SECOND table when a row is selectd in frist
public List<Disciplina> getDisciplinaUsuario() {
DisciplinaCRUD dcrud = new DisciplinaCRUD();
if (usuarioSelecionado != null) {
return dcrud.listaDisciplinasUsuario(usuarioSelecionado);
}
return null;
}
public void salvaUsuario() {
/* code to create*/
}
public void editarUser(){
/* code to update*/
}
public void apagaUser(){
/* code to delete*/
}
//GETTERS AND SETTERS
}
image: http://i.stack.imgur.com/Qbs0T.png
repository: https://bitbucket.org/VagnerGon/novo-academico
I'd say you just need to update the dialogs content from the p:ajax in the second table - just as you update the second datatable, when you select in the first.
So add id="dialog" to the dialog, and make the p:ajax in the second table
<p:ajax event="rowSelect" listener="#{alunos.onRowSelectDisciplina}" update=":form:dialog" oncomplete="PF('dDialog').show()" />
Or omit the new id and just do update=":form:detail".
I am trying to use a <p:dataTable> to display the contents of an object selected from a <p:selectOneMenu> list.
The <p:selectOneMenu> is correctly populated with options. However:
The <p:dataTable> is not updated when I select an item from the <p:selectOneMenu>
The server log indicates that the value of #{MB.selectedDatabase} is null.
#{MB.setSelectedDatabase()} is never invoked.
XHTML:
<h:form id="form">
<h:messages id="messages" />
<p:panelGrid columns="2">
<p:outputLabel value="Data:" />
<p:selectOneMenu immediate="true" id="customDatas" value="#{MB.selectedDatabase}" >
<p:ajax execute="customDatas" render="infos" listener="#{MB.whatIsSelected()}" />
<f:selectItem itemLabel="Select Data" itemValue="" />
<f:selectItems value="#{MB.datas}" var="data" itemLabel="#{data.name}" itemValue="#{data.id}"/>
</p:selectOneMenu>
</p:panelGrid>
<p:dataTable value="#{MB.selectedDatabase.infos}" var="item" id="infos" >
<p:column >
<f:facet name="header">
<h:outputText value="Info"/>
</f:facet>
<h:outputText value="#{item.info}"/>
</p:column>
</p:dataTable>
</h:form>
Partial code from my sessionScoped Bean:
public Data selectedDatabase;
public void whatIsSelected(){
logger.log(Level.SEVERE, "Selected data is : {0}", selectedDatabase.getName());
infoBusinessLocal.getinfosbydata(selectedDatabase);
}
public Data getSelectedDatabase() {
return selectedDatabase;
}
public void setSelectedDatabase(Data selectedDatabase) {
logger.log(Level.SEVERE, "Set selectedDB is invoked");
this.selectedDatabase = selectedDatabase;
}
SelectOneMenu with Pojo classes is needed a JSf converter between page and managed bean. Converter converts string from page to objects in managed beans.
#FacesConverter("dataConverter")
public class DataConverter implements Converter {
#Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
//return proper object, value is the string on the page
}
#Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) {
//return the string value of object which will be presented on the page
}
}
Add converter attribute to SelectOneMenu
<p:selectOneMenu immediate="true" id="customDatas" value="#{MB.selectedDatabase}" converter="dataConverter">
See also
BalusC SelectOneMenu tutorial
I have a datatable where I am showing List of Employees. At the end of the datatable I have a commandbutton. Now if user selects any checkbox inside the datatable, I want my commandbutton to be enabled. Initially the command button is disabled.
Below is my index.xhtml page
<p:dataTable value="#{userPreferencesBean.studentDataModel}"
var="studentDetails" emptyMessage="No Student found."
selection="#{userPreferencesBean.selectedStudents}">
<f:facet name="header">
List of Students
</f:facet>
<p:ajax event="rowSelect" update="submit" listener="#{userPreferencesBean.onRowSelect}"/>
<p:ajax event="rowUnselect" update="submit" listener="#{userPreferencesBean.onRowSelect}"/>
<p:column selectionMode="multiple" style="width:2%" />
//All the columns
<f:facet name="footer">
<p:commandButton id="submit" value="Save Preferences"
icon="ui-icon-disk" style="float:right;"
action="#{userPreferencesBean.savePreferredInterfaces}"
update=":#{p:component('selectedInterfaceDetails')}"
disabled="#{not userPreferencesBean.hasPreferenceChanged eq false}"/>
</f:facet>
</p:dataTable>
Below is my backing bean:
#ManagedBean
#SessionScoped
public class UserPreferencesBean implements Serializable {
private boolean hasPreferenceChanged = false;
public void onRowSelect(SelectEvent event) {
this.setHasPreferenceChanged(true);
}
public boolean isHasPreferenceChanged() {
return hasPreferenceChanged;
}
public void setHasPreferenceChanged(boolean hasPreferenceChanged) {
this.hasPreferenceChanged = hasPreferenceChanged;
}
What I am expecting is that once I select a row, my submit button should be enabled. But it is not happening. Could you please help me to understand where I am doing wrong? Thanks.
With regards,
Sudipta Deb
Try put your table into form.
Also you can make it more convient:
<p:commandButton id="submit" ... disabled="#{userPreferencesBean.selectedStudents==null}"/>
For some strange reason I can't manage to process my data when I'm using <p:ajax event="sort" inside <p:dataTable , while it works just fine for <p:ajax event="page" and <p:ajax event="filter"
I'm using myfaces 2.0.11 with primefaces 3.4 (tried with 4.0 snapshot)
If I edit my cell input and do PAGINATE I do see the updated value of the cell in the listener method
If i edit my cell input and do SORT I do NOT see the updated value of the cell in the listener method, Any Idea why? I mean both p:ajax (sort/page) got the process="#form" so why its does not process? I inspected the network of the chrome dev tools and in both cases (sort/page) the relevant updated values are being sent to the server, but for some reason in the sort listener the values are not updated ,
b.t.w the only suspicious difference between page and sort ajax request is the following parameter that is present only in the sort request is:
my_table_skipChildren:true , what does it mean? skip from processing ?
I can grab the relevant ids+value from the request from the ExternalContext, but it will be last resort only
Here is a really simple example
Session Scoped Bean code:
#ManagedBean
#SessionScoped
public class myBean {
private List<MyObject> myList = new ArrayList<MyObject>();
#PostConstruct
public void init() {
myList.add(new MyObject("1", "2"));
myList.add(new MyObject("11", "22"));
myList.add(new MyObject("111", "222"));
myList.add(new MyObject("1a", "2a"));
myList.add(new MyObject("11a", "22a"));
myList.add(new MyObject("111a", "222a"));
}
public List<MyObject> getMyList() {
return myList;
}
public void setMyList(List<MyObject> myList) {
this.myList = myList;
}
}
MyObject code
public class MyObject {
private String one;
private String two;
public MyObject(String one, String two) {
super();
this.one = one;
this.two = two;
}
public String getOne() {
return one;
}
public void setOne(String one) {
this.one = one;
}
public String getTwo() {
return two;
}
public void setTwo(String two) {
this.two = two;
}
}
XHTML :
<p:dataTable id="my_table" value="#{myBean.myList}"
var="deviceRow" widgetVar="myTable"
paginator="true" rows="4" paginatorPosition="bottom"
paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}">
<p:ajax process="#form" event="sort" listener="#{myBean.detectSortEvent}"></p:ajax>
<p:ajax process="#form" event="page" listener="#{myBean.detectPageEvent}"></p:ajax>
<p:column id="table_selection_column_one" sortBy="#{deviceRow.one}" filterBy="#{deviceRow.one}">
<f:facet name="header">
<h:outputText value="Column One" />
</f:facet>
<h:inputText id="myRow_One" value="#{deviceRow.one}" />
</p:column>
<p:column id="table_selection_column_two" sortBy="#{deviceRow.two}" filterBy="#{deviceRow.two}">
<f:facet name="header">
<h:outputText value="Column Two" />
</f:facet>
<h:inputText id="myRow_Two" value="#{deviceRow.two}" />
</p:column>
</p:dataTable>
(I also asked it on primefaces forum but got no response)
Tried Andy workaround (adding editable="true" to table, but its no good
The scenario is as follows: change some value of an input text , than click on table column header to do sort
Expectations: Inside detectSortEvent listener I see the updated value inside the list
Reality: Same old value in the list
Regards,
Daniel.
With a simple class wich contains only a list of list of String, I can view all its elements in a p:dataTable with the get(int) method of class List but it fails when I try to export this with the p:dataExporter component.
Here's the Java source code :
#ManagedBean
#SessionScoped
public class MainClass {
private List<ArrayList<String>> listOfListOfString;
public List<ArrayList<String>> getListOfListOfString() {
return listOfListOfString;
}
public void setListOfListOfString(List<ArrayList<String>> listOfListOfString) {
this.listOfListOfString = listOfListOfString;
}
public MainClass() {
listOfListOfString = new ArrayList<ArrayList<String>>();
ArrayList<String> firstList = new ArrayList<String>();
firstList.add("a");
firstList.add("b");
listOfListOfString.add(firstList);
ArrayList<String> secondList = new ArrayList<String>();
secondList.add("1");
secondList.add("2");
listOfListOfString.add(secondList);
}
}
And the XTHML code :
<h:body>
Hello from Facelets
<h:form prependId="false">
<p:dataTable id="tbl" var="myList" value="#{mainClass.listOfListOfString}" paginator="true" rows="10" >
<p:column headerText="First column">
<h:outputText value="#{myList.get(0)}"/>
</p:column>
<p:column headerText="Second column">
<h:outputText value="#{myList.get(1)}"/>
</p:column>
</p:dataTable>
<h:commandLink value=" csv">
<p:dataExporter type="csv" target="tbl" fileName="userList" />
</h:commandLink>
</h:form>
</h:body>
And the error stack when I click on the csv link:
For input string: "get"
- Stack Trace
java.lang.NumberFormatException: For input string: "get"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at javax.el.ListELResolver.coerce(ListELResolver.java:173)
at javax.el.ListELResolver.getType(ListELResolver.java:72)
Do you have an idea why p:dataTable can use the get(int) method and p:dataExporter can't?
Thanks,
Philippe
Probably both components are implemented differently.
But you can access list elements this way:
#{myList[n]}
where n is the list index.