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" label="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}"/>
Related
I have setup a ComboBox named cmbCopyFrom and its visibility is based on the whether a item 'Pricing Zone' was selected from another ComboBox, cmbBoundary, and it sometimes doesn't make the cmbCopyFrom to be visible even though, Pricing zone was selected. Not sure what might be causing this. Any suggestions?
<s:ComboBox id="cmbBoundary" prompt="Select a Boundary"
labelFunction="codeLabel" />
<s:ComboBox id="cmbCopyFrom" prompt="Copy From" labelFunction="codeLabelKey"
dataProvider="{model.arrOfLOB}"
includeInLayout="{model.isCopyFromVisible}"
visible="{model.isCopyFromVisible}" />
cmbBoundary.addEventListener(IndexChangeEvent.CHANGE,boundarySelected);
protected function boundarySelected(evt:IndexChangeEvent):void {
trace("Index change event fired..."+this.cmbBoundary.selectedItem.name);
if (this.cmbBoundary.selectedItem && this.cmbBoundary.selectedItem.name == "Pricing Zones" ) {
model.isCopyFromVisible = true;
}
else {
model.isCopyFromVisible = false;
}
}
What i am trying to do is very simple but it does not seem to work
I am trying to get a checkbox event handler to change the text value of a string.
protected function
transport_yes_catching_bus_to_venue_checkbox_clickHandler(event:MouseEvent):void
{
if (transport_yes_catching_bus_to_venue_checkbox.selected==true)
{
totals_transport_I_catch_bus_to_venue.text = "YES"
}
}
and this is my label before the change is applied
<s:Label id="totals_transport_I_catch_bus_to_venue"
includeIn="a13_summary_for_submission"
x="170" y="625" color="#3B70D1" text="-----"/>
The idea is for the label to be changed from "----" to "YES" if the checkbox has been clicked. Otherwise the label stays as it was.
When i run the application the label does not change regardless of the state of checkbox.
THANKS!!
<s:Label id="totals_transport_I_catch_bus_to_venue" includeIn="a13_summary_for_submission"
x="170" y="625" color="#3B70D1" text="{cbInput.selected?'YES':'-----'}" />
<s:CheckBox id="cbInput" />
I'm having difficulty figuring out how to add a dropdownlist control to only a single row of a data grid. For example, if I have two rows of data in the grid, I want the top to be the normal text from the data provider, and the second row to be a dropdownlist (bound to an array collection).
I've searched high and low for a solution to no avail.
Any help is much appreciated.
Thanks,
Conceptually you need an itemRenderer function, which is not implemented in the MX DataGrid. ( It may be in the new Spark one, but I don't know).
In lieu of that, just create an itemRenderer to conditionally display the DropDownList. Something like this:
<s:MXDataGridRenderer dataChange="onDataChange()">
<fx:script>
public function onDataChange():void{
if((this.ListData as DataGridListData).rowIndex == 0){
label.visible == false;
ddl.visible == true;
} else {
label.visible == true;
ddl.visible == false;
}
}
</fx:Script>
<s:Label id="label" />
<s:DropDownList id="ddl" />
</s:MXDataGridRenderer>
I have a datagrid with an xmlListCollection bound to it:
<mx:DataGrid id="dgCompetente" includeIn="Competente" x="10" y="66" width="547" height="468"
change="dgCompetente_changeHandler(event)" dataProvider="{colCompetente}"
editable="false">
<mx:columns>
<mx:DataGridColumn headerText="ID Competenţă" dataField="idCompetenta"/>
<mx:DataGridColumn headerText="Denumire Competenţă" dataField="denCompetenta"/>
<mx:DataGridColumn headerText="Competenţă Superioară" dataField="idCompSuperioara" labelFunction="labelFunctionCompetentaSuperioara"/>
</mx:columns>
</mx:DataGrid>
and a button to delete the currently selected item in the datagrid, which has this function assigned to the click event:
<s:Button id="btnDeleteCompetenta" includeIn="Competente" x="813" y="65" label="Stergere" click="deleteCompetenta()"/>
private function deleteCompetenta():void
{
try {
var position:int = dgCompetente.selectedIndex;
if (position >= 0) {
colCompetente.removeItemAt(position);
dgCompetente.selectedIndex = position;
}
clearEdit(fieldsCompetente);
saveCompetente();
} catch (error:Error) {
errorHandler.defaultErrorHandler(error);
}
}
I want the selectedIndex to remain the same. So, if I delete item 2, the next in the list should be selected. The problem is that if I delete item 2, item 3 will be selected and I have no idea why.
Can someone tell me what I'm missing?
Thank you!
It doesn't work selectedIndex don't update with the good value also in updateCompleteHandler
but with value-1 if force value+1, take value+2 and the selection escape from click, so it's exactly the same.
You're probably resetting the selectedIndex too early, before the DataGrid is updated, but it's hard to tell exactly since your code calls a bunch of functions. Try this
Add a "cached position" variable, like this:
protected var cachedPosition : int;
Then modify your deleteCompetenta method
private function deleteCompetenta():void
{
try {
cachedPosition = dgCompetente.selectedIndex;
if (position >= 0) {
colCompetente.removeItemAt(position);
// don't reset this here
// dgCompetente.selectedIndex = position;
}
clearEdit(fieldsCompetente);
saveCompetente();
} catch (error:Error) {
errorHandler.defaultErrorHandler(error);
}
}
Then add an updateCompleteHander to the DataGrid:
<mx:DataGrid id="dgCompetente" includeIn="Competente" x="10" y="66" width="547" height="468"
change="dgCompetente_changeHandler(event)" dataProvider="{colCompetente}"
editable="false" updateComplete="updateCompleteHandler(event)">
And reset the selectedIndex int he updateCompleteHandler:
protected function updateCompleteHandler(event:Event):void{
// reset here
dgCompetente.selectedIndex = cachedPosition;
}
Would that solve it?
I have a data grid that has a checkbox item renderer in a cloumn to allow row selections:
Main application:
<mx:DataGrid id="dg">
<mx:columns>
<mx:DataGridColumn id="ir" itemRenderer="renderers.RowCheckbox" />
<mx:DataGridColumn dataField="Name" headerText="Name" />
</mx:columns>
</mx:DataGrid>
Item renderer:
<-- RowCheckbox -->
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" horizontalAlign="center">
<mx:CheckBox id="chk"/>
</mx:HBox>
How can I get a handle to the item renderer / checkbox so that I may determine which rows are checked?
Just a word of advice: We had a similar problem in our application and we solved it by adding a "selected" property to the entities in the dataprovider of the datagrid. The selected property of the checkBox was then bound to the selected property of our entity. To know which ones were selected, we just looped over the entities in the dataprovider instead of the item renderers. After a lot of different approaches, this really was the best option.
If I remember correctly, the problem was that the itemrenderers did not remember the selected state correctly and the datagrid was completely messed up when you scrolled up and down. The wrong rows were selected after scrolling.
Another option would be to dispatch an event in the item renderer which bubbles up all the way to the control hosting the datagrid. You could then listen for these events and update your model to reflect the changes.
I ran into similar issues with the DataGrid and multiple item renderers and the reuse of item renderers when scrolling. In order to access DataGrid item renderers I extended the DataGrid. My first thought was to use the indicesToIndex() followed by indexToItemRenderer(). Unfortunately these methods didn't do what I expected so I added the indicesToItemRenderer() method:
package com.whatever.controls
{
import mx.controls.DataGrid;
import mx.controls.listClasses.IListItemRenderer;
public class CustomDataGrid extends DataGrid
{
public function CustomDataGrid()
{
super();
}
public function indicesToItemRenderer(rowIndex:int, colIndex:int):IListItemRenderer
{
var firstItemIndex:int = verticalScrollPosition - offscreenExtraRowsTop;
if (rowIndex < firstItemIndex ||
rowIndex >= firstItemIndex + listItems.length
)
{
return null;
}
return listItems[rowIndex - firstItemIndex][colIndex];
}
}
To resolve the reused item renderers when scrolling issue, refer to this article:
http://www.adobe.com/devnet/flex/articles/itemrenderers_pt1.html
It boils down to overriding the data setter and storing properties in data. For example, I had one column using a CheckBox itemRenderer and another column using ComboBox. For both I listen for the change event and store selected, selectedIndex, etc in data whenever properties change and override the data setter to set those properties:
override public function set data(value:Object):void
{
if (value != null)
{
super.data = value;
if (data.hasOwnProperty('selected') && data.selected)
{
selected = data.selected;
}
else
{
selected = false;
}
}
}
You can use the indexToItemRenderer() method exposed by all subclasses of ListBase.
For example:
private function someFunction(index:int):void
{
var rowCheckbox:RowCheckbox = dg.indexToItemRenderer(index) as RowCheckbox;
trace(rowCheckbox.chk.selected.toString());
}
... where index represents the index of the DataGrid item whose "chk" property you want test.
In the ItemRenderer, try putting Checkbox Component in a VBox..resolves the scrolling issue.