Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

您可以使用Beanshell表单绑定器预先填充一些新的表单。

下面的图1显示了其前3个字段将被预填充的表单的示例。

Image Added

图1:带有预填充字段的表单

解决这一要求的快速而简单的方法是在该分区的加载绑定器中使用Beanshell表单绑定器,编辑分区。

Image Added

图2:配置部分属性以确定如何处理数据

在  加载绑定器中,选择“Bean Shell Form Binder”作为加载文件夹。

Image Added

图3:选择Beanshell Form Binder作为绑定器

使用自己的编码配置Bean Shell Form Binder以填充相关字段,如下图所示。

在这个例子中使用的代码:

You may initialize a new form with some fields pre-populated using Beanshell Form Binder.

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

Image Removed
Figure 1: Form with Fields to Pre-populate

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

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

In Load Binder, select "Bean Shell Form Binder" as the Load Binder.

Image Removed
Figure 3: Choose Beanshell Form Binder as the Load Binder

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

Code used in this example:

Code Block
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 Binder with the Necessary Codes

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

图4:用必要的代码填充Beanshell表单绑定器

如果编码正确编写和测试,你应该得到这个结果:

Image Added
图5:  加载新表单后的3个预填充字段Image Removed
Figure 5: The 3 Pre-populated Fields After a New Form is Loaded