1 Reply Latest reply on Aug 24, 2006 7:29 AM by aBill

    how to use mx:DataGrid if column list is driven by  variable

    mounte
      Hi
      I need to show the data from a table. i can't hard code the column names because they are returned by a remoteObject as an ArrayList(includes column name and data type). then i got another dataList as an ArrayList of HashMa(a row of data. key as column name, value as data).

      how do i use mx:DataGrid to handle this? help!!

      thanks


        • 1. Re: how to use mx:DataGrid if column list is driven by  variable
          aBill
          Hi
          hope the following example help
          <?xml version="1.0"?>
          <Application xmlns=" http://www.adobe.com/2006/mxml">
          <DataGrid id="dgrid"/>

          <Script>
          <![CDATA[
          import mx.controls.dataGridClasses.*;


          private function createCols():void
          {
          var array:Array = new Array();
          var dc1:DataGridColumn = new DataGridColumn("a");
          array.push(dc1);
          dc1 = new DataGridColumn("b");
          array.push(dc1);
          dgrid.columns = array;
          }
          private function colC():void
          {
          var array:Array = new Array();
          var dc1:DataGridColumn = new DataGridColumn("c");
          array.push(dc1);
          dgrid.columns = array;
          }

          private function provideData():void
          {
          var data:Array = [{a:"a1",b:"b1",c:"c1"},{a:"a2",b:"b2",c:"c2"},{a:"a3",b:"b3",c:"c3"}];
          dgrid.dataProvider = data;
          }

          private function provideData2():void
          {
          var data:Array = [{a:"a11",d:"d1"},{a:"a22",d:"d2"}];
          dgrid.dataProvider = data;
          }

          private function removeData(): void
          {
          dgrid.columns = [];
          }
          ]]>
          </Script>
          <Button label="column a b" click="createCols()"/>
          <Button label="column c" click="colC()"/>
          <Button label="data" click="provideData()"/>
          <Button label="data2" click="provideData2()"/>
          <Button label="clear" click="removeData()"/>
          </Application>

          Thanks
          William Chan