Select to view content in your preferred language

dynamically change the fields shown in a datagrid

617
2
Jump to solution
02-01-2012 12:55 PM
TracySchloss
Honored Contributor
I have a map services with many fields.  There are logical names to the fields like "Pasture_2002_Acres", Pasture_2007_Acres", "Crops_2002_Acres", "Crops_2007_Acres"

I created an array of fields that I'd like to have show up in a datagrid.  Based on whether the user wants to see Pasture information vs Crop information, the fields you'd want to see would be different.

I've run a querytask that generates a graphics layer and it contains all the attributes.  If the user wants to see just Pasture information, I made an array of just the fields related to that.  These become the fieldnames for a newly creaed datagrid.  The year is set from a radio button.  The results is an ArrayCollection I populated during the OnResults function of the querytask and it does have the right data in it.

   var fieldNameArray:Array = ["COUNTY","SQMILES",year+"_PASTURE_ACRES",year+"_PASTURE_SQMILES_TREATED",year+"_ PCTAREA_PASTURE"]; var selectDGcolumns:Array = [];    for each (var fieldName:String in fieldNameArray) {     var dgColumn:DataGridColumn = new DataGridColumn;     dgColumn.dataField = fieldName;     trace (dgColumn.dataField);     dgColumn.setStyle("textAlign","right");     selectDGcolumns.push(dgColumn);     }    dg.columns = selectDGcolumns;    dg.dataProvider = results;


This works just great the first time around. I have an "Open Table" button that opens a titleWindow that displays the datagrid.   The grid populates with the fields and values I expect.  The problem comes in when I want to change to a different subject.

The field array traces just fine (shows the change in the year).But when I go back through the 'for each' loop, the columns in the datagrid continue to be the ones I had chosen from the first time I used the tool.  I think I have emptied the array out by maybe I'm not understanding how to empty out all my arrays etc properly.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
TracySchloss
Honored Contributor
There is no 'removeAll' option on the data provider.  The dg.columns = [];  sounded promising.  But no go!

I have the datagrid as a separate custom component (it has listeners etc back to the graphicslayer) and the titlewindow is also custom.

I'm making a new datagrid each time

var dg:Fields_DataGrid = new Fields_DataGrid;


but the titlewindow was a variable set at the top.  This turned out to be the problem, I needed to create a new one.  (You'd think I would have seen a bunch of datagrid all getting added to the same titlewindow, but it didn't work out that way!)

Thanks for your assistance, I need an extra pair of eyes, and I don't have anyone around me to ask!

View solution in original post

0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus
Tracy,

   Try this:

            dg.dataProvider.removeAll();
            dg.columns = [];
            var fieldNameArray:Array = ["COUNTY","SQMILES",year+"_PASTURE_ACRES",year+"_PASTURE_SQMILES_TREATED",year+"_ PCTAREA_PASTURE"];
            var selectDGcolumns:Array = [];
            for each (var fieldName:String in fieldNameArray) {
                var dgColumn:DataGridColumn = new DataGridColumn;
                dgColumn.dataField = fieldName;
                trace (dgColumn.dataField);
                dgColumn.setStyle("textAlign","right");
                selectDGcolumns.push(dgColumn);    
            }
            dg.columns = selectDGcolumns;
            dg.dataProvider = results;
0 Kudos
TracySchloss
Honored Contributor
There is no 'removeAll' option on the data provider.  The dg.columns = [];  sounded promising.  But no go!

I have the datagrid as a separate custom component (it has listeners etc back to the graphicslayer) and the titlewindow is also custom.

I'm making a new datagrid each time

var dg:Fields_DataGrid = new Fields_DataGrid;


but the titlewindow was a variable set at the top.  This turned out to be the problem, I needed to create a new one.  (You'd think I would have seen a bunch of datagrid all getting added to the same titlewindow, but it didn't work out that way!)

Thanks for your assistance, I need an extra pair of eyes, and I don't have anyone around me to ask!
0 Kudos