Add ajax behaviour to JSF renderer -


i have custom jsf component renderer renders pagination component datatables, hasn't ajax behavior . learned done inserting ajax encode method or making support of f ajax tag. unfortunately i'm not familiar ajax, please tell me how should modify code make ajaxable?

@facesrenderer(componentfamily="javax.faces.command",     renderertype="com.component.pager") 

public class pagerrenderer extends renderer {

private static final int show_pages = 3; private static final string   hidden_field_id ="tableformpaginatorinput"; public void encodebegin(facescontext context, uicomponent component)         throws ioexception {     clientbehaviorcontext behaviorcontext =                             clientbehaviorcontext.createclientbehaviorcontext(context,                            component, "click", component.getclientid(context), null);     string id = component.getclientid(context);     uicomponent parent = component;     while (!(parent instanceof uiform)) parent = parent.getparent();     string formid = parent.getclientid(context);       responsewriter writer = context.getresponsewriter();     writer.startelement("div", component);     writer.writeattribute("class", "paginator-section", null);     string styleclass = (string) component.getattributes().get("styleclass");     string selectedstyleclass             = (string) component.getattributes().get("selectedstyleclass");     string datatableid = (string) component.getattributes().get("datatableid");       // find component given id      uidata data = (uidata) component.findcomponent(datatableid);      int first = data.getfirst();     int itemcount = data.getrowcount();     int pagesize = data.getrows();     if (pagesize <= 0) pagesize = itemcount;      int pages = itemcount / pagesize;     if (itemcount % pagesize != 0) pages++;      int currentpage = first / pagesize;     if (first >= itemcount - pagesize) currentpage = pages - 1;     int startpage = 0;     int endpage = pages;     if (show_pages  > 0) {         startpage = (currentpage / show_pages ) * show_pages ;         endpage = math.min(startpage + show_pages , pages);     }     if (currentpage > 0)         writelink(writer, component, formid, id, "<", styleclass);      if (startpage > 0)         writelink(writer, component, formid, id, "..", styleclass);      (int = startpage; < endpage; i++) {         writelink(writer, component, formid, id, "" + (i + 1),                 == currentpage ? selectedstyleclass : styleclass);     }      if (endpage < pages)         writelink(writer, component, formid, id, "...", styleclass);      if (first < itemcount - pagesize)         writelink(writer, component, formid, id, ">", styleclass);    /*  commandbutton commandbutton = new commandbutton();     commandbutton.setvalue(currentpage);     commandbutton.setupdate("");     commandbutton.setajax(true);     commandbutton.addactionlistener(new myactionlistener());     commandbutton.encodeall(context);*/       writer.endelement("div");     // hidden field hold result     writehiddenfield(writer, component, id); }  private void writelink(responsewriter writer, uicomponent component,                        string formid, string id, string value, string styleclass)  throws ioexception {      writer.writetext(" ", null);      writer.startelement("a", component);      writer.writeattribute("href", "#", null);      writer.writeattribute("onclick", onclickcode(formid, id, value) , null);      if (styleclass != null)          writer.writeattribute("class", styleclass, "styleclass");      writer.writetext(value, null);      writer.endelement("a");   }  private void writelink1(responsewriter writer, uicomponent component,                        string formid, string id, string value, string styleclass)         throws ioexception {     writer.writetext(" ", null);     writer.startelement("a", component);     writer.writeattribute("href", "#", null);     writer.writeattribute("data-tableformpaginator", value, null);     if (styleclass != null)         writer.writeattribute("class", styleclass, "styleclass");     writer.writetext(value, null);     writer.endelement("a");    }     private string onclickcode(string formid, string id, string value) {     return new stringbuilder().append("document.forms['")             .append(formid).append("']['")             .append(id).append("'].value='").append(value).append("'; document.forms['")             .append(formid).append("'].submit(); return false;").tostring(); }  private string onclickcodewithout(string formid, string id, string value,uicomponent component) {     return "jsf.ajax.request('" + component.getclientid() +             "', null, {'render': '" +             component.getparent().getclientid() + "',"+id+":"+value+" })";  } private void writehiddenfield(responsewriter writer, uicomponent component,                               string id) throws ioexception {     writer.startelement("input", component);     writer.writeattribute("id", hidden_field_id, null);     writer.writeattribute("class", "paginator-input-value", null);     writer.writeattribute("type", "hidden", null);     writer.writeattribute("name", id, null);     writer.endelement("input"); }  public void decode(facescontext context, uicomponent component) {     string id = component.getclientid(context);     map<string, string> parameters             = context.getexternalcontext().getrequestparametermap();      string response = (string) parameters.get(id);     if (response == null || response.equals("")) return;      string datatableid = (string) component.getattributes().get("datatableid");     int showpages = show_pages ;//toint(component.getattributes().get("showpages"));      uidata data = (uidata) component.findcomponent(datatableid);      int first = data.getfirst();     int itemcount = data.getrowcount();     int pagesize = data.getrows();     if (pagesize <= 0) pagesize = itemcount;      if (response.equals("<")) first -= pagesize;     else if (response.equals(">")) first += pagesize;     else if (response.equals("..")) first -= pagesize * showpages;     else if (response.equals("...")) first += pagesize * showpages;     else {         int page = integer.parseint(response);         first = (page - 1) * pagesize;     }     if (first + pagesize > itemcount) first = itemcount - pagesize;     if (first < 0) first = 0;     data.setfirst(first); }  private static int toint(object value) {     if (value == null) return 0;     if (value instanceof number) return ((number) value).intvalue();     if (value instanceof string) return integer.parseint((string) value);     throw new illegalargumentexception("cannot convert " + value); } 

f:ajax tag can directly nested in uicomponent implements clientbehaviorholder interface.

i saw try implement onclick table, if using mojarra, has utils method you render onclick behavior work f:ajax renderselectonclick

http://grepcode.com/file/repo1.maven.org/maven2/com.sun.faces/jsf-impl/2.1.19/com/sun/faces/renderkit/renderkitutils.java#renderkitutils.renderselectonclick%28com.sun.faces.renderkit.facescontext%2cjavax.faces.component.uicomponent%2cboolean%29

you can use inside encode method


Comments