How to Dynamically populate OOTB Table Component in Adaptive Form
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.
First, drop out of the box table component on to page as shown:-
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.
- You need to set the min and max value of Row1 dialog as shown.
i.e. min = 1, max = -1 where -1 indicates unlimited number of rows.
So now when you choose your prefill service under Prefill Service option then click on Preview, now you can see rows according to your dynamic data as shown.
Notes:-
- Name in java code should be the same as name in the dialog of table or row1.
- Authoring min and max value is must for the table component to work.
Recent Comments