我想扩展DataGrid组件,以便在电子表格中看到行号的(只读)列。我遇到了这篇文章http://www.cflex.net/showFileDetails.cfm?ObjectID=735但它依赖于每行的唯一数据,因此它可以索引到数组中。如果数据不是唯一的(例如对于空网格),则它不起作用。我该如何实现呢?
答案 0 :(得分:2)
这对我有用:
<mx:Label xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.AdvancedDataGrid;
private var handleDataChangedEnabled:Boolean = false;
override public function set data(value:Object):void {
super.data = value;
if (!handleDataChangedEnabled) {
addEventListener("dataChange", handleDataChanged);
}
}
public function handleDataChanged(event:Event):void {
this.text = String(listData.rowIndex + (listData.owner as AdvancedDataGrid).verticalScrollPosition + 1);
}
]]>
</mx:Script>
当然,您必须将AdvancedDataGrid更改为DataGrid。
干杯。
答案 1 :(得分:0)
确保dataProvider具有唯一的列或属性,如果您不习惯,则不要显示该列/属性。 关键是dataProvider
答案 2 :(得分:0)
只需将此类用作itemRenderer: RowNumColumnRenderer.as
package
{
import mx.collections.IList;
import mx.controls.AdvancedDataGrid;
import mx.controls.Label;
import mx.controls.listClasses.ListBase;
public class RowNumColumnRenderer extends Label
{
override public function set data(value:Object):void
{
super.data = value;
if (listData != null)
this.text = (AdvancedDataGrid(listData.owner).itemRendererToIndex(this) + 1).toString();
}
}
}
答案 3 :(得分:-1)
我能够通过实现自定义itemRenderer,RowNumberRenderer.as
来实现这一点。package com.domain
{
import mx.collections.IList;
import mx.controls.Label;
import mx.controls.listClasses.ListBase;
public class RowNumberRenderer extends Label
{
public function RowNumberRenderer()
{
super();
}
override public function set data(value:Object):void
{
super.data = value;
this.text = String(IList(ListBase(listData.owner).dataProvider).getItemIndex(data) + 1);
}
}
}
答案 4 :(得分:-1)
以下内容如何:
RendererRowIndexPlusOne.as
package
{
import mx.controls.Label;
import mx.utils.StringUtil;
import mx.utils.ObjectUtil;
public class RendererRowIndexPlusOne extends Label
{
public override function set data(item:Object):void {
super.data = item;
trace('listData.label ' + listData.label);
trace('listData.rowIndex ' + listData.rowIndex);
trace('listData.columnIndex ' + listData.columnIndex);
trace('listData.owner ' + listData.owner);
text = String(listData.rowIndex + 1);
}
}
}