Not yet rated

Problem

For example, if I have a table of 10 rows and 10 cells of data, and I want to be able to refer to each uniquely, what is the most efficient way to do this?

Solution

I have created an example in which only unique vaue is used for the datagrid from the dataprovider. Use unique value functionality for updated data(collection) before setting dataprovider to datagrid.

Detailed explanation

There are various way to get data from the back-end(database) side like HTTPService, WebService and Remoting.
 
ListCollectionView is the most efficient  way to give data to Grid.
 
Using Item event you can refer each row uniquely.
 
In addition I have use Objectutil to find out duplicate object in collection. I have attaced sample file.
(this example is as per my understanding of recipe request, correct if differ).
 
                <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
creationComplete="onCreationComplete(event)">

<mx:Script>
<![CDATA[
import mx.events.ListEvent;
import mx.controls.Alert;
import mx.events.ItemClickEvent;
import mx.utils.ObjectUtil;
import mx.events.FlexEvent;
import mx.collections.ArrayCollection;

private var data_Contacts : ArrayCollection = new ArrayCollection
([
{name:"Virat", email:"vr@gl.com", contact:"456789"},
{name:"Sunil", email:"sr@gl.com", contact:"123456"},
{name:"Mayur", email:"mr@gl.com", contact:"123789"},
{name:"Dipes", email:"dr@gl.com", contact:"123456"},
{name:"Virat", email:"vr@gl.com", contact:"456789"},
{name:"Sunil", email:"sr@gl.com", contact:"123456"},
{name:"Bhargav", email:"br@gl.com", contact:"789456"}
]);

private function onCreationComplete(event:FlexEvent):void
{
var uniqueData : ArrayCollection = getUniqueValues(data_Contacts);

var obj:Object = uniqueData.getItemAt(0);
dataG.dataProvider = uniqueData;
}

private function getUniqueValues (collection : ArrayCollection) : ArrayCollection 
{
var length : Number = collection.length;
   var unique : ArrayCollection = new ArrayCollection();

for(var i : Number = 0; i < length; i++){
var bool : Boolean = compareObject(unique,collection.getItemAt(i));
if(!bool) 
unique.addItem(collection.getItemAt(i));
}
   return unique;
}

public function compareObject(source:ArrayCollection,obj:Object):Boolean
{
   var length : Number = source.length;
var bool : Boolean = false;     
for(var i : Number = 0; i < length; i++){
var index:int = ObjectUtil.compare(obj,source.getItemAt(i) );
if(index==0){
bool = true;
break;
} 
}
   return bool;
}

private function onItemClick(event:ListEvent):void
{
Alert.show("Row " + event.rowIndex + " Clicked" );
}
]]>
</mx:Script>

<mx:DataGrid id="dataG"  height="400" width="400" itemClick="onItemClick(event)" />

<mx:Form>
<mx:FormItem label="Name :">
<mx:Label text="{dataG.selectedItem.name}" />
</mx:FormItem>
<mx:FormItem label="Email :">
<mx:Label text="{dataG.selectedItem.email}" />
</mx:FormItem>
<mx:FormItem label="Contact :">
<mx:Label text="{dataG.selectedItem.contact}" />
</mx:FormItem>
</mx:Form>


</mx:Application>
    
 
 
Regards,
Virat Patel

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes