开发者

changing "enabled" value of a button during runtime in flex

开发者 https://www.devze.com 2023-01-03 07:26 出处:网络
i want to make a button disabled if a datagrid is empty and it should be enabled when there is atleast 1 entry.the entries in the grid are made at runtime.I tried this

i want to make a button disabled if a datagrid is empty and it should be enabled when there is atleast 1 entry.the entries in the grid are made at runtime.I tried this this is the button:

<mx:Button id="update" labe开发者_开发百科l="Update Contact" enabled="{isButtonEnabled()}"/>

and the function is defined as where dg_contact is the datagrid:

public function isButtonEnabled():Boolean
{
     if(dg_contact.selectedIndex==-1)
    {
        return false;
    }
    else
    {
        return true;
    }
}

where am i going wrong?


Your code doesn't work because isButtonEnabled() doesn't get called when selectedIndex changes. You can use BindingUtils to do that, but this can be done without BindingUtils

DataGrid can have items and yet have its selectedIndex equal to -1. If you're not bothered about if an item is selected or not, bind it to the length of DataGrid's dataProvider

<mx:Button id="update" label="Update Contact" 
               enabled="{dg_contact.dataProvider.length != 0}"/>

If you want button to be enabled only when something is selected, bind it to selectedIndex

<mx:Button id="update" label="Update Contact" 
               enabled="{dg_contact.selectedIndex != -1}"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消