数据网格中的csv

时间:2011-05-03 15:12:53

标签: flash flex actionscript

从包含一组数据的mysql生成csv格式,现在我可以导入到datagrid中以显示每个值但是我有一个问题,哪个值将出现双引号(“1”,“2”)而不是(1 ,2,3等),删除双引号(“”)的最佳方法是什么?

"1";"asd@sad.com";"asdasdasd";"1302531993"
"2";"asdasd@asd.com";"werwetewt";"234235"
"3";"asdasd@asd.com";"werwetewt";"234235"

2 个答案:

答案 0 :(得分:3)

您可以尝试以下内容:

<?xml version="1.0" encoding="utf-8"?>
<s:Application creationComplete="init(event)" xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark">
    <fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;

        protected function init(event:FlexEvent):void
        {
            var beginningQuotesExp:RegExp = /^\s*"/gm;
            var result:String = csvData.replace(beginningQuotesExp, "");
            var endingQuotesExp:RegExp = /"\s*$/gm;
            result = result.replace(endingQuotesExp, "");
            var bodyQuotesExp:RegExp = /"\s*;\s*"/gm;
            result = result.replace(bodyQuotesExp, ";");
            var splitRegExp:RegExp = /\r*\n+|\n*\r+/gm;
            var lines:Array = result.split(splitRegExp);
            var dataProvider:ArrayCollection = new ArrayCollection();
            for each (var line:String in lines)
            {
                var lineArray:Array = line.split(";");
                var dataObject:Object =
                    { counter: lineArray[0], email: lineArray[1], name: lineArray[2], phone: lineArray[3] };
                dataProvider.addItem(dataObject);
            }
            dg.dataProvider = dataProvider;
        }
    ]]>
    </fx:Script>

    <fx:Declarations>
        <fx:String id="csvData">
            <![CDATA["1";"asd@sad.com";"asdasdasd";"1302531993"
"2";"asdasd@asd.com";"werwetewt" ;  "234235"
"3";"asdasd@asd.com";"werwetewt";"234235"]]>
        </fx:String>
    </fx:Declarations>
    <mx:DataGrid horizontalCenter="0" id="dg" verticalCenter="0">
        <mx:columns>
            <mx:DataGridColumn dataField="counter" headerText="counter" />
            <mx:DataGridColumn dataField="email" headerText="email" />
            <mx:DataGridColumn dataField="name" headerText="name" />
            <mx:DataGridColumn dataField="phone" headerText="phone" />
        </mx:columns>
    </mx:DataGrid>
</s:Application>

答案 1 :(得分:1)

尝试使用str.replace("\"", "");,其中str是未修剪的字符串。 AS3中的String.replace找到第一个arg的所有出现,并用第二个arg替换它们。如果你愿意,这两个都可以是正则表达式,但在你的情况下,一个简单的转义字符\"将正常工作。