【问题标题】:NatTable - need checkbox only when editableNatTable - 仅在可编辑时才需要复选框
【发布时间】:2013-08-30 03:46:33
【问题描述】:

我是 NatTable 的新手。我已经浏览了 NatTable 示例及其源代码,但没有找到解决我的一个问题的方法。 在 NatTable 我有一列应该根据另一列的值提供一个用于选择的复选框。 我使用过 Checkboxpainter、checkboxcelleditor、defaultbooleanconverter 和 IEditableRule。无论单元格是否可编辑,这都会呈现一个复选框,尽管它允许我仅在启用时标记复选框。

但是根据我们的要求,如果该行不可选择,用户不应看到该复选框。或者在最坏的情况下,应该为不可选择的行呈现 disabledcheckbox。

有人可以帮帮我吗?

感谢和问候,

帕杜姆纳

【问题讨论】:

    标签: checkbox nattable


    【解决方案1】:

    为此找到了解决方案。 我必须编写一个自定义的 checkboxpainter(从可用的 OOTB 继承)并重写它的 getImage 方法以为适当的单元格返回 null

    【讨论】:

      【解决方案2】:

      有一个更好的解决方案,我刚刚在我的工作中应用了一个类似的案例。

      我通过将以下配置添加到表中来做到这一点:

      // make checkbox cells editable
      configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.ALWAYS_EDITABLE, DisplayMode.EDIT, CONFIG_LABEL_CHECKBOX);
      
      // register the checkbox editor for DisplayMode.EDIT
      configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new CheckBoxCellEditor(), DisplayMode.EDIT, CONFIG_LABEL_CHECKBOX);
      
      // register the checkbox painter for DisplayMode.NORMAL
      configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new CheckBoxPainter(), DisplayMode.NORMAL, CONFIG_LABEL_CHECKBOX);
      
      // register the painter for empty cells in DisplayMode.NORMAL
      configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new BackgroundPainter(), DisplayMode.NORMAL, CONFIG_LABEL_EMPTY);
      

      基本上,这为可编辑复选框引入了配置标签 CONFIG_LABEL_CHECKBOX,为空单元格引入了 CONFIG_LABEL_EMPTY。

      现在您所要做的就是将IConfigLabelAccumulator 附加到您的bodyDataLayer

      bodyDataLayer.setConfigLabelAccumulator(new IConfigLabelAccumulator()
      {
        public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition)
        {
          if(columnPosition == CHECKBOX_COLUMN_INDEX) 
          {
            if(someCodeToCheckIfRowIsEditable(rowPosition)) 
            {
              configLabels.add(CONFIG_LABEL_CHECKBOX); 
            }
            else
            {
              configLabels.add(CONFIG_LABEL_EMPTY);            
            }
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-07-08
        • 2020-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-09
        • 2021-06-06
        • 1970-01-01
        • 2016-10-31
        相关资源
        最近更新 更多