2012-02-16 35 views
6

我目前正在尝试禁用可选/可编辑/或将textInput更改为动态以获得我想要的结果。AS3禁用datagrid中的textInput可编辑/可选

我有一个带下拉和文本输入区域的自定义数据网格。但是,如果我的Model#列中没有数据,我不想在相应的PurchasePrice单元格中输入任何条目。

col1 = new DataGridColumn("Model"); 
    col1.headerText = "Model #"; 
    c2.consumables_dg.addColumn(col1); 
    col1.width = 60; 
    col1.editable = false; 
    col1.sortable = false; 
    col1.cellRenderer = AlternatingRowColors_editNum_PurchasePrice; 
col1 = new DataGridColumn("PurchasePrice"); 
    col1.headerText = "Purchase Price"; 
    c2.consumables_dg.addColumn(col1); 
    col1.width = 60; 
    col1.editable = false; 
    col1.sortable = false; 
    col1.cellRenderer = AlternatingRowColors_editNum_PurchasePrice; 

我试过在AlternatingRowColors_editNum_PurchasePrice各种物品,但似乎没有任何作为尚未上班。请看看我在我的else语句中试过的if(__ enbaled)

package{ 
    import flash.text.TextFormat; 
    import flash.text.TextFormatAlign; 
    import fl.controls.TextInput; 
    import flash.text.TextFieldType; 
    import flash.events.Event; 
    import fl.controls.listClasses.ListData; 
    import fl.controls.listClasses.ICellRenderer; 

public class AlternatingRowColors_editNum_PurchasePrice extends TextInput implements ICellRenderer 
{ 
    protected var _data:Object; 
    protected var _listData:ListData; 
    protected var _selected:Boolean; 
    private var __enabled:Boolean = false; 
    public static var _stage; 
    public static var _alignment; 
    private var tf:TextFormat; 

    public function AlternatingRowColors_editNum_PurchasePrice() 
    { 
     tf = new TextFormat(); 
     if(__enabled){ 
       if(_alignment == 2){ 
        tf.align = TextFormatAlign.RIGHT; 
       }else if(_alignment == 1){ 
        tf.align = TextFormatAlign.CENTER; 
       }else{ 
        tf.align = TextFormatAlign.LEFT; 
       } 
      restrict = "0-9."; 
      addEventListener(Event.CHANGE, textChange); 

     }else{ 
      //this.selectable = false; // did not work 
      //textField.selectable = false; // did not work 
      //textField.type = dynamic; // did not work 
      //textField.type = TextFieldType.DYNAMIC; // did not work 
      //this.mouseEnabled = false; // did not work 
      //this.tabEnabled = false; // did not work 
      //textField.mouseEnabled = false; // did not work 
      //textField.tabEnabled = false; // did not work 
      //selectable = false; // did not work 
      //this.selectable = false; // did not work 
      //_enabled = false; // did not work 
      //----------------------------------------------------------- 
      // *** Corresponding Entry to enable was placed above *** 
      //----------------------------------------------------------- 
     } 
     super(); 
    } 

    public function textChange(Event):void{ 
     //trace(_data.Discount); 
     _data.PurchasePrice = text; 
    } 

    public function get data():Object 
    { 
     return _data; 
    } 

    public function set data(value:Object):void 
    { 
     _data = value; 
     text = value.PurchasePrice; 

     if(value.Model != "") __enabled = true; 

     if (value.id % 2 == 0) { 
      setStyle("upSkin", AlternateColor1); 
     } else { 
      setStyle("upSkin", AlternateColor2); 
     } 
    } 

    public function get listData():ListData 
    { 
     return _listData; 
    } 

    public function set listData(value:ListData):void 
    { 
     _listData = value; 
    } 

    public function get selected():Boolean 
    { 
     return _selected; 
    } 

    public function set selected(value:Boolean):void 
    { 
     _selected = value; 
    } 

    public function setMouseState(state:String):void 
    { 
    } 
    override protected function drawLayout():void 
    { 
     textField.setTextFormat(tf); 
     super.drawLayout(); 
    } 

} 
} 

我刚刚接近这个错误的方式吗?而不是试图停止鼠标,选项卡或将文本字段转换为动态,我应该尝试其他方法来获得所需的结果吗?

感谢, JC

回答

12

最简单的方法是改变TextField类型本身,然后改变它的selectable属性:

//disable input 
tf.selectable = false; 
tf.type = TextFieldType.DYNAMIC; 

//enable input 
tf.selectable = true; 
tf.type = TextFieldType.INPUT; 

也不要忘记设置mouseEnabled & tabEnabled属性以适应/您的领域需要。

+0

如果我使用'TextFieldType'像你一样,它给了我一个错误。我用这个代码修正了它,而不是你的:'tf.type =“dynamic”;''和'txtAnswer.type =“input”;'可能对其他人有用;) – Metafaniel 2013-05-09 15:43:00

5

一定要在你的类/网页/帧的顶部导入TextFieldType:

import flash.text.TextFieldType;