Versions Compared

Key

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

本文主要解释如何 使用Bean Shell 表单绑定器 从表单表格中 加载/读取 数据 . 如果您需要从多个表中加载/存储表单数据,这可能非常方便。

加载绑定器 -> Bean Shell表单绑定器

This post explains how to load / read data from Form Grid using Bean Shell Form Binder. This can be handy if you need to load / store grid data from multiple tables.

Load Binder -> Bean Shell Form Binder

Code Block
import org.joget.apps.form.model.*;
import org.joget.apps.form.service.*;
import java.sql.*;
import org.apache.commons.collections.SequencedHashMap;
import java.util.*;

public FormRowSet test() {
	FormRowSet f = new FormRowSet();
        f.setMultiRow(true);
        Class.forName("com.mysql.jdbc.Driver").newInstance();
	con = DriverManager.getConnection("jdbc:mysql://localhost:3307/jwdb?characterEncoding=UTF-8", "database username", "database password");

	if(!con.isClosed()){
		String recordId = "#requestParam.id#"; // Get the url parameter
		String sql = "SELECT * FROM test_table WHERE id=?"; // Here you can query from one or multiple tables using JOIN etc
		PreparedStatement stmt = con.prepareStatement(sql);
		stmt.setString(1, recordId);
		ResultSet rs = stmt.executeQuery();
		while (rs.next()) {
		FormRow r1 = new FormRow();
			r1.put("gridColumn1", rs.getString(1));
			r1.put("gridColumn1", rs.getString(2));
			r1.put("gridColumn1", rs.getString(3));
			f.add(r1);
	   }
	}
	return f;
}
return test();

Store Binder 存储绑定器 -> Bean Shell Form Binder表单绑定器

Code Block
import org.joget.apps.form.model.*;
import org.joget.apps.form.lib.*;
import org.joget.apps.form.service.*;
import java.sql.*;
import org.apache.commons.collections.SequencedHashMap;
import java.util.*;
import org.joget.commons.util.UuidGenerator;

public FormRowSet getGridRows() {
	return rows ; // this will return the grid rows
}

public saveGridRows(FormRowSet rows) {
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	con = DriverManager.getConnection("jdbc:mysql://localhost:3307/jwdb?characterEncoding=UTF-8", "database username", "database password");
	if(!con.isClosed()){

		String recordId = "#requestParam.id#";

		// If you need to query data from different table(s) and process it before saving it to database,
		// you can do it here


		UuidGenerator uuid = UuidGenerator.getInstance();

		Iterator i= rows.iterator(); // Iterating grid rows
		while (i.hasNext()) {
			FormRow row = (FormRow) i.next();
			String gridColumn1 = row.get("gridColumn1"); // reading grid column value
			String gridColumn2 = row.get("gridColumn2");
			String gridColumn3 = row.get("gridColumn3");
			String pId = uuid.getUuid(); // generating Primary Key
			String insertSql = "INSERT INTO table(id,col1,col2,col3) VALUES(?,?,?,?)";
			PreparedStatement stmtInsert = con.prepareStatement(insertSql);
			stmtInsert.setString(1, pId);
			stmtInsert.setString(2, gridColumn1);
			stmtInsert.setString(3, gridColumn2);
			stmtInsert.setString(4, gridColumn3);
			stmtInsert.executeUpdate();
		}
	} else {
		System.out.println("Connection Problem");
	}
	con.close();
}

FormRowSet rows = getGridRows(); // getting the grid rows
saveGridRows(rows); // processing & storing the grid rows

Grid Design


表格设计

正确的表格设计应如下所示Correct Grid design should look like this:

 

part of load binder beanshell script :部分load binder beanshell脚本:

Code Block
languagejava
FormRowSet f = new FormRowSet();
f.setMultiRow(true);
FormRow r1 = new FormRow();
r1.put("gridColumn1", your_value);
r1.put("gridColumn2", your_value);
r1.put("gridColumn3", your_value);
f.add(r1);