Expand my Community achievements bar.

How to Dynamically populate OOTB Table Component in Adaptive Form | AEM Community Blog Seeding

Avatar

Administrator

BlogImage.jpg

How to Dynamically populate OOTB Table Component in Adaptive Form by AEM Blog | Lhotse Technologies

Abstract

In this blog, I want to share how you can dynamically prepopulate out of the box table component. You do not have to create your own custom table component to serve data dynamically in form of table.

Now for example, I want five rows dynamically, so I need to create row1 inside prefill service as shown where I created multiple rows:-
import com.adobe.forms.common.service.*;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
@Component()
public class MyPrefillAdaptiveForm implements DataProvider {
private Logger logger = LoggerFactory.getLogger(MyPrefillAdaptiveForm.class);
public String getServiceName() {
return “My Prefill Service Name”;
}
public String getServiceDescription() {
return “My Prefill Service”;
}
public PrefillData getPrefillData(final DataOptions dataOptions) throws FormsException {
return new PrefillData() {
public InputStream getInputStream() {
return getData(dataOptions);
}
public ContentType getContentType() {
return ContentType.XML;
}
};
}
private InputStream getData(DataOptions dataOptions) throws FormsException {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement(“data”);
doc.appendChild(rootElement);
Element col1 = doc.createElement(“tableItem11”);
col1.setTextContent(“1111”);
Element col2 = doc.createElement(“tableItem12”);
col2.setTextContent(“2222”);
Element row1 = doc.createElement(“Row1”);
row1.appendChild(col1);
row1.appendChild(col2);
rootElement.appendChild(row1);
row1 = doc.createElement(“Row1”);
col1 = doc.createElement(“tableItem11”);
col1.setTextContent(“33”);
col2 = doc.createElement(“tableItem12”);
col2.setTextContent(“44”);
row1.appendChild(col1);
row1.appendChild(col2);
rootElement.appendChild(row1);
row1 = doc.createElement(“Row1”);
col1 = doc.createElement(“tableItem11”);
col1.setTextContent(“55”);
col2 = doc.createElement(“tableItem12”);
col2.setTextContent(“66”);
row1.appendChild(col1);
row1.appendChild(col2);
rootElement.appendChild(row1);
row1 = doc.createElement(“Row1”);
col1 = doc.createElement(“tableItem11”);
col1.setTextContent(“77”);
col2 = doc.createElement(“tableItem12”);
col2.setTextContent(“88”);
row1.appendChild(col1);
row1.appendChild(col2);
rootElement.appendChild(row1);
row1 = doc.createElement(“Row1”);
col1 = doc.createElement(“tableItem11”);
col1.setTextContent(“99”);
col2 = doc.createElement(“tableItem12”);
col2.setTextContent(“10”);
row1.appendChild(col1);
row1.appendChild(col2);
rootElement.appendChild(row1);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final DOMSource source = new DOMSource(doc);
final StreamResult outputTarget = new StreamResult(outputStream);
TransformerFactory.newInstance().newTransformer().transform(source, outputTarget);
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
return inputStream;
} catch (Exception e) {
logger.error(“Error while creating prefill data”, e);
throw new FormsException(e);
}
}
}

Now some configuration needs to be done to make this logic to work.

Read Full Blog

How to Dynamically populate OOTB Table Component in Adaptive Form

Q&A

Please use this thread to ask the related questions.



Kautuk Sahni
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

0 Replies