超级强大的DataGrid组件

12年前

DataGrid 组件允许用户显示和操作多列数据。

当 DataGrid 实例从单击或 Tab 键切换中获得焦点时,您可以使用以下按键来控制它:

说明
向下箭头 如果正在编辑单元格,插入点将移到单元格文本的末尾。如果单元格不可编辑,则向下箭头处理选区的方式与 List 组件相同。
向上箭头 如果正在编辑单元格,插入点将移到单元格文本的开头。如果单元格不可编辑,则向上箭头处理选区的方式与 List 组件相同。
向右箭头 如果正在编辑单元格,插入点将向右移动一个字符。如果单元格不可编辑,内容向左移动一水平滚动行。
向左箭头 如果正在编辑单元格,插入点将向左移动一个字符。如果单元格不可编辑,内容向右移动一水平滚动行。
Enter 如果正在编辑单元格,则会提交更改,并且插入点将移到同一列中单元格的下一行。
Shift+Enter 如果正在编辑单元格,则会提交更改,并且插入点将移到同一列中单元格的上一行。
Tab 如果下一个单元格可编辑,将焦点移动到下一个单元格,并选中该单元格的所有文本;否则,将焦点移到下一个对象。
Shift+Tab 如果前一个单元格可编辑,将焦点移动到前一个单元格,并选中该单元格的所有文本;否则,将焦点移到前一个对象。

以下示例演示数据网格的属性。

从"库"面板中拖动相关组件到舞台(或直接通过 ActionScript 代码创建组件实例),并对实例命名。

在主时间轴中选择第一帧,打开"动作"面板,然后输入以下代码:

my_dg.setColumnNames(["id","name","score"]);  my_dg.addItem({id:0,name:"Clark",score:3135});  my_dg.addItem({id:1,name:"Bruce",score:403});  my_dg.addItem({id:2,name:"Peter",score:25});

通过设置 CellRenderer 创建类似 List 的 DataGrid。

首先,创建数据网格的行渲染器 DataGridRowRenderer 类(继承于 ListCellRenderer)。

package {   import flash.text.TextField;   import shinater.swing.DataGrid;   public class DataGridRowRenderer extends ListCellRenderer {    public override function setSize(width:Number, height:Number):void {     _width = width;     _height = height;     repaint();    }    public override function repaint():void {     var dataGrid:DataGrid = DataGrid(getOwner());     var x:Number = textField.x;     for (var i:int = 0,columnCount:uint = dataGrid.getColumnCount(); i<columnCount; i++) {      var txt:TextField;      if (i == 0) {       txt = textField;      } else {       var n:String = "textField" + i;       txt = getChildByName(n) as TextField;       if (txt == null) {        txt = new TextField();        txt.name = n;        addChild(txt);       }       txt.defaultTextFormat = textField.defaultTextFormat;       txt.x = x;       txt.y = textField.y;       txt.height = textField.height;      }      txt.text = getData()[dataGrid.getColumnName(i)];      txt.width = dataGrid.getColumnWidth(i);      x += txt.width;     }     skin.width = _width;     skin.height = _height;     if (focusRectSkin != null) {      focusRectSkin.width = _width;      focusRectSkin.height = _height;     }    }   }  }

同上例,在主时间轴中选择第一帧,打开"动作"面板,然后输入以下代码:

my_dg.setCellRenderer(DataGridRowRenderer);  my_dg.setColumnNames(["id","name","score"]);  my_dg.addItem({id:0,name:"Clark",score:3135});  my_dg.addItem({id:1,name:"Bruce",score:403});  my_dg.addItem({id:2,name:"Peter",score:25});  资料来源于:http://www.shinater.com/FlashSwing/Help/DataGrid.html