Announcement

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

    sorting valueMap in SelectItem

    Hello,

    I have a combo box with a map of values (every entry in map consists of id, label).
    Can I tell SelectItem to sort the map?
    (The SelectItem isn't related to data source)

    Code:
            SelectItem formItem = new SelectItem();
            LinkedHashMap<String,String> values = data.getValues();
            formItem.setValueMap(values);

    thanks

    #2
    la123,

    My understanding is that they offer the LinkedHashMap argument so that you can sort your map prior to sending it to the SelectItem. Perhaps they should have just exposed Map instead, and noted in the documentation that the ordering would be based on the Map's implementation of keySet(). That way you could send in a SortedMap and have the map worry about sorting, so you don't have to.

    Perhaps someone can offer up a reason behind LinkedHashMap as opposed to just Map.

    The javadocs on setValueMap don't seem to provide any indication of this decision.

    Comment


      #3
      thanks tdoty

      I added sorting on server side. Here is one of the options:
      Code:
          private static LinkedHashMap<String, String> getSortedByLabelsMap() {
              SortedMap <String, String> map = new TreeMap <String, String>(new Comparator <String>() {
                  public int compare(String word1, String word2) {
                      //Sort string in an alphabetical order
                      return word1.compareToIgnoreCase(word2);
                  }
              });
      
                  map.put('test1', 'label1');
                  map.put('test2', 'label6');
                  map.put('test3', 'label4');
      
              return new LinkedHashMap<String, String>(map);
          }

      Comment


        #4
        Originally posted by tdoty
        la123,

        My understanding is that they offer the LinkedHashMap argument so that you can sort your map prior to sending it to the SelectItem. Perhaps they should have just exposed Map instead, and noted in the documentation that the ordering would be based on the Map's implementation of keySet(). That way you could send in a SortedMap and have the map worry about sorting, so you don't have to.

        Perhaps someone can offer up a reason behind LinkedHashMap as opposed to just Map.

        The javadocs on setValueMap don't seem to provide any indication of this decision.
        LinkedHashMap preserves the order in which the user adds entries to the Map and they appear in the same order in a SelectItem. If the API supported a Map type instead, if a user passes an instance of HashMap instead the display order in the SelectItem would not correspond the the order in which the user put entries into the Map as the iteration will be based on the hash value of the key and this is never desirable.

        Technically the API should also support SortedMap since the iteration order is deterministic but the solution you posted is fine. I'll look into adding FormItem.setValueMap(SortedMap).

        Sanjiv

        Comment


          #5
          Originally posted by sjivan
          LinkedHashMap preserves the order in which the user adds entries to the Map and they appear in the same order in a SelectItem. If the API supported a Map type instead, if a user passes an instance of HashMap instead the display order in the SelectItem would not correspond the the order in which the user put entries into the Map as the iteration will be based on the hash value of the key and this is never desirable.

          Technically the API should also support SortedMap since the iteration order is deterministic but the solution you posted is fine. I'll look into adding FormItem.setValueMap(SortedMap).

          Sanjiv
          Right. I only suggested Map so that there would be one method, as opposed to one for each Map implementation that has a deterministic sort order. If the javadocs simply stated that the ordering is based on the Map's keySort() order, then users could determine what implementation they would send. If they simply didn't care about order, a HashMap would still be a valid option.

          Anyhow, thanks for considering adding the SortedMap method. I believe that will be beneficial for many use cases.

          Comment


            #6
            Nice! I'm very curious to this if this fixes the sorting problem on SelectItems in Chrome as well.

            Comment


              #7
              The setValueMap method for select item accept LinkedHashMap as a parameter. this work perfectly fine on firefox,the order is maintained but does not work in crome. It looks like in crome the LinkedHashMap is again sorted by numeric keys. Please let me know if this issue is fixed or any workaround for this.

              I am using smartgwt 2.4.

              Sandip Trivedi

              Comment


                #8
                I just noticed the same: sorting looks good on FF but not on IE.

                IE: 9.0.8112
                FF: 5.0
                Mode: DevMode
                SmartGWT: 2.5
                GWT: 2.3.0

                Could not test on Chrome because SelectItem still does not show the pick list.

                Will work on a reproducible test case later...

                Comment


                  #9
                  I also notices same this happen on chrome .

                  Actually Chrome and IE have same behavior on this issue
                  while Mozilla FF and Safari have same behavior .

                  I observed that IE and Chrome have its default functionality to sort a valuemap according to id while FF and safari have no such default implimentation that why we can make them customize for our expected sorting by value behavior . But our this customozation didn't work on IE and Chrome .

                  I use isc.sortObjectByProperties (cpValueMap) to sort our valuemap . alternativly i can also use a custom function for the same functionality :

                  function sortValueMap (map) {
                  var mapValues = isc.getValues(map);
                  mapValues.sort();
                  var newMap = {};
                  for (var i = 0; i < mapValues.length; i++) {
                  newMap[isc.getKeyForValue(mapValues[i],map)] = mapValues[i];

                  }
                  return newMap;
                  }

                  Comment


                    #10
                    @smartankur does that actually work to fool Chrome?
                    I find that doing whatever to manually sort a map on my terms before item.setValueMap(map) works, but after this call, it still appears in another order when clicking the dropdown in the UI.

                    The only way I could get it working is to transform the IDs in the Key to a String by using some prefix.
                    But this has a big impact when you're relying on the Key being a numeric database ID.

                    Comment


                      #11
                      Originally posted by sjivan View Post
                      ...
                      Technically the API should also support SortedMap since the iteration order is deterministic but the solution you posted is fine. I'll look into adding FormItem.setValueMap(SortedMap).

                      Sanjiv
                      Will this happen in the near future?
                      I have a form with N SelectItems, where the user can move entries from one SelectItem to another. The user expects the SelectItems to be ordered.
                      Currently I sort the maps after moving an entry using
                      Code:
                      lhmX = new LinkedHashMap<..., ...>(new TreeMap<..., ...>(lhmX));
                      siX.setValueMap(lhmX);
                      (siX stands for the SelectItems and lhmX for the maps).

                      Is there a better (faster) way?
                      Last edited by AloneAgainOr; 16 Aug 2012, 05:01. Reason: typo

                      Comment


                        #12
                        Multi-value SelectItem with client-only DataSource and sorting &quot;on the fly&quot;&quot;

                        Hey guys, i lately had the requirement for creating a SelectItem with a dropdown grid (without a header) where the user additionally has the opportunity to change the sort order of the SelectItems' values at runtime. This extra behaviour is required to not only get the selected values but also their relative positioning. After playing several hours around, trying to affect the sort order via item.setPickListCriteria(DSRequest), I gave up allthough an older post http://forums.smartclient.com/showthread.php?t=6907 sounded promissing. Maybe someone can tell me a better way.

                        Finally I decided to write a SelectItem whose sort order can be set to a combination of the following two options (log-in to see In-line Screenshots):

                        [A] Values can be moved Up/Down manually (y/n)
                        [B] Selected values appear always before deselected values (y/n)

                        - ![A] and [!B] results in the "usual" SelectItem
                        - [A] and [!B] is what I am familar and happy with (configurable 2 up/down icons next to each item):
                        ->
                        - [!A] and [B] is ok as well, but a bit exiting, since a value select/deselect might cause reordering:
                        -->
                        - [A] and [B]...well it works ;-)


                        Use it like this...
                        Code:
                        SortablePickListItem item= new SortablePickListItem(allowManualSorting, selectedItemsBeforeDeselected, hideFirstUpAndLastDownArrows, "up.png", "down.png");
                        item.setTitle("Normalize");
                        //item.setShowValueIconOnly(true);
                        //tem.setWidth(50);
                        item.setEndRow(true);
                        
                        List<SortablePickListRecord> normalizers= new ArrayList<SortablePickListRecord>(2);
                        normalizers.add(item.new SortablePickListRecord("Trim", "valueForTrim", "tooltip", initially_selected));
                        ...
                        item.populateInitial(normalizers);
                        
                        myForm.setItems(..., item, ...);
                        Of course, it is intended to work only with small client-side datasources. I even would prefer a way to do this just with setValueMap, without DataSources. It looks a bit complicated but I found no other way for updating the client-only data source and refreshing the grids content. Note, that the Items ChangedHandler is blocked by the Grids RecordclickHandler, but that is ok for me. Tested on Ubuntu 12.04, Firefox 14.0.1 (amd64) with SmartClient Version: 8.2/LGPL Development Only (built 2011-12-05).

                        Maybe, someone can make use of it. Greetings
                        Attached Files
                        Last edited by weihnachtsmann; 10 Oct 2012, 07:49.

                        Comment


                          #13
                          I surely appreciate this... thanks!

                          Comment

                          Working...
                          X