Reputation: 920
i have a search query that should search a database and return all the details that match that username or contain any part of the search string
<h:form id="searchForm">
<p:outputLabel value="Search: "/>
<h:inputText id="search" value="#{userdetailsController.search}" />
<p:commandButton value="Search" action="#{userdetailsController.submit}" ajax="true" update="UserTableSearch"/>
<br></br>
<br></br>
<p:dataTable id="UserTableSearch"
widgetVar="UserTableSearch"
paginator="true" rows="10"
value="#{userdetailsController.item}"
var="item"
emptyMessage="No details was found with given criteria">
<p:column id="id"
headerText="i.d."
sortBy="id">
<f:facet name="header">
<h:outputText value="#{bundle.ListUserdetailsTitle_id}"/>
</f:facet>
<h:outputText value="#{userdetails.username}"/>
</p:column>
<p:column id="username"
headerText="username"
sortBy="username">
<f:facet name="header">
<h:outputText value="#{bundle.ListUserdetailsTitle_username}"/>
</f:facet>
<h:outputText value="#{userdetails.username}"/>
</p:column>
</p:dataTable>
</h:form>
that is the view, where the user can search the username, it should automatically then update the datatable with the search results, however currently is displaying the empty message of
emptyMessage="No details was found with given criteria"
how can i check to see if the data being passed to the view contains some values,
this is the userdetails controller it calls
@Named("userdetailsController")
@SessionScoped
public class UserdetailsController implements Serializable {
private Userdetails current;
private DataModel items = null;
@EJB
private Richard.beans.UserdetailsFacade ejbFacade;
private PaginationHelper pagination;
private int selectedItemIndex;
private String search;
private List<Userdetails> item; // No need for DataModel here.
public List<Userdetails> getItem() {
return item;
}
public String submit() {
item = ejbFacade.searchByString(search);
return ""; //change this !, testing only
}
public String getSearch() {
return search;
}
public void setSearch(String search) {
this.search = search;
}
public UserdetailsController() {
}
public Userdetails getSelected() {
if (current == null) {
current = new Userdetails();
selectedItemIndex = -1;
}
return current;
}
private UserdetailsFacade getFacade() {
return ejbFacade;
}
public PaginationHelper getPagination() {
if (pagination == null) {
pagination = new PaginationHelper(10) {
@Override
public int getItemsCount() {
return getFacade().count();
}
@Override
public DataModel createPageDataModel() {
return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
}
};
}
return pagination;
}
public String prepareList() {
recreateModel();
return "List";
}
public String prepareView() {
current = (Userdetails) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
return "View";
}
public String prepareCreate() {
current = new Userdetails();
selectedItemIndex = -1;
return "Create";
}
public String create() {
try {
getFacade().create(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UserdetailsCreated"));
return prepareCreate();
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
public String prepareEdit() {
current = (Userdetails) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
return "Edit";
}
public String update() {
try {
getFacade().edit(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UserdetailsUpdated"));
return "View";
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
public String destroy() {
current = (Userdetails) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
performDestroy();
recreatePagination();
recreateModel();
return "List";
}
public String destroyAndView() {
performDestroy();
recreateModel();
updateCurrentItem();
if (selectedItemIndex >= 0) {
return "View";
} else {
// all items were removed - go back to list
recreateModel();
return "List";
}
}
private void performDestroy() {
try {
getFacade().remove(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UserdetailsDeleted"));
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
}
}
private void updateCurrentItem() {
int count = getFacade().count();
if (selectedItemIndex >= count) {
// selected index cannot be bigger than number of items:
selectedItemIndex = count - 1;
// go to previous page if last page disappeared:
if (pagination.getPageFirstItem() >= count) {
pagination.previousPage();
}
}
if (selectedItemIndex >= 0) {
current = getFacade().findRange(new int[]{selectedItemIndex, selectedItemIndex + 1}).get(0);
}
}
//datatable for all results
public DataModel getItems() {
if (items == null) {
items = getPagination().createPageDataModel();
}
return items;
}
private void recreateModel() {
items = null;
}
private void recreatePagination() {
pagination = null;
}
public String next() {
getPagination().nextPage();
recreateModel();
return "List";
}
public String previous() {
getPagination().previousPage();
recreateModel();
return "List";
}
public SelectItem[] getItemsAvailableSelectMany() {
return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
}
public SelectItem[] getItemsAvailableSelectOne() {
return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
}
public Userdetails getUserdetails(java.lang.Integer id) {
return ejbFacade.find(id);
}
@FacesConverter(forClass = Userdetails.class)
public static class UserdetailsControllerConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
UserdetailsController controller = (UserdetailsController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "userdetailsController");
return controller.getUserdetails(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuilder sb = new StringBuilder();
sb.append(value);
return sb.toString();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof Userdetails) {
Userdetails o = (Userdetails) object;
return getStringKey(o.getId());
} else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Userdetails.class.getName());
}
}
}
}
Thanks
Upvotes: 0
Views: 115
Reputation: 536
Your getItem()
method is called to populate the datable. In that method you can iterate through the list or just print out the number of items.
Also in submit()
check to see if there's any value in search
or if it's being called at all. I suspect search
is null.
Your inputtext is not a PrimeFaces component. I would change it to PrimeFaces. There's render time differences which may account for search string not being there.
Upvotes: 1