10 Replies Latest reply on Aug 22, 2008 4:03 PM by Astrolive

    Using different dataProvider in the same DataGrid

    Astrolive
      Hi,
      I'm trying to use 2 different dataProviders to populate 1 dataGrid (using e4x):
      - column 1 of the dataGrid should be populated using dataProvider="{DataTbl1.Phase}
      - column 2 of the dataGrid should be populated using dataProvider="{DataTbl2.Options}
      So I created a class called MyDatagGridColumn which is as follow:
      package DualProvider
      {
      import mx.controls.dataGridClasses.DataGridColumn;
      public class MyDataGridColumn extends mx.controls.dataGridClasses.DataGridColumn
      {
      public var dataProvider:String;
      public function MyDataGridColumn()
      {
      super();
      }
      }
      }

      and I'm using it as shown in the code below. I don't get any errors when compiling or running the code but the dataGrid is just empty. Also I know that the labelFunction works as I tested it with a simple dataGrid.

      If you could help me with this that would be awesome. I'm a beginner and just spent days trying to figure this thing out.
      Thank you,

      Olivia

        • 1. Re: Using different dataProvider in the same DataGrid
          Level 7

          "Astrolive" <webforumsuser@macromedia.com> wrote in message
          news:g8f09b$s06$1@forums.macromedia.com...
          > Hi,
          > I'm trying to use 2 different dataProviders to populate 1 dataGrid (using
          > e4x):
          > - column 1 of the dataGrid should be populated using
          > dataProvider="{DataTbl1.Phase}
          > - column 2 of the dataGrid should be populated using
          > dataProvider="{DataTbl2.Options}
          > So I created a class called MyDatagGridColumn which is as follow:
          > package DualProvider
          > {
          > import mx.controls.dataGridClasses.DataGridColumn;
          > public class MyDataGridColumn extends
          > mx.controls.dataGridClasses.DataGridColumn
          > {
          > public var dataProvider:String;
          > public function MyDataGridColumn()
          > {
          > super();
          > }
          > }
          > }

          You're probably better off joining the two tables on the back end and
          returning it as one set of records.

          HTH;

          Amy


          • 2. Re: Using different dataProvider in the same DataGrid
            Astrolive Level 1
            Thank you Amy but I really want to be able to use different dataProviders for the same DataGrid. Any idea how to do that? or what could be wrong with my code?
            Thank you,

            Olivia
            • 3. Re: Using different dataProvider in the same DataGrid
              Level 7

              "Astrolive" <webforumsuser@macromedia.com> wrote in message
              news:g8f5op$5st$1@forums.macromedia.com...
              > Thank you Amy but I really want to be able to use different dataProviders
              > for the same DataGrid. Any idea how to do that? or what could be wrong
              > with my code?

              I think you're going to need to merge it one way or the other.


              • 4. Re: Using different dataProvider in the same DataGrid
                amthekkel Level 1
                hi,

                are the two tables from which you are trying to access data related? i had problem few days ago where i was trying to display the associated name for a foreign key. If its similar then might work in your case.
                I had two XMLListCollection. One was the dataprovider for the datagrid and the other containing the xml from the second table. I also created a object which iterated through the second XMLlist and stored each xml element in that XML in the object.

                I had a datagridcolumn for the foreignKey and called the label function. In the label function I accessed the object i had created for the second XMLList and then displayed the associated name based on the selectedItem.'s foriegn key.

                HTH.

                regards,
                abhi
                • 5. Re: Using different dataProvider in the same DataGrid
                  Astrolive Level 1
                  Hi abhi,
                  Yes the two tables are related. The content of the second table actually depends on options checked in the first table. Table 1 provides the user with a list of options he can pick from a datagrid (using checkboxes). If the user picked Options 1, 3, 5 and 11 for example, these options then appear in the first column of the second table. And the remaining of Table 2 is populated based on the options selected. The reason i'm using different XML files (and thus different dataproviders) is that I have up to 7 levels of decomposition and having everything in one big file would be a nightmare...
                  If you could give me more information, details and some code about how you did it that would be awesome :)
                  Thank you very much,

                  Olivia
                  • 6. Re: Using different dataProvider in the same DataGrid
                    amthekkel Level 1
                    Hi Olivia,
                    I am not sure of the way you mentioned the relationship between the two tables. I assume that you have a foreign key in place in table two that links back to table 1 along with the other columns you specified. From your original code snippet it looks like you are using a static xml file rather than table data populated as xml.
                    here is a sample of my two xml's i am using
                    <shiftplans>
                    <id>1</id>
                    <ShiftType_ID>3<ShiftType_ID>
                    <Name>dummy</dummy>
                    ......
                    </shiftplans>
                    <shiftplans>
                    ......
                    </shiftplans>

                    the second table has the following xml
                    <shifttype>
                    <ShiftType_ID>3</ShiftType_ID>
                    <ShiftTypeName>test1</ShiftTypeName>
                    </shiftType>
                    <shifttype>
                    .....
                    </shifttype>
                    regarding the code i used, below i am creating an object ShiftTypeIdMap , iterate through the xmllistcollection shiftypesxlc which contains the data from my second table.create an id element in the object ShiftTypeIDMap with
                    identifying key like an id, and associate it with an xml element from the ShiftTypesXLC list.
                    ShiftTypeIdMap = {};
                    var ShiftTypesCursor:IViewCursor =ShiftTypesXLC.createCursor();
                    while (!ShiftTypesCursor.afterLast)
                    {
                    var ShiftType:XML = XML(ShiftTypesCursor.current);
                    ShiftTypeIdMap[ShiftType.ShiftType_ID] = ShiftType;
                    ShiftTypesCursor.moveNext();
                    }


                    In my datagrid I have a the following column
                    <mx:DataGridColumn headerText="ShiftType" dataField="ShiftType_ID" labelFunction="getShiftName" />

                    and here is the function
                    public function getShiftName(item:Object,column:DataGridColumn):String
                    {
                    return ShiftTypeIdMap[item.ShiftType_ID].ShiftTypeName;
                    }

                    the ShiftTypeIdMap[item.ShiftType_ID] contains the xml element with the corresponding ShiftType_ID, so this would be one row from the second table, ie with all the options. I can then access the ShiftTypeName property in that element using the dot operator. In your case it would be option1, option 2, etc

                    Hope the code helps

                    regards
                    abhi
                    • 7. Re: Using different dataProvider in the same DataGrid
                      amthekkel Level 1
                      Hi Olivia,
                      I am not sure of the way you mentioned the relationship between the two tables. I assume that you have a foreign key in place in table two that links back to table 1 along with the other columns you specified. From your original code snippet it looks like you are using a static xml file rather than table data populated as xml.
                      here is a sample of my two xml's i am using
                      <shiftplans>
                      <id>1</id>
                      <ShiftType_ID>3<ShiftType_ID>
                      <Name>dummy</dummy>
                      ......
                      </shiftplans>
                      <shiftplans>
                      ......
                      </shiftplans>

                      the second table has the following xml
                      <shifttype>
                      <ShiftType_ID>3</ShiftType_ID>
                      <ShiftTypeName>test1</ShiftTypeName>
                      </shiftType>
                      <shifttype>
                      .....
                      </shifttype>
                      regarding the code i used, below i am creating an object ShiftTypeIdMap , iterate through the xmllistcollection shiftypesxlc which contains the data from my second table.create an id element in the object ShiftTypeIDMap with
                      identifying key like an id, and associate it with an xml element from the ShiftTypesXLC list.
                      ShiftTypeIdMap = {};
                      var ShiftTypesCursor:IViewCursor =ShiftTypesXLC.createCursor();
                      while (!ShiftTypesCursor.afterLast)
                      {
                      var ShiftType:XML = XML(ShiftTypesCursor.current);
                      ShiftTypeIdMap[ShiftType.ShiftType_ID] = ShiftType;
                      ShiftTypesCursor.moveNext();
                      }


                      In my datagrid I have a the following column
                      <mx:DataGridColumn headerText="ShiftType" dataField="ShiftType_ID" labelFunction="getShiftName" />

                      and here is the function
                      public function getShiftName(item:Object,column:DataGridColumn):String
                      {
                      return ShiftTypeIdMap[item.ShiftType_ID].ShiftTypeName;
                      }

                      the ShiftTypeIdMap[item.ShiftType_ID] contains the xml element with the corresponding ShiftType_ID, so this would be one row from the second table, ie with all the options. I can then access the ShiftTypeName property in that element using the dot operator. In your case it would be option1, option 2, etc


                      Hope the code helps

                      regards
                      abhi
                      • 8. Re: Using different dataProvider in the same DataGrid
                        Astrolive Level 1
                        Hi abhi,
                        Thank you very much for your help. I still have a few questions if you don't mind:
                        - which one of the file is the one you call ShiftTypesXLC?
                        - which one do you use as the dataProvider for your second datagrid?
                        - where in your code do you actually put these expressions?
                        ShiftTypeIdMap = {};
                        var ShiftTypesCursor:IViewCursor =ShiftTypesXLC.createCursor();
                        while (!ShiftTypesCursor.afterLast)
                        {
                        var ShiftType:XML = XML(ShiftTypesCursor.current);
                        ShiftTypeIdMap[ShiftType.ShiftType_ID] = ShiftType;
                        ShiftTypesCursor.moveNext();
                        }

                        As for me I have the following XML files:
                        DataTbl1 (<mx:XML id="DataTbl1" format="e4x" source="DataTbl1.xml"/>), which looks like this:
                        <TrafficPhase>
                        <Phase>
                        <PhaseName>Phase 1</PhaseName>
                        <Option id="1">
                        <name>Option 11</name>
                        <Imchecked>true</Imchecked>
                        <Option_Concept_ID>1</Option_Concept_ID>
                        </Option>
                        <Option id="2">
                        <name>Option 12</name>
                        <Imchecked>true</Imchecked>
                        <Option_Concept_ID>2</Option_Concept_ID>
                        </Option>
                        <Option id="3">
                        <name>Option 13</name>
                        <Imchecked>false</Imchecked>
                        <Option_Concept_ID>3</Option_Concept_ID>
                        </Option>
                        </Phase>
                        ....

                        and DataTbl3 (<mx:XML id="DataTbl3" format="e4x" source="DataTbl3.xml"/>), which looks like this:

                        <Improvement>
                        <Option_Concept_ID id="1">
                        <OpCon id="1">
                        <OpConName>OpCon 111</OpConName>
                        <OpConchecked>true</OpConchecked>
                        </OpCon>
                        <OpCon id="2">
                        <OpConName>OpCon 112</OpConName>
                        <OpConchecked>false</OpConchecked>
                        </OpCon>
                        <OpCon id="3">
                        <OpConName>OpCon 113</OpConName>
                        <OpConchecked>true</OpConchecked>
                        </OpCon>
                        <OpCon id="4">
                        <OpConName>OpCon 114</OpConName>
                        <OpConchecked>false</OpConchecked>
                        </OpCon>
                        </Option_Concept_ID>
                        ....

                        In my case Option_Concept_ID is the key that links the 2 datagrids.

                        Also the following:
                        public var OptionConceptIDMap:Object = {};
                        var x:XMLListCollection = DataTbl3.Improvement;
                        var OpConCursor:IViewCursor=x.createCursor();
                        while(!OpConCursor.afterLast)
                        {
                        var Option_Concept:XML = XML(OpConCursor.current);
                        OptionConceptIDMap[Option_Concept_ID] = Option_Concept;
                        OpConCursor.moveNext()
                        }

                        This line: var x:XMLListCollection = DataTbl3.Improvement; gives me the error: 1152: A conflict exists with inherited definition mx.core:UIComponent.x in namespace public mx.core:IFlexDisplayObject public.

                        Again thank you very much for all your help,

                        Olivia
                        • 9. Re: Using different dataProvider in the same DataGrid
                          amthekkel Level 1
                          HI Olivia,

                          - which one of the file is the one you call ShiftTypesXLC?

                          The ShiftTypeXLC is the second xml in my previous post, the one with shiftType element rather than the shiftplans one.

                          - which one do you use as the dataProvider for your second datagrid?
                          for my data provider i am using shiftplans, i have that as an xmllist so i have like a ShiftPlansXLC as the
                          dataprovider for my data grid.

                          - where in your code do you actually put these expressions?
                          this function is called just after wherever you have the event handler for processing the xml request. In my case i am sending an http request which brings the response back as xml, i have the shiftTypeXLC bound to the as follows

                          <mx:HTTPService id="svcShiftTypesList" url="/shifttypes.xml" resultFormat="e4x" result="handleShiftTypesListResult(event)"/>
                          <mx:XMLListCollection id="ShiftTypesXLC" source="{XMLList(svcShiftTypesList.lastResult.children())}"/>


                          here is my function for handle ShiftTypesListResult
                          private function handleShiftTypesListResult(event:ResultEvent):void
                          {
                          _gotShiftTypes = true;
                          var resultXML: XML = XML(event.result);
                          hrnet.debug("ShiftTypesListBox#ShiftTypesListResult:\n" +resultXML.toString());
                          updateShiftTypeIdMap();
                          }

                          The updateShiftTypeIDMap() is where the code i sent you earlier with the object mapping was placed. what I have also done is added couple of flags like the _gotShiftType in the above function.
                          In updateshifttypeIdMap, when i have created the object i call a listShiftPlans function. In the listshiftplans function i check if the _gotShiftType flag is true ie. i have got the shifttype xml, if so i can call the code that deals with the datagrid.


                          regarding the error you were getting, try creating an xml object outside the function that stores the xml in the datatbl3. try creating an xmlListCollection and see if its working fine as well. I have only created xmlListCollection like above so I am not sure how to deal with static xml data.

                          hope that answers some of your questions.

                          cheers
                          abhi
                          • 10. Re: Using different dataProvider in the same DataGrid
                            Astrolive Level 1
                            It does! Thank you!

                            Olivia