Announcement

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

    Sorting ListGridField Dropbox not case-insensitive

    Hello,
    I am trying to sort a dropbox for a ListGridField in alphabetical order. The code below sorts by the ascii value, which makes all lower case letters come after uppercase letters. Is there anyway to make the sort case-insensitive?
    Example sort:
    What I'm getting now: ("AJ", "Ab", "Be", "Gi", "Zh", "bo")
    What I need: ("Ab", "AJ", "Be", "bo", "Gi", "Zh")

    Code:
    DataSource aDS = DataSource.get("aDatasource");
    SortSpecifier[] sortSpecifiers = {new SortSpecifier("name", SortDirection.ASCENDING)};
    DSRequest dsr = new DSRequest();
    dsr.setSortBy(sortSpecifiers);
    
    ListGridField user= new ListGridField("user", "Created by");
    user.setOptionDataSource(aDS);
    user.setOptionFilterContext(dsr);
    user.setValueField("userName");
    user.setDisplayField("name");
    SmartClient Version: v8.3p_2013-04-15/Enterprise Deployment (built 2013-04-15)
    Firefox 20.0.1
    Last edited by SNA2; 3 Jan 2014, 09:17.

    #2
    Add a SortNormalizer that just upper- or lower-cases record[fieldName] and returns it

    Comment


      #3
      I'm not really familiar with how to use the SortNormalizer, but I tried the following code under the code I posted.
      Code:
      user.setSortNormalizer(new SortNormalizer() {
                public Object normalize(ListGridRecord record, String fieldName) {
                     return record.getAttributeAsString("name").toUpperCase();
                }
      });
      Is this the correct way of writing it?
      The dropbox was still not correctly sorted.

      Comment


        #4
        Set the sortNormalizer on the sortSpecifier you're creating - and, of course, use the passed fieldName, rather than "name"

        Comment


          #5
          The sortSpecifer does not have a setSortNormalizer method. What do you mean by "set the sortNormalizer on the sortSpecifier"?

          Comment


            #6
            Ah yes, SortSpecifier.setNormalizer() isn't publicly available in SmartGWT.

            So you *will* have to set the normalizer on a ListGridField - but not the one in your code, because you want to sort the field's picklist, not the field itself.

            So, you'll need to set it on the ListGridField in the picker that's displayed by this ListGridField's editor. To do that, you'll want something like this:

            Code:
            SelectItem editorProps = new SelectItem();
            // create the "name" field for the pickList grid, and assign
            // the sortNormalizer to it - then assign it to the SelectItem's picklist
            editorProps.setPickListFields(...);
            // and assign the SelectItem as the editor for the main ListGridField
            listGridField.setEditorProperties(editorProps);

            Comment


              #7
              Thanks this solution works, except
              ListGridField does not have a method called setEditorProperties. Instead I used listGridField.setFilterEditorType(editorProps).

              Comment


                #8
                Yes, 3.1 didn't have that API.

                Comment


                  #9
                  I am using v9.1p_2015-09-11/PowerEdition and came across this to solve a similar need. I was able to get it working, but I also had to call setSortField() on the editor properties object. Otherwise, the SortNormalizer would be ignored.

                  My requirement was to sort on a store number field that is text and may have leading 0s. I had to pad the num attribute to 10 characters using leading 0s before sorting as the data isn't guaranteed to have leading 0s.

                  Code:
                  ListGridField field = new ListGridField("store", 100);
                  field.setOptionDataSource(DataSource.get("locStoreListDS"));
                  field.setValueField("id");
                  field.setDisplayField("num");
                  
                  ComboBoxItem properties = new ComboBoxItem();
                  ListGridField dispField = new ListGridField("num");
                          dispField.setSortNormalizer(new SortNormalizer() {
                              @Override
                              public Object normalize(ListGridRecord record, String fieldName) {
                                  String value = record.getAttribute(fieldName);
                                  return StringUtil.padStart(value, 10, '0');
                              }
                          });
                  properties.setPickListFields(dispField);
                  properties.setSortField("num");
                  
                  field.setEditorProperties(properties);
                  field.setFilterEditorProperties(properties);

                  Comment

                  Working...
                  X