【问题标题】:Datagrid selects the wrong custom cell in my datagridDatagrid 在我的数据网格中选择了错误的自定义单元格
【发布时间】:2010-03-15 11:47:38
【问题描述】:

我很快就在处理一个问题,但我仍然无法按预期工作。我有一个 DataGrid,它有一个 HBox 和一个 CheckBox 和一个标签作为 itemRenderer(参见下面的代码)。当我点击单元格时,标准 itemEditor 会弹出并让您输入标签的内容。那是标准行为。除了 2 个问题,我工作正常:

  1. 如果我输入的文本过多,会弹出水平滚动条,并且单元格会被该滚动条填充。如您所见,我尝试将 Horizo​​ntalScrollPolicy 设置为关闭,但这根本不起作用......我尝试对所有不同的元素执行此操作,但故障仍然存在。

  2. 当我填写了多行时,会发生另一个错误。如果我点击一行,数据网格会选择该行下方的那个。仅当已经选择了一行时。如果我在数据网格外部点击,然后在任何一行点击右侧行的 itemEditor 将显示...现在我的 set data 方法的设置中有什么正确的吗?

__

package components
{
 import mx.containers.HBox;
 import mx.controls.CheckBox;
 import mx.controls.Label;

 public class ChoiceRenderer extends HBox
 {

  private var correctAnswer:CheckBox;
  private var choiceLabel:Label;

  public function ChoiceRenderer()
  {
   super();
   paint();
  }

  private function paint():void{
   percentHeight = 100;
   percentWidth = 100;
   setStyle("horizontalScrollPolicy", "off");
   super.setStyle("horizontalScrollPolicy", "off");

   correctAnswer = new CheckBox;
   correctAnswer.setStyle("horizontalScrollPolicy", "off");  
   addChild(correctAnswer);

   choiceLabel = new Label;
   choiceLabel.setStyle("horizontalScrollPolicy", "off");   
   addChild(choiceLabel);  


  }

     override public function set data(xmldata:Object):void{
      if(xmldata.name() == "BackSide"){
       var xmlText:Object = xmldata.TextElements.TextElement.(@position == position)[0];
       super.data = xmlText;
       choiceLabel.text = xmlText.toString();
       correctAnswer.selected = xmlText.@correct_answer;

      }               
 }
}

提前致谢! 马库斯

【问题讨论】:

    标签: apache-flex datagrid selection itemrenderer


    【解决方案1】:
    • 我不确定这是否是您的问题背后的原因,但创建孩子的标准方法是覆盖 createChildren 方法。

    • 此外,您缺少 else 语句 - 当 if 条件失败时,您没有调用 super.data。这看起来也不好。

    试试:

    package components
    {
     public class ChoiceRenderer extends HBox  {
      private var correctAnswer:CheckBox;
      private var choiceLabel:Label;
    
      public function ChoiceRenderer() {
        super();
        percentHeight = 100;
        percentWidth = 100;
        setStyle("horizontalScrollPolicy", "off");
      }
      override protected function createChildren():void {
        super.createChildren();
        correctAnswer = new CheckBox();
        addChild(correctAnswer);
        choiceLabel = new Label();
        choiceLabel.setStyle("horizontalScrollPolicy", "off");   
        addChild(choiceLabel);  
      }
      override public function set data(xmldata:Object):void {
        if(xmldata.name() == "BackSide") {
           var xmlText:Object = xmldata.TextElements.TextElement.(@position == position)[0];
           super.data = xmlText;
           choiceLabel.text = xmlText.toString();
           correctAnswer.selected = xmlText.@correct_answer;
        }
        else {
          //what if xmldata.name() is not "BackSide"?
          //you are not calling super.data in that case
        }
      }
    }
    

    【讨论】:

    • 感谢您的回答。不幸的是,它并没有解决这两个问题。我没有 else 语句的原因是,有时该函数调用整行(xml 中的 BackSide),有时它使用 TextElement 调用单元格......我不知道为什么,但确切地说......跨度>
    • @Markus 至于滚动条问题,请尝试注释掉百分比宽度和百分比高度线。
    • 不会改变任何东西......但我添加了 setStyle("horizo​​ntalAlign", "center");声明,这有效果!...我不明白...
    【解决方案2】:
    • 为了避免滚动条,您必须让数据网格具有可变高度
    <mx:DataGrid id="dg" dataProvider="{dp}" variableRowHeight="true" creationComplete="dg.height=dg.measureHeightOfItems(0,dp.length)+dg.headerHeight+2"/>

    【讨论】:

    • 嗨 Nishu,感谢您的回答,但我的问题不是垂直滚动条,而是水平...
    • 对误读表示歉意。您是否尝试将 wordwrap 设置为 true 并将 datagridcolum.width 设置为更新单元格和 datagridcolumn.width 的最大值
    猜你喜欢
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多