Flex DataGrid with row number column

梦想与她 提交于 2019-12-12 21:03:19

问题


I want to extend the DataGrid component so that there is a (read-only) column for the row number like you see in spreadsheets. I came across this article http://www.cflex.net/showFileDetails.cfm?ObjectID=735 but it depends on the data being unique for each row so that it can index into the array. If the data is not unique (like for an empty grid) it doesn't work. How can I implement that?


回答1:


This worked for me:

<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>

Of course, you would have to change AdvancedDataGrid to DataGrid.

Cheers.




回答2:


ensure that the dataProvider has a unique column or property, then dont show that column/property if you dont wont to. The key is the dataProvider




回答3:


Just use this class as your 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();
        }
    }
}



回答4:


I was able to do this by implementing a custom 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);                     
        }

    }
}



回答5:


how about the following:

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);
        }
    }
}


来源:https://stackoverflow.com/questions/946465/flex-datagrid-with-row-number-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!