我想在spark数据网格中禁用(并禁用)某些行。我找到了这个答案来停止选择,这很棒 In flex, is there a way to capture and optionally cancel a row selection event in a DataGrid?
但我想要除了显示特定行不可选。理想情况下,我想要某种覆盖,但我不确定这是否可行。我的替代解决方案是将文本颜色更改为灰色以用于不可选择的行。查看datagrid呈现它们似乎都是基于列的。我看了潜在的皮肤(覆盖交替的颜色属性),但这只是设置背景属性而不是文本颜色。这可能吗?
由于
答案 0 :(得分:3)
最基本的解决方案是使用您提到的自定义渲染器和选择阻止逻辑:
<强> DataGridRowDisabling.mxml 强>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:local="*">
<fx:Script>
<![CDATA[
import spark.events.GridSelectionEvent;
private function dataGrid_selectionChangingHandler(event:GridSelectionEvent):void
{
var index:int = event.selectionChange.rowIndex;
var product:Product = dataGrid.dataProvider.getItemAt(index) as Product;
if (product && !product.enabled)
event.preventDefault();
}
]]>
</fx:Script>
<s:DataGrid id="dataGrid" itemRenderer="GridItemRenderer2" selectionChanging="dataGrid_selectionChangingHandler(event)">
<s:dataProvider>
<s:ArrayCollection>
<local:Product name="iPod" price="199.99"/>
<local:Product name="iPad 3" price="499.99"/>
<local:Product name="iPad 4" price="599.99" enabled="false"/>
<local:Product name="iPad 5" price="699.99" enabled="false"/>
</s:ArrayCollection>
</s:dataProvider>
</s:DataGrid>
</s:Application>
<强> GridItemRenderer2.mxml 强>
<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" clipAndEnableScrolling="true">
<fx:Script>
<![CDATA[
override public function prepare(hasBeenRecycled:Boolean):void
{
if (data is Product)
enabled = Product(data).enabled;
lblData.text = data[column.dataField];
}
]]>
</fx:Script>
<s:Label id="lblData" top="9" left="7"/>
</s:GridItemRenderer>
<强> Product.as 强>
package
{
public class Product
{
public var name:String;
public var price:Number;
public var enabled:Boolean = true;
}
}