在本文中,我们将使用Bean Shell代码以自定义顺序填充网格,并过滤掉一些不需要的数据。有两种方法来实现这一点。

1.使用FormDataDao检索数据

FormDataDao是处理表单数据的服务类。我们将使用find方法检索数据并使用sort参数对数据进行排序。我们可以使用conditionparams  参数来过滤它。

public org.joget.apps.form.model.FormRowSet find(java.lang.String formDefId, java.lang.String tableName, final java.lang.String condition, final java.lang.Object[] params, final java.lang.String sort, final java.lang.Boolean desc, final java.lang.Integer start, final java.lang.Integer rows)

示例代码:

import org.joget.apps.app.service.AppUtil;
import org.joget.apps.form.dao.FormDataDao;
import org.joget.apps.form.model.Element;
import org.joget.apps.form.model.FormData;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.apps.form.service.FormUtil;
import org.joget.plugin.base.PluginManager;
import org.joget.apps.form.model.FormLoadBinder;
import org.joget.commons.util.LogUtil;
 
public FormRowSet load(Element element, String primaryKey, FormData formData) {
    String formDefId = "grid_entry";  //change this to the form id used to store grid data
    String tableName = "grid_entry";  //change this to the table name used to store grid data
    String foreignKey = "fk"; //change this to the foreign key
    String sort = "name"; //change to other field id for sorting
    Boolean desc = false; //change to true for descending order
     
    FormRowSet rows = new FormRowSet();
 
    try {
        FormDataDao formDataDao = (FormDataDao) AppUtil.getApplicationContext().getBean("formDataDao");
        String foreignKeyFilter = getFormPropertyName(foreignKey);
        String condition = (foreignKeyFilter != null && !foreignKeyFilter.isEmpty()) ? " WHERE " + foreignKeyFilter + " = ?" : "";
        Object[] paramsArray = new Object[]{primaryKey};
        
        //add additional filter
        /*
            String nameFilter = getFormPropertyName("name");
            condition += " AND " + nameFilter + " LIKE ?";    
            paramsArray = new Object[]{primaryKey, "%TEST%"};
        */
            
        rows = formDataDao.find(formDefId, tableName, condition, paramsArray, sort, desc, null, null);
    } catch (Exception e) {
        LogUtil.error("Bean Shell Form Load Binder", e, "Load data with custom sorting and filtering");
    }
    rows.setMultiRow(true);        
    return rows;
}
public String getFormPropertyName(String propertyName) {
    if (propertyName != null && !propertyName.isEmpty()) {
        if (!FormUtil.PROPERTY_ID.equals(propertyName)) {
            propertyName = FormUtil.PROPERTY_CUSTOM_PROPERTIES + "." + propertyName;
        }
    }
    return propertyName;
}
 
//call load method with injected variable
return load(element, primaryKey, formData);

 

2. 后处理数据

在这种方法中,我们将重用多行表单绑定器来检索数据,然后做一些后处理,在将数据返回到Grid之前进行排序或过滤。

import org.joget.apps.form.model.Element;
import org.joget.apps.form.model.FormData;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.apps.form.service.FormUtil;
import org.joget.plugin.base.PluginManager;
import org.joget.apps.form.model.FormLoadBinder;
import java.util.Collections;
import java.util.Comparator;
 
public FormRowSet load(Element element, String primaryKey, FormData formData) {
    String formDefId = "grid_entry";  //change this to the form id used to store grid data
    String foreignKey = "fk"; //change this to the foreign key
    final String sortField = "name";
     
    FormRowSet f = new FormRowSet();
 
    // Reuse Multi Row Binder to load data
    PluginManager pluginManager = (PluginManager) FormUtil.getApplicationContext().getBean("pluginManager");
    FormLoadBinder binder = (FormLoadBinder) pluginManager.getPlugin("org.joget.plugin.enterprise.MultirowFormBinder");
     
    //Load from the grid table
    binder.setProperty("formDefId", formDefId);
    binder.setProperty("foreignKey", foreignKey);
    f = binder.load(element, primaryKey, formData);

    //post processing 
    if (f != null && !f.isEmpty()) {
        Collections.sort(f, new Comparator() {
            public int compare(Object row1, Object row2) {
                String sort1 = ((FormRow) row1).getProperty(sortField);
                String sort2 = ((FormRow) row2).getProperty(sortField);
                return sort1.compareTo(sort2);
            }
        });
    }
    return f;
}
 
//call load method with injected variable
return load(element, primaryKey, formData);

 

本文示例应用: APP_custom_grid_load-1-20151119111806.jwa

  • No labels