Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to re-order ListGridFields coming from the datasource

    Hi,

    Our datasources are generated server side. The order in which the datasource fields are in the datasource is just
    as we generate them (so no particular order).

    The fields in the listgrid should be displayed in a particular order for one window and as our
    datasources are re-used on multiple "windows", the order and which fields we show can be different.

    I tried re-ordering the fields via ListGrid.reorderField(fieldNum, moveToPosition);
    However, it's not working... probably because I'm moving all my ListGridFields in one go, is that correct?

    Is there another way to re-order the bound fields?

    Don't want to use setFields explicitly as I get from the javadoc that that's not a good idea when using setDataSources as well.
    It would be nice if you could set the actual field order on initialisation.

    Code:
                             //partiesFieldOrder is a static list of 10 field names which correspond to fields that are in the datasource
                             //datasource actually contains many more fields
    
    		ListGrid partiesGrid = new ListGrid();
    		partiesGrid.setWidth100();
    		partiesGrid.setDataSource(involvedparty);//generated datasource
    		partiesGrid.setSelectionType(SelectionStyle.SINGLE);
    		partiesGrid.setVisible(false);//hide for now...
    
    		//draw (grid is still hidden) but needed for the getAllFields (to apply the datasource)
    		partiesGrid.draw();
    
    		ListGridField[] gridFields = partiesGrid.getAllFields();
    		for(int i=0; i < gridFields.length; i++){
    			//set actual fields to show => otherwise everything is shown
    			partiesGrid.hideField(gridFields[i].getName());
    			for(int x=0; x < partiesFieldOrder.length; x++){
    				if(gridFields[i].getName().equalsIgnoreCase(partiesFieldOrder[x])){									
    					partiesGrid.showField(gridFields[i].getName());
    					partiesGrid.reorderField(i, x);//this isn't working
    					continue;
    				}
    			}			
    		}
    		
    		partiesGrid.markForRedraw();
    		partiesGrid.show();//now show the grid (already drawn)
    Originally posted by javadoc
    void com.smartgwt.client.widgets.grid.ListGrid.setFields(ListGridField... fields)
    An array of field objects, specifying the order, layout, formatting, and sorting behavior of each field in the listGrid object. In ListGrids, the fields array specifies columns. Each field in the fields array is a ListGridField object. Any listGrid that will display data should have at least one visible field.

    If dataSource is also set, this value acts as a set of overrides as explained in fields. Sets the fields array and/or field widths to newFields and sizes, respectively.

    If newFields is specified, it is assumed that the new fields may have nothing in common with the old fields, and the component is substantially rebuilt. Consider the following methods for more efficient, more incremental changes: resizeField, reorderField, showField, hideField, setFieldProperty.
    Last edited by bade; 28 Mar 2011, 23:47.

    #2
    Not sure why you say that using setFields() with setDataSource() is bad. In the docs you quoted and in the QuickStart Data Binding chapter, it is explained that reordering and limiting the fields for a specific DataBound component is exactly what the combination of setFields() and setDataSource() is intended for.

    Comment


      #3
      I'm interested to hear what your suggestion is... First 'clone' the list grid fields (somehow)?

      I used to do the following code, which obviously is no good as it overwrites the original fields.
      The effect I was seeing is that the datasource field properties were "erased".
      So ListGridField.getType() would return null instead of the type as defined in the datasource xml.

      Code:
      		ListGridField[] gridFields = new ListGridField[partiesFieldOrder.length];
      		for (int i=0; i<gridFields.length; i++) {														
      			gridFields[i] = new ListGridField();
      			gridFields[i].setName(partiesFieldOrder[i]);
      			gridFields[i].setSortNormalizer(new SilkSortNormalizer());
      		}						
      		partiesGrid.setFields(gridFields);
      Last edited by bade; 29 Mar 2011, 05:05.

      Comment


        #4
        Looks like you need to provide a complete example as setting the fields (just the name) and providing a data source is exactly how you configure a grid with a different column order. This works everyday and data source properties are merged in with any overrides provided with the fields. See the merged fields sample.

        Comment


          #5
          Hi David,

          Ok, got it to work... don't know where I messed up.
          I'm guessing it had to do with setting the fields AFTER calling draw().

          ==== Update

          I'm still curious to know if "reorderField" works with more then one at a time?

          Thanx for the help, sorry for the hassle.
          Bart
          Last edited by bade; 29 Mar 2011, 05:57.

          Comment

          Working...
          X