Apache flex 在操作脚本中指定datagrid列的percentColWidth
尝试 && 其中col是数据网格中的一列Apache flex 在操作脚本中指定datagrid列的percentColWidth,apache-flex,actionscript-3,flex3,Apache Flex,Actionscript 3,Flex3,尝试 && 其中col是数据网格中的一列 谢谢。据我所知,数据网格上没有百分比宽度。您可以编写一些代码来手动执行此操作,即:将宽度设置为.1*dataGrid.width为10%,但这显然有点烦人。我过去使用过这个解决方案 我在google上快速搜索了一下,找到了下面的链接,其中一个人扩展了DataGrid类,使其具有我认为您正在下沉的功能。你可以在这里查看他的文章: 我还没有试过用这个,所以我想知道它是否有效 祝你好运。我想请你告诉我们什么不起作用;但是由于percentageWidth不是类
谢谢。据我所知,数据网格上没有百分比宽度。您可以编写一些代码来手动执行此操作,即:将宽度设置为.1*dataGrid.width为10%,但这显然有点烦人。我过去使用过这个解决方案 我在google上快速搜索了一下,找到了下面的链接,其中一个人扩展了DataGrid类,使其具有我认为您正在下沉的功能。你可以在这里查看他的文章: 我还没有试过用这个,所以我想知道它是否有效
祝你好运。我想请你告诉我们什么不起作用;但是由于percentageWidth不是类的属性,我怀疑您的意思是您看到了编译器错误
如果要使用百分比调整dataGrid列的大小,可能需要扩展dataGrid并自己编写列大小调整代码 使用以下扩展数据网格:
col.percentWidth //doesnt compile
使用以下语法将所有列宽声明为分配给“columnWidths”的at数组:
package {
import mx.controls.DataGrid;
import mx.events.DataGridEvent;
public class ExDataGrid extends DataGrid {
//--------------------------------------
// Constructor
//--------------------------------------
public function ExDataGrid() {
super();
addEventListener(DataGridEvent.COLUMN_STRETCH, onColumnStretch);
}
//------------------------------------------------------------------------------
// Properties
//------------------------------------------------------------------------------
//--------------------------------------
// Private
//--------------------------------------
/**
* @private
* Keeps track of whether the columns have been manually adjusted or not. If they
* have, then do not apply the columnWidths that have been specified.
*/
private var _columnsAdjusted : Boolean = false;
/**
* @private
* Storage for the columnWidths property.
*/
private var _columnWidths : Array = new Array();
/**
* @private
*/
private var _columnWidthsChanged : Boolean = false;
/**
* @private
* Stores the explicit width portions of the column widths.
*/
private var _explicitColWidths : Object;
/**
* @private
* Stores the percentage width portions of the column widths.
*/
private var _percentColWidths : Object;
//--------------------------------------
// Getters / Setters
//--------------------------------------
public function get columnWidths() : Array {
return _columnWidths;
}
/**
* Sets the widths of each of the columns. The widths can either be percentages or
* explicit widths. For each column in the DataGrid, there should be a column width
* value. The column widths should be expressed as strings.
*
* If there are 4 columns and we want the 1st column to be 40% width, the 2nd column
* to be 60% width, the 3rd column to be a fixed width of 200, and the 4th column to
* be a fixed width of 300. Then we would set the columnWidths property to be:
* ['40%', '60%', 200, 300]
*/
public function set columnWidths(values : Array) : void {
if (_columnWidths != values) {
_columnWidths = values;
_columnWidthsChanged = true;
invalidateProperties();
invalidateDisplayList();
}
}
//------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------
//--------------------------------------
// Protected
//--------------------------------------
/**
* @private
*/
override protected function commitProperties() : void {
super.commitProperties();
if (_columnWidthsChanged) {
splitPercentWidths(columnWidths);
_columnWidthsChanged = false;
}
}
/**
* @private
* Sizes each of the columns in the DataGrid based on the columnWidths property,
* unless the user has manually resized the columns, then the column widths will
* not be adjusted.
*/
override protected function updateDisplayList(unscaledWidth : Number, unscaledHeight : Number) : void {
// Determine how much width is left over for percentage calculations after the fixed
// widths are allocated.
var leftoverWidth : Number = unscaledWidth;
for each (var explicitColWidth : Number in _explicitColWidths) {
leftoverWidth -= explicitColWidth;
}
// Manually adjust the column width before doing super.updateDisplayList. This way when
// super.updateDisplayList is called, it can perform any minor adjustments to the columns,
// but the column widths will still be pretty consistant with the specified widths.
if (columns && columnWidths && !_columnsAdjusted && columns.length == columnWidths.length) {
for (var i : int = 0; i < columnWidths.length; i++) {
var w : Number = 0;
if (_explicitColWidths[i]) {
w = _explicitColWidths[i];
}
else {
w = leftoverWidth * (_percentColWidths[i] / 100);
}
// Adjust the column's width. After digging through the DataGridColumn, I found
// 3 different properties that need to be set to override the default column width
// calculations performed by DataGrid and DataGridColumn. They are _width (changed
// in the setWidth method), explicitWidth, and preferredWidth.
columns[i].setWidth(w);
columns[i].explicitWidth = w;
columns[i].preferredWidth = w;
}
}
super.updateDisplayList(unscaledWidth, unscaledHeight);
}
//--------------------------------------
// Private
//--------------------------------------
/**
* @private
*/
private function onColumnStretch(event : DataGridEvent) : void {
_columnsAdjusted = true;
}
/**
* Called from the <code>commitProperties()</code> method to break up the columnWidths
* into percentage based widths and explicit widths.
*
* When we calculate the percentage widths in <code>updateDisplayList()</code> we need
* to know the remaining available width after explicit widths are subtracted.
*/
private function splitPercentWidths(values : Array) : void {
if (columns && columnWidths && columnWidths.length > 0) {
_percentColWidths = new Object();
_explicitColWidths = new Object();
for (var i : uint = 0; i < columnWidths.length; i++) {
var columnWidth : String = columnWidths[i] + "";
// If columnWidth contains a '%' then it is a percentage width, otherwise
// it is an explicit width.
if (columnWidth.indexOf("%") == -1) {
_explicitColWidths[i] = Number(columnWidth);
}
else {
_percentColWidths[i] = Number(columnWidth.substr(0, columnWidth.length - 1));
}
}
}
}
}
}
没有%符号的宽度通常会被处理。就像其他海报所说的,mx DataGrid列上没有percentWidth属性,但是可以这样做,您需要扩展Flex DataGrid。参见这里的示例9 应用列宽时要记住的一件事是,在设置列宽之前,确保将horizontalScrollPolicy设置为on 实际上,我们在DataGrid的扩展上提供了percentWidth属性。而且实施非常严格: 1计算网格的总宽度 2根据百分比宽度值对其进行划分 3将水平滚动策略设置为on 4应用计算的宽度 5将水平滚动策略设置回原来的状态
这对我们有用,所以对你也有用。如果在遵循上述psuedo代码或将HSCP设置为不适用宽度后,请在此处发布,我们可以尝试提供帮助 不,它不会给出任何编译或运行时错误。它只是忽略了这一点。我知道我找不到datagrid列中指定的任何percentWidth属性,但我想问一下如何实现它?在mxml中,如果我提供三列的小数,例如0.6,0.2,0.2,只要总和为所有列中的1,我总是能够实现它。对于动作脚本,我尝试通过为列的宽度绑定变量,并在运行时使用自定义函数以百分比值设置这些变量来实现同样的效果。但它似乎也不起作用。。。
package {
import mx.controls.DataGrid;
import mx.events.DataGridEvent;
public class ExDataGrid extends DataGrid {
//--------------------------------------
// Constructor
//--------------------------------------
public function ExDataGrid() {
super();
addEventListener(DataGridEvent.COLUMN_STRETCH, onColumnStretch);
}
//------------------------------------------------------------------------------
// Properties
//------------------------------------------------------------------------------
//--------------------------------------
// Private
//--------------------------------------
/**
* @private
* Keeps track of whether the columns have been manually adjusted or not. If they
* have, then do not apply the columnWidths that have been specified.
*/
private var _columnsAdjusted : Boolean = false;
/**
* @private
* Storage for the columnWidths property.
*/
private var _columnWidths : Array = new Array();
/**
* @private
*/
private var _columnWidthsChanged : Boolean = false;
/**
* @private
* Stores the explicit width portions of the column widths.
*/
private var _explicitColWidths : Object;
/**
* @private
* Stores the percentage width portions of the column widths.
*/
private var _percentColWidths : Object;
//--------------------------------------
// Getters / Setters
//--------------------------------------
public function get columnWidths() : Array {
return _columnWidths;
}
/**
* Sets the widths of each of the columns. The widths can either be percentages or
* explicit widths. For each column in the DataGrid, there should be a column width
* value. The column widths should be expressed as strings.
*
* If there are 4 columns and we want the 1st column to be 40% width, the 2nd column
* to be 60% width, the 3rd column to be a fixed width of 200, and the 4th column to
* be a fixed width of 300. Then we would set the columnWidths property to be:
* ['40%', '60%', 200, 300]
*/
public function set columnWidths(values : Array) : void {
if (_columnWidths != values) {
_columnWidths = values;
_columnWidthsChanged = true;
invalidateProperties();
invalidateDisplayList();
}
}
//------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------
//--------------------------------------
// Protected
//--------------------------------------
/**
* @private
*/
override protected function commitProperties() : void {
super.commitProperties();
if (_columnWidthsChanged) {
splitPercentWidths(columnWidths);
_columnWidthsChanged = false;
}
}
/**
* @private
* Sizes each of the columns in the DataGrid based on the columnWidths property,
* unless the user has manually resized the columns, then the column widths will
* not be adjusted.
*/
override protected function updateDisplayList(unscaledWidth : Number, unscaledHeight : Number) : void {
// Determine how much width is left over for percentage calculations after the fixed
// widths are allocated.
var leftoverWidth : Number = unscaledWidth;
for each (var explicitColWidth : Number in _explicitColWidths) {
leftoverWidth -= explicitColWidth;
}
// Manually adjust the column width before doing super.updateDisplayList. This way when
// super.updateDisplayList is called, it can perform any minor adjustments to the columns,
// but the column widths will still be pretty consistant with the specified widths.
if (columns && columnWidths && !_columnsAdjusted && columns.length == columnWidths.length) {
for (var i : int = 0; i < columnWidths.length; i++) {
var w : Number = 0;
if (_explicitColWidths[i]) {
w = _explicitColWidths[i];
}
else {
w = leftoverWidth * (_percentColWidths[i] / 100);
}
// Adjust the column's width. After digging through the DataGridColumn, I found
// 3 different properties that need to be set to override the default column width
// calculations performed by DataGrid and DataGridColumn. They are _width (changed
// in the setWidth method), explicitWidth, and preferredWidth.
columns[i].setWidth(w);
columns[i].explicitWidth = w;
columns[i].preferredWidth = w;
}
}
super.updateDisplayList(unscaledWidth, unscaledHeight);
}
//--------------------------------------
// Private
//--------------------------------------
/**
* @private
*/
private function onColumnStretch(event : DataGridEvent) : void {
_columnsAdjusted = true;
}
/**
* Called from the <code>commitProperties()</code> method to break up the columnWidths
* into percentage based widths and explicit widths.
*
* When we calculate the percentage widths in <code>updateDisplayList()</code> we need
* to know the remaining available width after explicit widths are subtracted.
*/
private function splitPercentWidths(values : Array) : void {
if (columns && columnWidths && columnWidths.length > 0) {
_percentColWidths = new Object();
_explicitColWidths = new Object();
for (var i : uint = 0; i < columnWidths.length; i++) {
var columnWidth : String = columnWidths[i] + "";
// If columnWidth contains a '%' then it is a percentage width, otherwise
// it is an explicit width.
if (columnWidth.indexOf("%") == -1) {
_explicitColWidths[i] = Number(columnWidth);
}
else {
_percentColWidths[i] = Number(columnWidth.substr(0, columnWidth.length - 1));
}
}
}
}
}
}
columnWidths = ['70%','30%','100'];