views:

244

answers:

1

I receive from the httpservice with a certain frequency a string like this: 1#3#234525234 where row#column#value I want to display into datagrid the above string at real time; at the moment my code displays the whole string, composed by many strings like the one above. How can i solve the problem? Thanks in advance

I have the following code

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
xmlns="*" creationComplete="srv.send()" >
<mx:Script>
<![CDATA[
    import mx.effects.effectClasses.AddItemActionInstance;
    import mx.effects.AddItemAction;
    import mx.collections.ArrayCollection;
    import mx.controls.dataGridClasses.DataGridColumn;
    import mx.events.*;
       import mx.rpc.events.ResultEvent;
       import mx.rpc.events.InvokeEvent;
       import mx.rpc.events.FaultEvent;
       import mx.rpc.AsyncRequest;
       import mx.rpc.AsyncResponder;
       import mx.rpc.AsyncToken;
       import mx.rpc.AbstractInvoker;
       import mx.controls.Alert;
       import mx.core.Container;
       import mx.core.IDataRenderer;
       import mx.controls.dataGridClasses.DataGridItemRenderer;
       import mx.controls.DataGrid;
       import flash.display.DisplayObject;
       [Bindable]
       public var i:Number;
       public var source:String;
       [Bindable]
       public var row:Array;
       public var column:Array;
       public var value:Array;
       public function cycle(source:String):void
       {
       var data:Array = source.split('#');
       i=0;
       for each(data in source)
       {
        row[i]=data[i]
        column[i]=data[i+1]
        value[i]=data[i+2]

        i=i+3
       }
       }

]]>
    </mx:Script>    
    <mx:HTTPService 
     id="srv" 
     url="http://10.15.20.75/server4flex/servlet/Datagen" 
     method="GET"
     />  
    <mx:TextArea text="{cycle(srv.lastResult.toString())}" x="10" y="50" 
    width="699" height="59"/>
    <mx:AdvancedDataGrid dataProvider="{source}"  liveScrolling="true" id="dg" 
    x="10" y="117" width="621">
            <mx:columns>
                 <mx:AdvancedDataGridColumn dataField="{row[i]}"
               headerText="Riga"/>
                 <mx:AdvancedDataGridColumn dataField="{column[i]}"
               headerText="Colonna"/>
                 <mx:AdvancedDataGridColumn dataField="{value[i]}" 
               headerText="Valore"/>
            </mx:columns>
    </mx:AdvancedDataGrid>

A: 

First of all, the loop seems to be wrong, you iterate over a string source, not an array. Secondly, your grid should bind to some properties of objects stored in a dataProvider (Array, ArrayCollection, etc.). So the data provider shouldn't be a string, as in your code. Thridly, dataField is the name of the field of the object stored in the dataProvider, which value should be displayed in the grid cell.

Basicly, you need to parse your incoming string, store each row in an object and store all these objects in a collection.

So, the code should be something like:

[Bindable]
private var dataList:ArrayCollection;

public function cycle(source:String):void
{
var ac:ArrayCollection = new ArrayCollection();
for(var i:int = 0; i < data.length; i += 3) {
var dataObj:Object = {row: data[i], column: data[i+1], value: data[i+2]};
ac.addItem(dataObj);
}
dataList = ac;
}

<mx:AdvancedDataGrid dataProvider="{dataList}"  liveScrolling="true" id="dg" 
    x="10" y="117" width="621">
            <mx:columns>
                        <mx:AdvancedDataGridColumn dataField="row"
               headerText="Riga"/>
                        <mx:AdvancedDataGridColumn dataField="column"
               headerText="Colonna"/>
                        <mx:AdvancedDataGridColumn dataField="value" 
               headerText="Valore"/>
            </mx:columns>
    </mx:AdvancedDataGrid>
Hrundik
it doesn't work, i think because of the # separator omitted in you code
Franky
Yes, sure, you need to add `var data:Array = source.split('#');` before the loop.
Hrundik
still not working, datagrid displays nothing
Franky
Do you call `cycle(source)` method?
Hrundik
i have to call the httpservice:so i call in dataProvider="{cycle(srv.lastResult.toString())}"
Franky
In this case, make `cycle(source)` method return ArrayCollection it's been populating.
Hrundik
it works but the datagrid displays all the data, i want to display one data real time at each srv.send, not the whole data
Franky
Then add to `dataList` ArrayCollection the only object you want to display. I don't know what you mean by 'one data'
Hrundik
httpservice send data like this:1#2#789# 3#1#912# 4#2#613# each x seconds;one data means only one string (row#col#value) each x second;if i send 20 of those single string my application displays the whole 20 strings, but i want to display in real time one by one at each srv.send
Franky