Announcement

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

    About sorting FilterBuilder's drop-down values list

    I have a FilterBuilder showing a SelectItem for a certain field, but I've not yet found a clean way to sort the SelectItem values.

    Let me explain:
    My FilterBuilder uses a datasource defined as follows
    Code:
    ...
    final DataSourceIntegerField optionField = new DataSourceIntegerField ("option", "OPTION");
    //NEEDED to let the filter builder show a drop-down list
    final SelectItem optionsItem = new SelectItem ();
    optionsItem.setName (optionField.getName ());
    optionsItem.setOptionDataSource (getOptionDataSource ());
    optionsItem.setValueField ("id");
    optionsItem.setDisplayField ("name");
    //USELESS
    optionsItem.setSortField ("name");
    optionField.setEditorType (optionsItem);
    
    mainDataSource.addField (optionField);
    ...
    as depicted in http://forums.smartclient.com/showthread.php?t=13775.
    If I use the SelectItem in a form I can simply call setSortField on it and it works. But calling this method on a SelectItem used as editorType on the field seems not enough...

    Moreover I noticed that if I use the form then the FilterBuilder works fine!?!
    Could you please give me some explanations?

    I'm attaching a simple test case.

    At client side I'm using
    SmartClient Version: SC_SNAPSHOT-2010-11-03/LGPL Development Only (built 2010-11-03)
    GWT 2.0.4
    Firefox 3.6.12


    Kind regards
    Davide

    UPDATE: Obviously, adding a DynamicForm with the SelectItem is not a viable workaround (besides being an ugly solution) since if the form is not visible the FilterBuilder drop-down remains unsorted...
    Attached Files
    Last edited by d.cavestro; 5 Nov 2010, 05:31.

    #2
    You can use a simple solution like this:
    Code:
    public class SortedDatasource extends DataSource{
    		
    	public SortedDatasource(DataSource parent) {
    		DataSourceField[] unsortedFields = parent.getFields();
    		List<DataSourceField> sortedFields = Arrays.asList(unsortedFields);
    		Collections.sort(sortedFields, new Comparator<DataSourceField>() {
    			@Override
    			public int compare(DataSourceField dsf1, DataSourceField dsf2) {
    				String dsf1Title = dsf1.getTitle();
    				String dsf2Title = dsf2.getTitle();
    				
    				return dsf1Title.compareTo(dsf2Title);
    			}
    		});
    		for(DataSourceField dsf:sortedFields){
    			addField(dsf);
    		}
    	}
    }
    and use new SortedDatasource(delegate) on the filterbuilder.

    Comment

    Working...
    X