Announcement

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

    How to set the SelectItem selected value count instead of the values themselves

    Hi Isomorphic. I am implementing a sub class of SelectItem and I want to set the selected item count as the SelectItem value that's visible on the collapsed control (see prototype image below), instead of the default list of values that is usually shown (and which in this case will almost always be clipped because of the amount of items that can be selected at any given time):
    Click image for larger version

Name:	BufferSelectorProto.png
Views:	194
Size:	2.7 KB
ID:	238609


    Some background: I am writing a simulator and this SelectItem allows to select the elements that will be included in one analysis, which could be just a few, many or even all of them (depending on the scope defined by the user).

    So the specific question is if there's a way to get this behavior "out of the box", other than using a trigger that fires every time the SelectItem loses focus, and then use SelectItem.setValue("n buffers selected") to set the value? Is there a method that I can override to get this summary message instead of the whole (clipped) value list? Or the trigger + setValue method is the correct approach here?

    Thanks

    ps. I am using SmartGWT 6.0-p20160531.
    Attached Files
    Last edited by carlossierra; 14 Jun 2016, 08:52. Reason: Added clarity

    #2
    The approach you suggested doesn't make sense since calling setValue() would destroy the list of selected items (which is the value of the item).

    Instead use a formatter as shown in this sample.

    Comment


      #3
      Thanks Isomorphic for your answer, but I haven't been able to get that method in the sample to work, because this will give me a formatted value per each selected item, hence getting an undesired result.

      This is my FormItemValueFormatter code:

      Code:
      setValueFormatter(new FormItemValueFormatter() {  
          public String formatValue(Object value, Record record, DynamicForm form, FormItem item) {  
              return BufferPickList.this.getSelectedRecords().length + " buffers selected";
          }  
      });
      And this is the result after some values have been selected: Click image for larger version

Name:	BufferSelectorWithFormatter.png
Views:	243
Size:	3.6 KB
ID:	238617
      What am I missing here?

      Thanks!
      Last edited by carlossierra; 14 Jun 2016, 12:01. Reason: improve clarity

      Comment


        #4
        There's no simple way to get exactly what you originally asked for, however, you can get something better. Just add "3 buffers selected: " to the first selected value only, and return the other values unchanged. Then you'll have something like:

        3 buffers selected: buffer 1, buffer 8, buffer 5

        This conveys more information and makes the full list of values visible to the user if they hover.

        Comment


          #5
          Thanks Isomorphic. Indeed, that sounds better. This is what I came up with, but it still won't render the desired result:

          Code:
          private class BufferPickListFormatter implements FormItemValueFormatter {
              
              public String formatValue(Object value, Record record, DynamicForm form, FormItem item) {  
                  
                  ListGridRecord[] selectedRecords = BufferPickList.this.getSelectedRecords();
                  if (selectedRecords != null) {
                      int valueCount = selectedRecords.length;
                      String bufferName = getFullBufferName(item);
                      if (valueCount == 1 || isFirstSelectedValue(item, selectedRecords)) {
                          return valueCount + " selected: " + bufferName;
                      } else {
                          return bufferName;
                      }
                  }            
                  return "";
              }
              
          [B]   private boolean isFirstSelectedValue(FormItem item, ListGridRecord[] selectedRecords) {
                  return item.getSelectedRecord().equals(selectedRecords[0]);
              }[/B]
              
              private String getFullBufferName(FormItem item) {
                  ListGridRecord r = item.getSelectedRecord();
                  return r.getAttribute("sku") + " @ " + r.getAttribute("location");
              }
          }
          Question: how can I tell apart which is the first selected value? The code marked in bold above is my attempt at getting this, but it doesn't work...

          The result is still the same "repeating" behavior shown above, but now with the "N selected" text repeating before each value :(

          Thanks

          Comment


            #6
            Get the current value of the item and compare the value you are passed to the first value.

            You seem to be trying to compare by records - that won't work for various reasons, including record identity not necessarily being the same, and records not necessarily having been fetched when formatters are called.

            Comment


              #7
              Hi Isomorphic. After several attempts, I couldn't get it to work using the item passed to FormItemValueFormatter.formatValue(), since it always returned the value of the first element selected in the SelectItem. Anyway, I found an alternative way to do it using the value passed to this method, and getting a reference of the SelectItem.getSelectedRecords. Here is my working code, in case anyone is interested:

              Code:
              private class BufferPickListFormatter implements FormItemValueFormatter {
                  
                  public String formatValue(Object currentValue, Record record, DynamicForm form, FormItem item) {  
                      ListGridRecord[] selectedRecords = BufferPickList.this.getSelectedRecords();
                      if (selectedRecords != null) { 
                          int valueCount = selectedRecords.length;
                          String bufferName = getFullBufferName(currentValue, selectedRecords);
                          if (valueCount == 1 || isFirstSelectedValue(currentValue, selectedRecords)) {
                              return messages.runParametersForm_buffersSelected(valueCount, bufferName);
                          } else {
                              return bufferName;
                          }
                      }            
                      return "";
                  }
                  
                  private boolean isFirstSelectedValue(Object currentValue, ListGridRecord[] selectedRecords) {
                      Record firstSelectedRecord = selectedRecords[0];
                      String firstSelectedValue = firstSelectedRecord.getAttribute(VALUE_FIELD);
                      return currentValue.toString().equals(firstSelectedValue);
                  }
                  
                  private String getFullBufferName(Object currentValue, ListGridRecord[] selectedRecords) {
                      for (ListGridRecord record: selectedRecords) {
                          String recordValue = record.getAttribute(VALUE_FIELD);
                          if (currentValue.toString().equals(recordValue)) {
                              return record.getAttribute(PICKLIST_FIELD1) + " @ " + record.getAttribute(PICKLIST_FIELD2);
                          }
                      }
                      return "ID not found";
                  }
              }
              A couple of notes:
              • For this to work I needed to use setValueField(VALUE_FIELD) and setDisplayField(VALUE_FIELD) on the SelectItem, in order for the proper underlying value to be stored and later retrieved by the functionality using/querying the SelectItem, but also for the valueFormatter to be able to do its job, because that way the value passed to it in the value Object parameter is a unique value (other fields used to populate the PickList can have repeat values - think for instance in first or last names, that can be common to several people).
              • I did get a reference to the currently selected records by referencing the outer class BufferPickList, which is my SelectItem subclass. This way I can iterate over the different selected PickList records, and query their attributes to produce the proper display value.
              The outcome is this:
              Click image for larger version

Name:	BufferSelectorWithFormatterWorking.png
Views:	223
Size:	12.8 KB
ID:	238663

              Comment


                #8
                Isomorphic, one minor issue I just noticed: the separator (setMultipleValueSeparator) used for the selected values is not being honored when the tooltip is shown (still uses the comma, instead of the pipe I specified for this control).

                Thanks

                Comment


                  #9
                  The issue related to the separator (setMultipleValueSeparator) has been solved. Please try the next nightly build, dated June 22.

                  However, we were not able to reproduce the problem where the formatter is always called for the first value. If you can provide more details on how to reproduce it, we will take another look.

                  Regards
                  Isomorphic Software

                  Comment


                    #10
                    Hi Isomorphic,

                    I can confirm that the issue with the custom value separator has been fixed in 6.0p 2016-06-22. Thanks!

                    Regarding the formatter problem, here is the code that didn't produce the desired effect for me:
                    Code:
                    private class BufferPickListFormatter implements FormItemValueFormatter {
                        
                        public String formatValue(Object value, Record record, DynamicForm form, FormItem item) {  
                            
                            ListGridRecord[] selectedRecords = BufferPickList.this.getSelectedRecords();
                            if (selectedRecords != null) { 
                                int valueCount = selectedRecords.length;
                                [B]String bufferName = getFullBufferName(item);[/B]
                                if (valueCount == 1 || isFirstSelectedValue(value, selectedRecords)) {
                                    return valueCount + " selected: " + bufferName;
                                } else {
                                    return bufferName;
                                }
                            }            
                            return "";
                        }
                        
                        private boolean isFirstSelectedValue(Object currentValue, ListGridRecord[] selectedRecords) {
                            Record firstSelectedRecord = selectedRecords[0];
                            String firstSelectedValue = firstSelectedRecord.getAttribute(VALUE_FIELD);
                            return currentValue.toString().equals(firstSelectedValue);
                        }
                        
                    [B]    private String getFullBufferName(FormItem item) {
                            ListGridRecord recordBeingFormatted = item.getSelectedRecord();
                            return recordBeingFormatted.getAttribute(PICKLIST_FIELD1) 
                                    + " @ " + recordBeingFormatted.getAttribute(PICKLIST_FIELD2);
                        }[/B]
                    }
                    Thanks again...

                    Comment


                      #11
                      Right, your code is broken because you just call getSelectedRecord() every time instead of using the value parameter passed into formatValue(). getSelectedRecord() just returns the first selected record if multiple are selected.

                      Comment


                        #12
                        Thanks Isomorphic. That's clear. But I have one more question: how can I get the attributes I need for each selected record in the PickList if not by using getSelectedRecord? The value parameter works as expected, but I won't allow me to access the extra information I use for display purposes to construct the desired value representation....

                        Comment


                          #13
                          See docs for getValue() - it returns a List.

                          Comment


                            #14
                            Thanks Isomorphic.

                            Comment

                            Working...
                            X