11 Replies Latest reply on Oct 30, 2008 12:04 PM by R1R_7humbs

    Trying to assign an XML attribute value to a button label

    R1R_7humbs
      I have a Flex app (developed in FLEX 2) that reads from multiple XML files and populates datagrids with element values. What I'm trying to do now, is to create a second column with a button, enabling users to view archived versions for each current report.

      My problem is: I can't get the <mx:Button> label to accept the attribute "name". I've tried atleast 10 - 20 different syntax.

      My XML file looks like this:
      <metrics>
      <report name="test">
      <link>test Report 10/28/2008</link>
      <url>test-10_28_2008.zip</url>
      <status>active</status>
      </report>
      </metrics>

      The mxml looks like this:
      <mx:Button buttonMode="true" useHandCursor="true" click="handleClick()" label="{data.@name}" width="80">
      <mx:Script>
      <![CDATA[
      public function handleClick():void{
      var url:URLRequest = new URLRequest([L=http://new.test.com/pages/r_archive_apps/"+data.link+".html");[/L]]http://n ew.test.com/pages/r_archive_apps/"+data.link+".html");[/L][/L]
      navigateToURL(url,"_blank");
      }
      ]]>
      </mx:Script>
      </mx:Button>

      When I try to label a button with an element it works fine.

      Some of the other sytax I've used are:
      - label="{data.report.@name}"
      - label="{data.report.(@name=='test')}"
      - label="{data.report.(@name='test')}"
      - label="{data.@name}"
      - label="{data.metrics.report.@name}"
      - label="{data.metrics.report.(@name=='test')}"
      - label="{data.metrics.report.(@name='test')}"
        • 1. Re: Trying to assign an XML attribute value to a button label
          rtalton Level 4
          I used this code and your XML data, and it works. Copy and paste this code into a new Flex MXML application and try it...

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
          <mx:XML id="data" source="metrics.xml"/>
          <mx:Button label="{data.report.@name}"/>
          </mx:Application>

          • 2. Re: Trying to assign an XML attribute value to a button label
            R1R_7humbs Level 1
            Thanks that would work but it doesn't in my situation. the word "data" isn't the id of anything, it's a reference to the data object (xmlList) passed to the datagrid as a dataprovider.

            If I do this: label={data}, the buttons label is unique for each report child node i.e. it shows the following:
            <report name="test><link>test Report 10/28/2008</link><url>test\report-10_28_2008.zip</url><status>active</status></report>

            by looking at that, it leads me to believe that I can get to the name attribute with what you suggest but it simply doesn't work. If I do the following: label={data.report.@name==''} it prints a boolean value on the button of false.

            This makes no sense to me because I can see that all of the needed information is included in the data object, yet the only piece I can't get from it is the "name" attribute. All of the elements work perfectly.

            Thanks

            quote:

            Originally posted by: rtalton
            I used this code and your XML data, and it works. Copy and paste this code into a new Flex MXML application and try it...

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
            <mx:XML id="data" source="metrics.xml"/>
            <mx:Button label="{data.report.@name}"/>
            </mx:Application>




            • 3. Re: Trying to assign an XML attribute value to a button label
              rtalton Level 4
              Can you post some code so we can see how you are using the button? I think you may be using the button within a datagrid itemRenderer, which might make a difference.
              • 4. Re: Trying to assign an XML attribute value to a button label
                R1R_7humbs Level 1
                quote:

                Originally posted by: rtalton
                Can you post some code so we can see how you are using the button? I think you may be using the button within a datagrid itemRenderer, which might make a difference.


                You're right, the button is in a datagrid itemRenderer. I've pasted more dataGrid code below - thanks again.


                <mx:DataGrid id="dgCatalog" dataProvider="{_xlcCatalog}" rowCount="4" editable="false" sortableColumns="false"
                left="148" top="65" bottom="42" borderStyle="solid" alternatingItemColors="[#ecf8ff, #ffffff]"
                themeColor="#ffff80" alpha="1.0" cornerRadius="0" dropShadowEnabled="true" dropShadowColor="#000000" width="549"
                creationCompleteEffect="{glow3}">
                <mx:columns>
                <mx:Array>
                <mx:DataGridColumn editable="false" headerText="Daily - Report Names" dataField="link" textAlign="left" width="200">
                <mx:itemRenderer>
                <mx:Component>
                <mx:LinkButton click="handleClick()" label="{data.link}" >
                <mx:Script>
                <![CDATA[
                public function handleClick():void{
                var url:URLRequest = new URLRequest(" http://test.new.com/test/"+data.url);
                navigateToURL(url,"_blank");
                }
                ]]>
                </mx:Script>
                </mx:LinkButton>
                </mx:Component>
                </mx:itemRenderer>
                </mx:DataGridColumn>
                <mx:DataGridColumn editable="false" headerText="Daily - Report Archives" dataField="link" textAlign="left" width="80">
                <mx:itemRenderer>
                <mx:Component>
                <mx:Button buttonMode="true" useHandCursor="true" click="handleClick()" label="{data.report.@name}" width="80">
                <mx:Script>
                <![CDATA[
                public function handleClick():void{
                var url:URLRequest = new URLRequest(" http://test.new.com/pages/test_apps/"+data.link+".html");
                navigateToURL(url,"_blank");
                }
                ]]>
                </mx:Script>
                </mx:Button>
                </mx:Component>
                </mx:itemRenderer>
                </mx:DataGridColumn>
                <!--mx:DataGridColumn headerText="URL" dataField="url" width="350"/>
                <mx:DataGridColumn headerText="Status" dataField="status" width="70"/-->
                </mx:Array>
                </mx:columns>
                </mx:DataGrid>
                • 5. Re: Trying to assign an XML attribute value to a button label
                  rtalton Level 4
                  This helps.
                  I made some changes to your code and the metrics.xml file (added two more records). Not sure if this is what you are after, but take a look:

                  http://www.anaheimwib.com/_comps/xmlTest/

                  You appear to be using just one element in your xml file. I used three for testing. But anyway, the button label is appearing now using (data.@name) so I think your XML file structure may have tripped you. Remember that you DO NOT reference the root node "metrics". So the reference is simply data.@name since you are refering to the element "report" and name is an attribute of report. The "data" reference in the data grid IS the report element, so you don't reference it by using report. That would be like saying "report.report.@name".

                  All the code is here so you can look at my edits and decide for yourself. (for instance, I removed the <mx:array> tags as they are unnecessary).
                  • 6. Re: Trying to assign an XML attribute value to a button label
                    R1R_7humbs Level 1
                    Thanks rtalton.

                    Believe it or not I'm still having problems. When I try to label the button with data.@name the application won't run at all. There are no warnings or errors, so I can't figure out what the problem is.

                    Any idea on why the application won't run at all now? When I launch I get a blank screen in my browser.

                    Thanks again.
                    • 7. Re: Trying to assign an XML attribute value to a button label
                      R1R_7humbs Level 1
                      I've attached all of the code below... hopefully it's legible in the forum. The only thing I can think of is that I'm using an XMLlist and not just XML as the dataProvider.

                      Also, the AS code in the middle of the app is just code to sort the results (XML data) for each grid and a refresh function that updates the grids every 5 seconds.
                      • 8. Re: Trying to assign an XML attribute value to a button label
                        rtalton Level 4
                        Good morning!
                        Yes it may be because you are setting the data provider to be a collection object. I've had so many problems with doing it that way that I just throw straight XML at the datagrid and let it convert to a collection, which it does by default anyway. As long as you are not changing the original data, and only allowing changes through the datagrid, this is ok to do as the changes will be detected through the datagrid, but not the original xml.

                        Another possibility could be that you are just creating ONE object in the _xlcCatalog collection, instead of what you are expecting. So the data provider has only one "node" holding all of your data, instead of a "node" for each "record" you are expecting.

                        I can't run this code because the missing skins are creating errors. So I can't debug this for you. But try debugging your code. Set a breakpoint on the result handler line where it is setting the _xlcCatalog variable. Run debug, and when the app stops, look in the variables window for _xlcCatalog. See if the name attribute is being returned as you expect. I think you will be able to see what is happening.

                        If you can put your app on a webserver, with source view enabled, I can take another look at it.
                        • 9. Trying to assign an XML attribute value to a button label
                          R1R_7humbs Level 1
                          That makes sense, I didn't know that the collection would manipulate the data structure like that. I needed to use the collection in the AS code in order to use a .refresh() function.

                          I've hosted the application @ my site.

                          The link to the source in the app is wrong... this link exposes all of the files. I also wasn't sure which file to point to in order to make the view source feature look like your flex demo did.

                          I'm working on debugging by setting the breakpoints as you suggested above.

                          Thanks again for your help.
                          • 10. Re: Trying to assign an XML attribute value to a button label
                            rtalton Level 4
                            For source view in your app, simply check "Enable source view" when you "Export Release Build". You can also decide what files to include by clicking the "Choose Source Files" button on the same screen, or Flex will include everything by default (xml, actionscript classes, images, etc).

                            You are right, you'll need to keep the dp as a XMLListCollection so you can use the refresh method.I believe the button label problem is unrelated to that. When you debug I'm confident you'll spot it right away. Don't give up. You're almost there!
                            • 11. Trying to assign an XML attribute value to a button label
                              R1R_7humbs Level 1
                              I can't debug... the IDE is saying that I don't have an up to date version of Flash 9 deugging software. Unfortunately, I can't install it because my PC has access restrictions.

                              I asked the person that's building the XML if they could easily add another child element, and fortunately, it's no problem. I still want to figure this out and I'm sure I can get it, i just need more time to re-engineer the application and learn more about FLEX / AS.

                              I really appreciate all of your help and I'm not giving up, just taking a different route for now.

                              Thanks again and I'll let you know when I get it - with the aid of the debugging mode.