开发者

csv in datagrid

Generate csv format from mysql that contain a set of data, now I could import into datagrid to display each value but I have a problem which value will appear with double quote ("1", "2") instead of (1,2,3, etc), what is the best way to remove the double quote ("")?

"1";"asd@sad.com";"asdasdasd";"1302531993"
"2";"asdasd@asd.com";"werwetewt";"2开发者_如何学编程34235"
"3";"asdasd@asd.com";"werwetewt";"234235"


You can try something like the following:

<?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>


Try using str.replace("\"", ""); where str is the untrimmed string. String.replace in AS3 finds all the occurrences of the first arg and replaces them with the second arg. Both of those could be regular expressions if you want, but in your situation a simple escape character \" will work just fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜