目前Flex api提供的DataGrid没有双击子适应内容宽度的功能,咱们可以补充一下这个功能!
1.建立目录结构org.sk.controls,org.sk.controls.vo
2.类MyDataGrid 继承 DataGrid,
3. 用到的 值对象
1.建立目录结构org.sk.controls,org.sk.controls.vo
2.类MyDataGrid 继承 DataGrid,
- package org.sk.controls
- {
- import flash.events.MouseEvent;
- import flash.text.TextField;
- import flash.text.TextLineMetrics;
- import mx.collections.ArrayCollection;
- import mx.controls.DataGrid;
- import mx.controls.dataGridClasses.DataGridColumn;
- import mx.core.ScrollPolicy;
- import org.sk.controls.vo.DataGridColumnInfoVo;
- /**
- *
- * @author Ozone
- * MyDataGrid-> DataGrid
- * 1. 双击 自定适应内容宽度
- * 思路: 1.先给 DataGrid的header添加 MouseDoubleClick事件
- * 2.记录 所有列的x坐标
- * 3.双击是 根据x的坐标寻找 Column
- * 4.找到Column后,找出 此列下最长的 文字的 长度
- * 5.设置此Column的width 为 合适的长度的
- */
- public class MyDataGrid extends DataGrid
- {
- private var widthMap:Array=[]; //存放 列,宽度 映射关系
- private var _textField:TextField=new TextField();
- /**
- * 构造
- */
- public function MyDataGrid()
- {
- super();
- this.doubleClickEnabled=true;
- this.horizontalScrollPolicy=ScrollPolicy.ON;
- }
- override protected function createChildren():void
- {
- super.createChildren();
- this.header.addEventListener(MouseEvent.DOUBLE_CLICK, mouseDblClickHandler);
- }
- override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
- {
- super.updateDisplayList(unscaledWidth, unscaledHeight);
- widthMap.length=0;
- var tmpWidth:uint=0;
- for (var i:uint=0; i
- /*表头 双击事件*/
- private function mouseDblClickHandler(e:MouseEvent):void
- {
- var colVo:DataGridColumnInfoVo=getColumnInfo(this.mouseX);
- if (colVo)
- {
- var maxLen:int=getMaxLength(colVo.label);
- if (maxLen != -1)
- {
- maxLen+=30;
- var col:DataGridColumn=columns[colVo.index];
- if (col)
- {
- col.width=maxLen;
- }
- }
- }
- }
- /*获取 列索引 */
- private function getColumnInfo(mousex:int):DataGridColumnInfoVo
- {
- var scrollColCount:int = this.horizontalScrollPosition;
- if(scrollColCount!=0){
- mousex += widthMap[scrollColCount-1].min + DataGridColumnInfoVo.PRECSITION;
- }
- for each (var colVo:DataGridColumnInfoVo in widthMap)
- {
- if (colVo.min
- /*获取 最长行的长度*/
- private function getMaxLength(colLabel:String):int
- {
- var arr:ArrayCollection=this.dataProvider as ArrayCollection;
- var maxLen:int=-1;
- if (arr)
- {
- for (var i:uint=0; i maxLen)
- maxLen=line.width;
- }
- }
- return maxLen;
- }
- }
- }
package org.sk.controls
{
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextLineMetrics;
import mx.collections.ArrayCollection;
import mx.controls.DataGrid;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.core.ScrollPolicy;
import org.sk.controls.vo.DataGridColumnInfoVo;
/**
*
* @author Ozone
* MyDataGrid-> DataGrid
* 1. 双击 自定适应内容宽度
* 思路: 1.先给 DataGrid的header添加 MouseDoubleClick事件
* 2.记录 所有列的x坐标
* 3.双击是 根据x的坐标寻找 Column
* 4.找到Column后,找出 此列下最长的 文字的 长度
* 5.设置此Column的width 为 合适的长度的
*/
public class MyDataGrid extends DataGrid
{
private var widthMap:Array=[]; //存放 列,宽度 映射关系
private var _textField:TextField=new TextField();
/**
* 构造
*/
public function MyDataGrid()
{
super();
this.doubleClickEnabled=true;
this.horizontalScrollPolicy=ScrollPolicy.ON;
}
override protected function createChildren():void
{
super.createChildren();
this.header.addEventListener(MouseEvent.DOUBLE_CLICK, mouseDblClickHandler);
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
widthMap.length=0;
var tmpWidth:uint=0;
for (var i:uint=0; i
/*表头 双击事件*/
private function mouseDblClickHandler(e:MouseEvent):void
{
var colVo:DataGridColumnInfoVo=getColumnInfo(this.mouseX);
if (colVo)
{
var maxLen:int=getMaxLength(colVo.label);
if (maxLen != -1)
{
maxLen+=30;
var col:DataGridColumn=columns[colVo.index];
if (col)
{
col.width=maxLen;
}
}
}
}
/*获取 列索引 */
private function getColumnInfo(mousex:int):DataGridColumnInfoVo
{
var scrollColCount:int = this.horizontalScrollPosition;
if(scrollColCount!=0){
mousex += widthMap[scrollColCount-1].min + DataGridColumnInfoVo.PRECSITION;
}
for each (var colVo:DataGridColumnInfoVo in widthMap)
{
if (colVo.min
/*获取 最长行的长度*/
private function getMaxLength(colLabel:String):int
{
var arr:ArrayCollection=this.dataProvider as ArrayCollection;
var maxLen:int=-1;
if (arr)
{
for (var i:uint=0; i maxLen)
maxLen=line.width;
}
}
return maxLen;
}
}
}
3. 用到的 值对象
- package org.sk.controls.vo
- {
- import mx.controls.dataGridClasses.DataGridColumn;
- public class DataGridColumnInfoVo
- {
- private var _index:uint;
- private var _min:int;
- private var _max:int;
- private var _label:String;
- public static const PRECSITION:int = 3;
- public function DataGridColumnInfoVo(_index:uint,_label:String,_min:int,_max:int)
- {
- this._index = _index;
- this._label = _label;
- this._min = _min;
- this._max = _max;
- }
- public function get index():uint{
- return _index;
- }
- public function set index(index:uint):void{
- _index = index;
- }
- public function get label():String{
- return _label;
- }
- public function set label(label:String):void{
- _label = label;
- }
- public function get min():int{
- return _min;
- }
- public function set min(min:int):void{
- _min = min;
- }
- public function get max():int{
- return _max;
- }
- public function set max(max:int):void{
- _max = max;
- }
- }
- }