如何绑定索引的DataModel字段以具有灵活的Grid

时间:2012-01-27 09:46:27

标签: gwt gxt

我希望能够渲染网格,而某些额外列的数量是未知的,直到我使用基于特定数据类型的onRender()方法。

我有这个班级

ActivityModel extends BaseDataModel {
  // defines all common setXXX and getXXX needed
}

我的问题是,从服务器端回来,模型还带有一些额外的字段,这些字段也必须在网格中呈现为列。

我的问题: 有没有办法在AcitivityModel中有一种索引字段才能执行以下操作? :关注col.setId("{indexed}");

        List<ActivityParameterModel> parameters = folderModel.getParameters();
    for (ActivityParameterModel param : parameters) {
        ActivityParamType paramType = param.getParamType();
        col = new ColumnConfig();
        col.setHeader(param.getParamName());
        col.setWidth(2);
                    //
                    // the following line is what I'm expecting to do
        col.setId("{indexed}");
        switch (paramType) {
        case N:
            NumberField nf = new NumberField();
            nf.setPropertyEditorType(Integer.class);
            col.setEditor(new CellEditor(nf));
            break;
        case S:
            TextField<String> tf = new TextField<String>();
            col.setEditor(new CellEditor(tf));
            break;
        }
        config.add(col);

2 个答案:

答案 0 :(得分:0)

使用标准配置渲染网格,当您知道网格应该如何查找特定配置时,修改它和layout()...只需几毫秒,最终用户就不会受到影响。< / p>

答案 1 :(得分:0)

这就是我为解决这个问题所做的工作:

我在DB中存储了变量参数,这里是实体:

    @Entity
    @Table(name = "ACTIVITYPARAMETER")
    public class ActivityParameter implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "ACTIVITYPARAMETER_IDACTIVITYPARAMETER_GENERATOR",   sequenceName = "SN_ACTIVITYPARAMETER", 
    allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ACTIVITYPARAMETER_IDACTIVITYPARAMETER_GENERATOR")
@Column(name = "ID_ACTIVITYPARAMETER", unique = true, nullable = false)
private Long idActivityParameter;

@Column(nullable = false, length = 256)
private String paramName;

@Column(nullable = false, length = 256)
private String paramType;

// uni-directional many-to-one association to Activitysetting
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ACTIVITYSETTING_ID", nullable = false)
private ActivitySetting activitySetting;

@Column(name = "PARAMORDER", nullable = false)
private Long order;

@Column(name = "PARAMMANDATORY", nullable = false, precision = 1)
@TypeConverter(name = "booleanToNumberConverter", dataType = BigDecimal.class, objectType = Boolean.class)
private boolean mandatory;

在网格创建时,我创建了固定列,然后使用简单的set(paramName,DEFAULT_VALUE)创建了一个循环来构建变量参数,如下所示:

        // Extra Fields
    List<ActivityParameterModel> parameters = folderModel.getParameters();
    int index = 1;
    for (ActivityParameterModel param : parameters) {
        String columnId = "param" + (index++);
        col = new ColumnConfig(columnId, param.getParamName(), 90);
        NumberField nf = new NumberField();
        col.setEditor(new CellEditor(nf));
        ActivityParamType paramType = param.getParamType();
        switch (paramType) {
        case N:
            nf.setValidateOnBlur(true);
            break;
        }
        config.add(col);
    }

它创建了额外的列,在提交时,我通过在参数列表上再次循环并调用get(paramName)来恢复值来恢复值。