0 Replies Latest reply: Apr 29, 2013 6:52 AM by Kariko1337 RSS

    XML Data in LinkBar

    Kariko1337 Community Member

      Hi guys,

       

      I'm new to FLEX and still learning a lot, but am struggling with the following issue,

       

      I have a simple "Book List" style application that i have created. It is linked to an XML document and i am able to edit/delete/add items in the XML document.

       

      Now what i want to do is have a LinkBar at the top with three buttons for different stypes of lists (different XML documents)

       

      I have tried to take my working application and put it into a LinkBar but it fails to work.

       

      Any help would be awesome.

       

      Cheers

       

      Kariko

       

      <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" initialize="{init()}"

                                                                  layout="absolute" height="451" width="542">

                <!-- // -->

                                    <mx:Script>

                                    <![CDATA[

                                              import mx.collections.XMLListCollection;

                                              // Code block 1

                                              private var myXml:XML;

                                              private var myLoader:URLLoader;

                                              [Bindable]

                                              private var myBooksCol:XMLListCollection;

                                              // --- //

                                              // Code Block 2 //

                                              public function init():void

                                              {

                                                        var dataFile:File = new File('app-storage:/data.xml');

                                                        if(dataFile.exists){

                                                                  var myReq:URLRequest = new URLRequest('app-storage:/data.xml');

                                                                  myLoader = new URLLoader()

                                                                  myLoader.addEventListener(Event.COMPLETE, dataComplete);

                                                                  myLoader.load(myReq);

                                                        }else{

                                                                  createFile()

                                                        }

       

                                              }

       

                                              // Code Block 3 //

                                              private function createFile():void

                                              {

                                                        var xmlToSave:XML = new XML('<flashEnabled><books>' +

                                                                  '<book>' +

                                                                  '<title>Contact1</title>' +

                                                                  '<series></series>' +

                                                                  '<author></author>'+

                                                                  '<ISBN></ISBN>' +

                                                                  '</book>' +

                                                                  '</books>' +

                                                                  '</flashEnabled>')

                                                        var saveStr:String = xmlToSave.toXMLString();

       

                                                        var file:File = new File('app-storage:/data.xml');

                                                        var fs:FileStream = new FileStream();

                                                        fs.open(file, FileMode.WRITE);

       

                                                        fs.writeUTFBytes(saveStr);

                                                        fs.close();

                                                        init();

                                              }

       

                                              //Code block 4

                                              private function dataComplete(e:Event):void

                                              {

                                                        myXml = new XML(myLoader.data.toString());

                                                        myBooksCol = new XMLListCollection(myXml..contact);

       

                                              }

       

                                              //Code Block 5

                                              private function updateList():void

                                              {

                                                        myBooksCol = new XMLListCollection(myXml..contact.(name.toLowerCase().indexOf(search_txt.text.toLowerCase( )) != -1 ))

                                              }

       

                                              //Code Block 6

                                              private function saveData(e:Event):void

                                              {

                                                        var xmlToSave:XML = new XML('<flashEnabled><books>'+myBooksCol.toXMLString()+'</books></flashEnabled>')

                                                        var saveStr:String = xmlToSave.toXMLString();

       

                                                        var file:File = new File('app-storage:/data.xml');

                                                        var fs:FileStream = new FileStream();

                                                        fs.open(file, FileMode.WRITE);

       

                                                        fs.writeUTFBytes(saveStr);

                                                        fs.close();

                                              }

       

                                              //Code Block 7

                                              private function addItem():void

                                              {

                                                        myBooksCol.addItem(new XML('<book>' +

                                                                  '<title>Contact1</title>' +

                                                                  '<series></series>' +

                                                                  '<author></author>' +

                                                                  '<ISBN></ISBN>' +

                                                                  '</book>'))

                                              }

       

                                              private function removeItem():void

                                              {

                                                        myBooksCol.removeItemAt(myDg.selectedIndex);

                                              }

       

                                    ]]>

                          </mx:Script>

                                    <!-- Code Block 8 -->

                                    <mx:DataGrid bottom="60" top="10" left="10" id="myDg" dataProvider="{myBooksCol}" editable="true" right="10">

                                    <mx:columns>

                                              <mx:DataGridColumn headerText="Title" dataField="title"/>

                                              <mx:DataGridColumn headerText="Series" dataField="series"/>

                                              <mx:DataGridColumn headerText="Author" dataField="author"/>

                                              <mx:DataGridColumn headerText="ISBN" dataField="ISBN"/>

                                    </mx:columns>

                          </mx:DataGrid>

                                    <mx:Label x="10" text="Search:" bottom="10"/>

                                    <mx:TextInput x="65" id="search_txt" bottom="10"/>

                                    <!-- Code block 9 -->

                                    <mx:Button x="233" label="Go!" id="go_btn" click="{updateList()}" bottom="10"/>

                                    <mx:Button label="Save" click="{saveData(event)}" id="saveButton" bottom="10" right="10"/>

                                    <mx:Button x="307" label="Add" click="{addItem()}" bottom="10"/>

                                    <mx:Button x="381" label="Remove" click="{removeItem()}" bottom="10"/>

       

      </mx:WindowedApplication>