Here is an example (2.0) that dynamically builds a datagrid.  It uses
logic based on the structure of the xml dataProvider, but could just as
easily use metadata from a data call.

http://www.cflex.net/showfiledetails.cfm?ChannelID=1&Object=File&objectI
D=552

 

Tracy

 

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of deepa golamudi
Sent: Monday, March 26, 2007 3:17 AM
To: [email protected]
Subject: [flexcoders] Datagrid Help

 

Hai everybody,

I am new to flex environment 

I am using flex 1.5 where i am trying topopulate a datagrid using remote
object mechanism invking java object.

I am getting the coloumn names using a remote object and records with
another.i am sure of getting the DB values into Flex component as i have
seen them using an alert.

I am unable to view the records and column names at a time.

 

Pl help me ASAP.

i also give u the code for ur reference

 

My mxml file

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml
<http://www.macromedia.com/2003/mxml> " initialize="initApp()"
backgroundColor="#FFFFFF" backgroundImage="@Embed('bg1.png')"
height="716" width="1034">
<mx:Style source="styles.css" />
<mx:Script>
<![CDATA[
var tablename:String;
var tblList:Array;
var tbl;
var dlist:Array;
[Bindable]
var final:Array;
var names:Array;
var dg;
var i:Number;
function initApp()
{
retrieveFromSession();
sessionRetrieved();  

} 

  function retrieveFromSession() 
   {
            servlet.session("get", "cartInfo");
   
        }
  function logout1(cartInfo)
{
cartInfo.name=" ";
servlet.session("set","cartInfo",cartInfo);
getUrl('select.mxml');
}
 function sessionRetrieved(cartInfo)
  {
    userid.text=cartInfo.name;
 //alert(userid.text);
 tblr.getrecs(userid.text);
 tsel.text="Table Selected: "+userid.text;
  }
function names1(event)
{
tblList=event.result;
dg.dataProvider=tblList;

//alert("length is "+tblList);
//dg.dataProvider=tblList;
/*for(i=0;i<=names.length;i++)
{
var st:String=names[i].toString;
dg.columns[st]=tblList[i];
}*/
}
function log()
{
data12.getdatas(userid.text);
//dg.dataProvider=tblList;
//dg.columnNames=names;
//tblList=mx.utils.ArrayUtil.toArray(event.result);
//var t=tblList.length;
//alert(t);
//dg.dataProvider=tblList;
}
function resultHandler(event)
{
names=mx.utils.ArrayUtil.toArray(event.result);
this.destroyChild(dg);
tblList=mx.utils.ArrayUtil.toArray(event.result);
dg=cv.createChild(mx.controls.DataGrid,"datagrid",{x:300,columnNames:nam
es,y:249,width:620});
//dg.columnNames=names;
//dg.dataProvider=tblList;
}

]]>
</mx:Script>
<mx:Canvas id="cv" width="936" height="641">
  <mx:Button x="687" y="569"  label="Finish"
click="getUrl('view.mxml')"/>
  <mx:Button x="599" y="569"  label="DELETE"/>
    <mx:Button x="506" y="568"  label="UPDATE"/>
    <mx:Button x="433" y="568"  label="ADD"/>
 <mx:Label id="userid" x="86" y="345" visible="false"/>
    <mx:Label id="tsel" x="373" y="195" width="226" height="21"/>    
    <mx:FormHeading x="419" y="92" fontSize="20"  label="DYNAMIC
CHARTING COMPONENT" width="407" height="33" backgroundColor="#FFFFFF"/>
    <mx:Button label="Get Records" x="549" y="526" click="log()" />
    <mx:Link x="822" y="173"  label="MainMenu" width="78" height="22"
click="getUrl('main.mxml')" color="#0000FF" textDecoration="none"
fontSize="12"  />
    
</mx:Canvas>

  <mx:RemoteObject id="data12" source="TableData" result="names1(event)"
fault='alert("TableData class problem")'>
    <mx:method name="getDatas"></mx:method>    
    </mx:RemoteObject>
 
   <mx:RemoteObject source="servlet" id="servlet" showBusyCursor="true"
        result="sessionRetrieved(event.result)"
        fault="mx.controls.Alert.show(event.fault.faultstring,
'Error')"/>
  
  <mx:RemoteObject id="tblr" source="ColNames"
result="resultHandler(event)" fault='alert("TableData class problem")'>
    <mx:method name="getrecs"></mx:method>    
    </mx:RemoteObject>
  
</mx:Application>

 

my java classes for column names retrieval

 

//package samples.explorer;

import java.sql.*; 
import java.util.ArrayList;

public class ColNames{
final static String jdbcURL = "jdbc:odbc:chart";
  final static String jdbcDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
public int valid=0;

//Connect  q=new Connect();


public ColNames()
{
}


    
public ArrayList getrecs(String tableid)
 {
ArrayList list = new ArrayList();

 try
  {
  Class.forName(jdbcDriver);
      Connection con = DriverManager.getConnection(jdbcURL,"","");
//  Connection Conn=q.establish_Connection();
  Statement s = con.createStatement();
ResultSet rs= s.executeQuery("select * from "+tableid);
ResultSetMetaData rsmd = rs.getMetaData();
      int columnCount = rsmd.getColumnCount();
      for(int col = 1; col <= columnCount; col++) {
    String name=rsmd.getColumnLabel(col);
        list.add(name);
   }
   
  
  s.close();  
  con.close();
  }
  
  catch(Exception e)
  {
  //System.out.println("TechRP ramana"+e);
  }
  return list;
 }


public static void main(String args[])
{

ColNames q=new ColNames();


ArrayList a = new ArrayList();  
a=q.getrecs("Exam_Details");
for (int i = 0; i < a.size(); i++) {
 System.out.println((String)(a.get(i)));
}
}
} 

 


my java Class for fetching the records

 

//package samples.explorer;

import java.sql.*; 
import java.util.ArrayList;

public class TableData{
 static String db="chart";
final static String jdbcURL = "jdbc:odbc:"+db;
final static String jdbcDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
public int valid=0;
Connection con =null; 
Statement s = null;
public TableData()
{
}
public ArrayList getdatas(String tableid)
{
ArrayList listy = new ArrayList();
ArrayList fullList = new ArrayList();

 try
  {
  Class.forName(jdbcDriver);
con=DriverManager.getConnection(jdbcURL,"","");
s=con.createStatement();
  int i =0;
ResultSet rs= s.executeQuery("select * from "+ tableid);
ResultSetMetaData rsmd = rs.getMetaData();
 int numCols = rsmd.getColumnCount();


    while (rs.next()) {
     listy = new ArrayList();
     for(i=1;i <= numCols;i++){
      String str1=rs.getString(i);
      listy.add(str1);
     }
     fullList.add(listy);
    
         }
  }
  catch(Exception e)
   {
  System.out.println("TechRP"+e);
   } 

  return fullList;
}

public static void main(String args[])
{
TableData q=new TableData();
ArrayList a = new ArrayList();  
a=q.getdatas("Report");
int j=0;
for (int i = 0; i < a.size(); i++) 
 {
    System.out.println((a.get(i)));
 j++;
           System.out.println(j);  
 }
}
} 


 

 

 

Deepa

 

  

________________________________

Here's a new way to find what you're looking for - Yahoo! Answers
<http://us.rd.yahoo.com/mail/in/yanswers/*http:/in.answers.yahoo.com/>  

 

Reply via email to