Links

   Quran Explorer - Interactive Audio Recitations & Translations

Wednesday, July 3, 2013

Programaticaly Creating Primefaces DataExporter


The following is a utility method that creates a single HtmlCommandLink(h:commandLink NOT p:commandLink) that exports to PDF.

NB: please not that DataExporter is an ActionListener and not a UIComponent !

Modify the id and target variables accordingly

public static HtmlCommandLink createExportCommand(XMLElement wd){

    FacesContext fc = FacesContext.getCurrentInstance();
    Application application = fc.getApplication();
    ExpressionFactory ef = application.getExpressionFactory();
    ELContext elc = fc.getELContext();

    String table_id = "dt_mn_" + wd.getAttribute("key","x") + "_"  +    wd.getAttribute("keyfield","kf"); //id of parent datatable


          HtmlCommandLink command = new HtmlCommandLink();
          command.setId("cmd_pdf_" + table_id);
          command.setValue("PDF");
          command.setTitle("Click to Export");
          //command.addActionListener(new DataExporter(table.getId(),"pdf","Export","false","false","","utf-8",null,null));

          ValueExpression target = ef.createValueExpression(elc, ("mainForm:"+table_id), String.class);
          ValueExpression type = ef.createValueExpression(elc, "pdf", String.class);
          ValueExpression fileName = ef.createValueExpression(elc, "Export", String.class);
          ValueExpression pageOnly = ef.createValueExpression(elc, "true", String.class);
          ValueExpression selectionOnly = ef.createValueExpression(elc, "false", String.class);
          ValueExpression exludeColumns = ef.createValueExpression(elc, "", String.class);
          ValueExpression encoding = ef.createValueExpression(elc, "CP1252", String.class);
          MethodExpression preProcessor = FacesAccessor.createMethodExpression("#{eventManager.preProcessor}",Void.class, new Class[2]);
          MethodExpression postProcessor = FacesAccessor.createMethodExpression("#{eventManager.postProcessor}",Void.class, new Class[2]);

          DataExporter exporter = new DataExporter(target,type,fileName,pageOnly,selectionOnly,exludeColumns,encoding,null,null);

          command.addActionListener(exporter);
       // }

    return command;
      }


===============
Stack
===============
Primefaces 3.3
Tomcat 7
JSF 2.0

No comments:

Post a Comment

Feel free to leave a comment