How to update dataprovider data? - actionscript-3

I have a datagrid with editable option set to true. If i change data in the grid view the same changes should apply to dataprovider.I am new to flex can any one help me to solve this.
My idea is we need to use ArrayCollection properties or itemUpdate method.

Have a look at my code.
I have one field in the data provider. In the data grid a have added this field twice to let you see the effect of editing. If you edit values in the second column and press Enter, you can see the result in the first one.
So your data provider is always up to date.
Here is the working example.
Let me know if you meant something else.
<?xml version="1.0" encoding="utf-8"?>
<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"
minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]private var myDP:ArrayCollection = new ArrayCollection([
{myfield:"Hello"},
{myfield:"World"}
]);
]]>
</fx:Script>
<s:DataGrid id="myDG" x="30" y="30" height="120" dataProvider="{myDP}" editable="true">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="myfield" headerText="My Field" width="120"/>
<s:GridColumn dataField="myfield" headerText="My Field Edit" editable="true" rendererIsEditable="true" width="120">
<s:itemRenderer>
<fx:Component>
<s:GridItemRenderer>
<fx:Script>
<![CDATA[
import spark.events.TextOperationEvent;
protected function textinput_changeHandler(event:TextOperationEvent):void
{
var value:String = (event.currentTarget as TextInput).text
data[column.dataField] = value;
}
]]>
</fx:Script>
<s:TextInput text="{data['myfield']}" change="textinput_changeHandler(event)"/>
</s:GridItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:GridColumn>
</s:ArrayList>
</s:columns >
</s:DataGrid>
</s:Application>

Related

AdvancedDatagrid with itemEditor: Value disappears when clicked

I'm trying to use a ComboBox as itemEditor in an AdvancedDatagrid. Both the grid and the comboBox fills up correctly with values. But when I click in a cell the value disappears. Do I need to set more properties to make this work, or do I have to write my own itemEditor that copies the value to and from the itemEditor?
Full code:
<?xml version="1.0"?>
<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"
initialize="initCB();">
<fx:Script>
<![CDATA[
import mx.collections.*;
[Bindable] public var stateArray:ArrayCollection;
public function initCB():void {
stateArray=new ArrayCollection([{label:"AL", data:"Montgomery"},
{label:"AK", data:"Juneau"},
{label:"AR", data:"Little Rock"}]);
}
[Bindable] public var initDG:ArrayCollection = new ArrayCollection([{etikett:'NY', tekst:"New York"}, {etikett:'CA', tekst:"Los Angeles"}]);
]]>
</fx:Script>
<mx:AdvancedDataGrid id="gridIntMapping" y="24" left="10" right="605" height="166" editable="true" dataProvider="{initDG}">
<mx:columns>
<mx:AdvancedDataGridColumn width="130" dataField="etikett" headerText="Skjema referanse" editorDataField="text">
<mx:itemEditor>
<fx:Component>
<mx:ComboBox editable="true" dataProvider="{outerDocument.stateArray}" labelField="label"/>
</fx:Component>
</mx:itemEditor>
</mx:AdvancedDataGridColumn>
</mx:columns>
</mx:AdvancedDataGrid>
Set data.etikett to combobox text. like following:
<mx:ComboBox editable="true" dataProvider="{outerDocument.stateArray}" labelField="label" text="{data.etikett}" />
It will display text when you click.

How to get the value of Datagrid Cell in text box after cell getting updated by ItemEditor FX:Component ComboboxItemEditor

How to get the value of Datagrid Cell in text box after cell getting updated by {s:GridColumn s:itemEditor fx:Component s:ComboBoxGridItemEditor s:dataProvider>]}
Here I am using Webservice to get data in the datagrid now I want to update my last GridView Column. while clicking on the Status column am getting a dropdown from ItemEditor
ComboboxGridItemEditor and Array List as Dataprovider of this column . i want to update the value in the text box on change.
You see The example i have used in my application.
Resource Used :
http://help.adobe.com/en_US/flex/using/WS0ab2a460655f2dc3-427f401412c60d04dca-7ff8.html
http://help.adobe.com/en_US/Flex/4.0/UsingSDK/WS2db454920e96a9e51e63e3d11c0bf69084-7ca4.html
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication 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:vtrservices="services.vtrservices.*"
xmlns:vtrservices1="services.vtrservices1.*"
width="1122" height="632">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import spark.components.Grid;
import spark.events.*;
import spark.events.GridSelectionEvent;
protected function selectionChangeHandler(event:GridSelectionEvent):void {
const eventGrid:Grid = event.currentTarget.grid;
var currentIndx:int = eventGrid.selectedIndex;
var currentDataItem:Object = eventGrid.selectedItem;
selIndex.text = String(currentIndx);
selLName.text = String(currentDataItem.Status);
}
import spark.components.gridClasses.GridColumn;
protected function dgTasks_creationCompleteHandler(event:FlexEvent):void
{
GetMyTasksNew1Result.token = vtrServices1.GetMyTasksNew1(451);
}
]]>
</fx:Script>
<fx:Declarations>
<s:CallResponder id="GetMyTasksNew1Result"/>
<vtrservices1:VtrServices1 id="vtrServices1"
fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"
showBusyCursor="true"/>
</fx:Declarations>
<s:DataGrid id="dgTasks" width="100%" height="181"
creationComplete="dgTasks_creationCompleteHandler(event)" editable="true" selectionChange="selectionChangeHandler(event)"
variableRowHeight="true" >
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="AssigneeName" headerText="AssigneeName" editable="false"></s:GridColumn>
<s:GridColumn dataField="Priority" headerText="Priority" editable="false"></s:GridColumn>
<s:GridColumn dataField="StartTime" headerText="StartTime" editable="false"></s:GridColumn>
<s:GridColumn dataField="EndTime" headerText="EndTime" editable="false"></s:GridColumn>
<s:GridColumn dataField="Progress" headerText="Progress" ></s:GridColumn>
<s:GridColumn dataField="Status" headerText="Status" >
<s:itemEditor >
<fx:Component>
<s:ComboBoxGridItemEditor>
<fx:Script>
<![CDATA[
]]>
</fx:Script>
<s:dataProvider>
<s:ArrayList >
<fx:String>Open</fx:String>
<fx:String>In Progress</fx:String>
<fx:String>In Progress On Hold</fx:String>
<fx:String>In Approval</fx:String>
<fx:String>Reopened</fx:String>
<fx:String>Closed</fx:String>
<fx:String>Deleted</fx:String>
</s:ArrayList>
</s:dataProvider>
</s:ComboBoxGridItemEditor>
</fx:Component>
</s:itemEditor>
</s:GridColumn>
</s:ArrayList>
</s:columns>
<s:AsyncListView list="{GetMyTasksNew1Result.lastResult}"/>
</s:DataGrid>
<s:Label text="Selected index:"/>
<s:TextArea id="selIndex" height="50"/>
<s:Label text="Selected Last Name:"/>
<s:TextArea id="selLName" height="50"/>
</s:WindowedApplication>
Any Help will be greatly Admired
Thanks
I suggest to create own class based on AsyncListView and then to override method itemUpdated. From there you can for example dispatch your custom Event to propagate changes to other UI components.
See: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/AsyncListView.html#itemUpdated()

datefield itemrenderer

I try to create an editable datefield itemrenderer.
This itemrenderer works fine if I click on calendar button. But if I put data with keyboard, data isn't updating, if field is focus out data become empty.
See below my code
Datagrid column is like that
<mx:DataGridColumn dataField="echDate"
headerText="Date" headerStyleName="dgHeader"
itemEditor="ui.itemRenderer.irDateD" editorDataField="dateModif"/>
and item editor is
<?xml version="1.0" encoding="utf-8"?>
<s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
focusEnabled="true">
<fx:Script>
<![CDATA[
import mx.controls.dataGridClasses.DataGridListData;
import mx.controls.listClasses.BaseListData;
import mx.events.CalendarLayoutChangeEvent;
import mx.events.FlexEvent;
[Bindable] public var dateModif:String;
override public function set data(value:Object):void{
if(listData){
var newDate:Date;
var value1 = value.echDate;
if (value1 is String){
newDate = new DateUtility().dateStringToObject2(value1);
super.data = newDate;
dfDG.selectedDate = newDate;
dfDG.text = value1;
}
else if (value1 is Date){
super.data = value as Date;
dfDG.selectedDate = value1 as Date;
dfDG.text = new DateUtility().DateAsToString(value1);
}
}
}
protected function dfDG_changeHandler(event:CalendarLayoutChangeEvent):void
{
dateModif=dfDG.text;
}
]]>
</fx:Script>
<mx:DateField id="dfDG"
editable="true"
formatString="DD/MM/YYYY"
yearNavigationEnabled="true"
width="95"
change="dfDG_changeHandler(event)"
>
</mx:DateField>
</s:MXDataGridItemRenderer>
I'll happy if someone could help me to solve that
You can try this solution.
I have inserted the second DG to let you see, if data is updated or not.
I don't have your DateUtility().dateStringToObject2(value1), that's why I use a Validator instead.
The "backward connection" between Renderer and App is implemented by means of Binding.
When you use your DateUtility() to correct the input you can insert result to the text property of the DateField.
<?xml version="1.0" encoding="utf-8"?>
<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" minWidth="955" minHeight="600">
<fx:Declarations>
<fx:Model id="cells">
<dataset>
<data>
<name>a</name>
<echDate>14/01/2013</echDate>
</data>
<data>
<name>b</name>
<echDate>15/01/2013</echDate>
</data>
</dataset>
</fx:Model>
</fx:Declarations>
<s:VGroup>
<mx:DataGrid
width="386" height="100" dataProvider="{cells.data}"
alternatingItemColors="[0xffffff]"
horizontalGridLineColor="0x999999"
horizontalGridLines="true"
verticalGridLineColor="0x999999"
sortableColumns="false"
resizableColumns="false" selectable="false">
<mx:columns>
<mx:DataGridColumn dataField="name" headerText="Name"/>
<mx:DataGridColumn dataField="echDate" headerText="Date"
itemRenderer="com.dgdatefield.irDateD"/>
</mx:columns>
</mx:DataGrid>
<mx:DataGrid
width="386" height="100"
dataProvider="{cells.data}">
<mx:columns>
<mx:DataGridColumn dataField="name" headerText="Name"/>
<mx:DataGridColumn dataField="echDate" headerText="Date"/>
</mx:columns>
</mx:DataGrid>
</s:VGroup>
</s:Application>
//Renderer
<?xml version="1.0" encoding="utf-8"?>
<s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
focusEnabled="true">
<fx:Declarations>
<mx:DateValidator
id="dateValidator"
source="{dfDG}"
property="text"
inputFormat="DD/MM/YYYY"
wrongLengthError="The date format is not correct"
allowedFormatChars="/ - ."/>
</fx:Declarations>
<fx:Binding source="dfDG.text" destination="data.echDate"/>
<s:HGroup width="100%" horizontalAlign="center">
<mx:DateField
id="dfDG" text="{data.echDate}"
editable="true"
formatString="DD/MM/YYYY"
yearNavigationEnabled="true"
width="120"/>
</s:HGroup>
</s:MXDataGridItemRenderer>
I hope it can help you

FLEX DataGrid Column Modification

I am new in Flex. I want to display data in attached grid format. I found best way to display in DataGrid. But the CIs column has multiple entries so because of that other columns will duplicate. I want to avoid duplicate data in other columns. Attached screenshot is of excel, i want to achieve same format in Flex. I am using Flex 4.5
Best way I see to do what you want is to create a custom item renderer for the CLS column and have it render as a list control. That way, the CLS item in each row will only take up one row instead of multiple rows.
You can get some idea by following code... you can build logic using below concept for your implementation. Below example is sample not complete according to your requirement: -
<?xml version="1.0" encoding="utf-8"?>
<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" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.core.UIComponent;
import mx.events.AdvancedDataGridEvent;
import mx.events.ListEvent;
[Bindable]
private var dpHierarchy:ArrayCollection= new ArrayCollection
([
{name:"Barbara Jennings", address: "Arizona", contact:999970, orders:["1"]},
{name:"Dana Binn", address: "Arizona", contact:9999130, orders:["2"]},
{name:"Joe Smith", address: "California", contact:9992929, orders:["3"]},
{name:"Alice Treu", address: "California", contact:9999230, orders:["4"]}
]);
private function addOrder():void
{
for(var i:int = 0 ; i < dpHierarchy.length; i++)
{
if(dropDownID.selectedItem.name == dpHierarchy[i].name)
{
var itemUpdated:Array = dpHierarchy[i].orders;
itemUpdated.push(orderNumber.text);
dpHierarchy[i].orders = itemUpdated;
dpHierarchy.itemUpdated(dpHierarchy[i]);
break;
}
}
}
]]>
</fx:Script>
<mx:VBox x="30" y="30" width="450" height="400">
<s:HGroup>
<s:Button label="Add Order" click="addOrder()"/>
<s:DropDownList id="dropDownID" dataProvider="{dpHierarchy}" labelField="name" selectedIndex="0"/>
<s:TextInput id="orderNumber" width="100"/>
</s:HGroup>
<mx:AdvancedDataGrid id="myADG"
dataProvider="{dpHierarchy}"
variableRowHeight="true" wordWrap="true">
<mx:columns>
<mx:AdvancedDataGridColumn dataField="name" headerText="Name"/>
<mx:AdvancedDataGridColumn dataField="address" headerText="Address"/>
<mx:AdvancedDataGridColumn dataField="contact" headerText="Contact"/>
<mx:AdvancedDataGridColumn dataField="orders" headerText="Orders" itemRenderer="OrderItemRenderer"/>
</mx:columns>
</mx:AdvancedDataGrid>
</mx:VBox>
</s:Application>
Item Reenderer Name: - OrderItemRenderer
<?xml version="1.0" encoding="utf-8"?>
<s:MXAdvancedDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
focusEnabled="true" width="90%" height="90%">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var initDG:ArrayCollection = new ArrayCollection();
override public function set data(value:Object):void
{
super.data = value;
initDG = new ArrayCollection(value.orders)
}
]]>
</fx:Script>
<mx:DataGrid id="dataGrid" dataProvider="{initDG}" headerHeight="0" rowCount="{initDG.length}"/>
</s:MXAdvancedDataGridItemRenderer>

How to add items to a datagrid at runtime in flex4?

My UserInfromation form contains two input fields username,location(city) and one radio button as gender and two buttons add and reset.when I click on add button that data will be added in datagrid at runtime.
I am unable to reproduce how will i do this.
can anyone help me on this with example?
Here is a sample app (with code):
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
private var ac:ArrayCollection = new ArrayCollection();
protected function addBtn_clickHandler(event:MouseEvent):void
{
var obj:Object = new Object();
obj.userName = userNameTI.text;
obj.location = locationTI.text;
ac.addItem(obj);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:Form width="100%">
<mx:FormItem label="UserName:">
<s:TextInput id="userNameTI"/>
</mx:FormItem>
<mx:FormItem label="Location:">
<s:TextInput id="locationTI"/>
</mx:FormItem>
</mx:Form>
<s:Button id="addBtn" label="Add" click="addBtn_clickHandler(event)"/>
<mx:DataGrid id="dg" width="100%" dataProvider="{ac}"/>
</s:WindowedApplication>
Couple of things are left for you as an exercise :)