You may initialize a new form with some fields pre-populated using Bean Shell Form Data Store.

คุณสามารถเริ่มต้นฟอร์มใหม่ด้วยบางฟิลด์ที่มีการเติมข้อมูลไว้ล่วงหน้าโดยใช้ Bean Shell Form Data Store

Figure 1 below shows an example of a form whose first 3 fields are to be pre-populated.

รูปที่ 1 ด้านล่างแสดงตัวอย่างของฟอร์มที่มี 3 ฟิลด์แรกที่จะถูกเติมไว้ล่วงหน้า



Figure 1: Form with Fields to Pre-populate

รูปที่ 1: ฟอร์มที่มีฟิลด์ที่จะเติมไว้ล่วงหน้า


The quick and easy approach in addressing this requirement is to make use of the Beanshell Form Data Store in the section's Load Data Store. Edit the section.

วิธีที่ง่ายและรวดเร็วในการจัดการกับความต้องการนี้คือการใช้ประโยชน์จาก Beanshell Form Data Store ใน Load Data Store ของส่วน แก้ไขส่วน



Figure 2: Configuring Section Properties to Determine How Data Will Be Handled

รูปที่ 2: การกำหนดค่าคุณสมบัติส่วนเพื่อกำหนดวิธีจัดการข้อมูล


In Load Data Store, select "Bean Shell Form Data Store" as the Load Data Store.

ใน Load Data Store เลือก "Bean Shell Form Data Store" เป็น Load Data Store



Figure 3: Choose Beanshell Form Data Store as the Load Data Store

รูปที่ 3: เลือก Beanshell Form Data Store เป็น Load Data Store


Configure the Bean Shell Form Data Store with your own coding to populate relevant fields, as shown in the figure below.

Code used in this example:

กำหนดค่า Bean Data Store แบบฟอร์มถั่วด้วยการเข้ารหัสของคุณเองเพื่อเติมฟิลด์ที่เกี่ยวข้องดังแสดงในรูปด้านล่าง

รหัสที่ใช้ในตัวอย่างนี้:

import org.joget.apps.app.service.*;
import org.joget.apps.form.model.*;
import org.joget.apps.form.service.*;
import java.sql.*;
import java.util.*;

public FormRowSet getData() {

    //-----------------------------------------------------------------------------------
    //In this part of code, it trying to load the original data from form data table.

    FormRowSet results = null;
    if (primaryKey != null && primaryKey.trim().length() > 0) {
        AppService appService = (AppService) FormUtil.getApplicationContext().getBean("appService");
        Form form = FormUtil.findRootForm(element);
        if (form.equals(element) && form.getParent() != null) {
            form = FormUtil.findRootForm(form.getParent());
        }
        if (form != null) {
            results = appService.loadFormDataWithoutTransaction(form, primaryKey);
        }
    }
    //------------------------------------------------------------------------------------

    //------------------------------------------------------------------------------------
    //In this second part of code, it will load the data from external source by using
    //JDBC. It will run only when the first part of code fail to retrieve data from
    //form data table. This example use dir_user table of Joget as external source.

    if (results == null) {
        results = new FormRowSet();

        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
	        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jwdb2?characterEncoding=UTF-8", "root", "root");

	        if(!con.isClosed()){
	            String pId = "#currentUser.username#";
	            String sql = "SELECT firstName, lastName, email FROM dir_user WHERE username=?";
	            PreparedStatement stmt = con.prepareStatement(sql);
		        stmt.setString(1, pId);

		        ResultSet rs = stmt.executeQuery();
		        while (rs.next()) {
		            FormRow row = new FormRow();
		            row.put("firstName", (rs.getString(1) != null)?rs.getString(1):"");
			        row.put("lastName", (rs.getString(2) != null)?rs.getString(2):"");
			        row.put("email", (rs.getString(3) != null)?rs.getString(3):"");
			        results.add(row);
		        }
	        }

	    } catch(Exception ex) {
	        System.err.println("Exception: " + ex.getMessage());
	    } finally {
	        try {
                if(con != null)
                    con.close();
            } catch(SQLException e) {}
	    }
    }
    //------------------------------------------------------------------------------------

    return results;
}

return getData();



Figure 4: Populate Beanshell Form Data Store with the Necessary Codes

รูปที่ 4: เติม Data Store แบบฟอร์ม Beanshell ด้วยรหัสที่จำเป็น


If the coding is properly written and tested, you should get this result:

หากการเขียนโค้ดและการทดสอบถูกต้องคุณควรได้รับผลลัพธ์นี้:



Figure 5: The 3 Pre-populated Fields After a New Form is Loaded

รูปที่ 5: ฟิลด์ที่มีการเติมข้อมูลล่วงหน้า 3 ฟิลด์หลังจากฟอร์มใหม่เต็มแล้ว

 

Related Elements

องค์ประกอบที่เกี่ยวข้อง