SitePoint Sponsor

User Tag List

Results 1 to 1 of 1
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Location
    Middletown, CT.
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Flex .csv and datagrid

    I am using a csv parser to parse some csv data and display it in a datagrid. I am also using the Flex DataGridExporter class available on the web to get the csv data out of the data grid and store it. I am running into some odd bugs.

    1. when the data comes in ( from a recently exported to csv from a datagrid ) I am losing the last person in the list ( though they are really there in the data, just not displaying in the datagrid ).
    2. I am also losing the letter in each person's email ( someone@email.co ) but in the actual data it DOES NOT look like that, it has the last letter...BUT, if i save the list ( to a database ), it does in-fact save that way ( with the letter cut off ) ...any ideas?
    Attached is screenshots of the output, and the saved data, as well as the traced data.
    Below is the csv parser and datagrid exporter.

    Code:
    trace("Parsing CSV Data...");
    			var properties:Array = new Array();
    			var headings:Boolean = false;
    			var carriage:Number = new Number();
    			var comma:Number = new Number();
    			var cursor:Number = new Number();
    			var sub:Number = new Number();
    			var item:Object = new Object();
    			var value:String = new String();
    			var line:String = new String();
    	    	var contacts:Array = new Array();	
    		
    			var result:String = csvData;
    			
    			 while( result.indexOf( "\n", cursor ) != -1 ) {
    			    carriage = result.indexOf( "\n", cursor );
    			    line = result.substring( cursor, carriage );
    			
    			    cursor = 0;
    			    sub = 0;
    			
    			    item = new Object();
    			
    			   while( line.indexOf( ",", cursor ) != -1 ) {
    			      comma = line.indexOf( ",", cursor );
    			      value = line.substring( cursor, comma );
    				  
    			      if( !headings ) {
    			        properties.push( value );
    			      } else {
    			        item[properties[sub]] = value;
    			        //trace("email:\n" + value.toString());
    			      }
    			
    			      cursor = comma + 1;
    			      sub++;
    			    }
    			
    			    value = line.substring( cursor, line.length - 1 );
    			
    			    if( !headings ) {
    			      properties.push( value );
    			      headings = true;
    			      //trace("headings:\n" + value.toString());
    			    } else {
    			      item[properties[sub]] = value;
    			      contacts.push( item );
    			      //trace("contact:\n" + item.Name.toString());
    			    }
    			    cursor = carriage + 1;
    			  }
    			  
    			  trace("Parsing Complete " + contacts.length + " contacts found: \n");
    			  
    			  return contacts;
    and the data exporter:

    Code:
    package com.abdulqabiz.utils
    {
    	import mx.controls.DataGrid;
    	import mx.controls.dataGridClasses.DataGridColumn;
    	import mx.collections.ArrayCollection;
    	import mx.collections.XMLListCollection;
    	import mx.collections.IList;
    	import mx.collections.IViewCursor;
    	import mx.collections.CursorBookmark;
    
    	public class DataGridDataExporter
    	{
    
    		public static function exportCSV(dg:DataGrid, csvSeparator:String=",", lineSeparator:String="\n"):String
    		{
    			var data:String = "";
    			var columns:Array = dg.columns;
    			var columnCount:int = columns.length;
    			var column:DataGridColumn;
    			var header:String = "";
    			var headerGenerated:Boolean = false;
    			var dataProvider:Object = dg.dataProvider;
    
    			var rowCount:int = dataProvider.length;
    			var dp:Object = null;
    
    		
    			var cursor:IViewCursor = dataProvider.createCursor ();
    			var j:int = 0;
    			
    			//loop through rows
    			while (!cursor.afterLast)
    			{
    				var obj:Object = null;
    				obj = cursor.current;
    				
    				//loop through all columns for the row
    				for(var k:int = 0; k < columnCount; k++)
    				{
    					column = columns[k];
    
    					//Exclude column data which is invisible (hidden)
    					if(!column.visible)
    					{
    						continue;
    					}
    				
    					data += column.itemToLabel(obj);
    					
    					if(k < (columnCount -1))
    					{
    						data += csvSeparator;
    						trace(data);
    					}
    
    					//generate header of CSV, only if it's not genereted yet
    					if (!headerGenerated)
    					{
    						header += column.headerText;
    						if (k < columnCount - 1)
    						{
    							header += csvSeparator;
    						}
    					}
    					
    				
    				}
    				
    				headerGenerated = true;
    
    				if (j < (rowCount - 1))
    				{
    					data += lineSeparator;
    				}
    
    				j++;
    				cursor.moveNext ();
    			}
    			
    			//set references to null:
    			dataProvider = null;
    			columns = null;
    			column = null;
    
    			return (header + "\r\n" + data);
    		}	
    	}
    
    }
    Attached Files Attached Files


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •