Sunday, May 16, 2010

Google Suggest Implemented ....


Guys, this is my first blog that i have released so far. While working with the Seam framework, i have found out lots of tips n tricks which i will be sharing from now on. It took me a day to get it going in my project.

In the view page, just paste the code within the form.The rich:suggestionBox is having three attributes i.e minChars,for and suggestionAction which is worth noting. The first one is the character limit, which upon achieving triggers the calling of a method. Second one is a kind of notification sent to the container suggesting about the component(id) to which it has been applied.The final one is for triggering a method action which invokes autoComplete() written in the Java class that finally fetches data from the database.

Get the EntityManager object earlier as Ejb 3.0 has been used and then you are done i guess.


JSP CODE:

<h:inputText id="searchedVehicle" 
value="#{mapAction.searchedVehicle}" style="width:160px"
size="20"/>
<rich:suggestionbox height="150" width="163"
rules="#{suggestionBox.rules}" usingSuggestObjects="false"
nothingLabel="No Registration Number Found"
suggestionAction="#{mapAction.autoComplete}"
var="cap" minChars="5" for="searchedVehicle"
fetchValue="#{cap.registrationNumber}"
id="suggestion" tokens=",">
  <h:column>
    <h:outputText value="#{cap.registrationNumber}"/>
  </h:column>
</rich:suggestionbox>
<rich:spacer width="17px"/>




JAVA CODE:


public List autocomplete(Object suggest)
{
String pref= (String)suggest;
log.info("In Pref = "+pref);
ArrayList result = new ArrayList();
result.clear();
List resultList = new ArrayList();
resultList=entityManager.createQuery("from Vehicle vehicle where vehicle.registrationNumber like" + "'"+pref+"%'").getResultList();
log.info("IN AutoComplete = "+resultList.size());
return resultList;
}


JavaScript:


<Script type="text/javascript">
function printObjectsSelected(output,sgcomponent)
{
output.innerHTML=sgcomponent.getSelectedItems().pluck('state');
}
</Script>