Announcement

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

    ListGrid: initial column width enough to display all contents

    Hi,

    I'm using a ListGrid to display some data, and so far have managed to customize it mostly how I want it. However, I've not been able to figure out how to set the initial column width to be enough to make all the data in that column visible.

    Say for instance I have 30 columns named col1, col2,... col30. Some columns are empty, some have the data "foobar", and some have the data "12345678901234567890". I ideally want the empty columns to be just wide enough to display just the column heading, the column with data "foobar" just wide enough to display "foobar", and the column with the long number just wide enough to fully display the number.

    The ListGridField.setWidth() method only takes "*", a percentage, or a number of pixels, so this doesn't look supported. Has anybody managed to do this?

    #2
    I've been digging a bit further into this, and found some code in the SmartClient JavaScript library that looks like it may do this. I got the smartgwt-1.0b2 tag out of the Subversion repository, and in the file ListGrid.js, line 18873, three is a function that looks like this:

    Code:
    //>	@method	listGrid.autoSizeColumn()	(A)
    //	    @group	sizing, positioning
    //
    //      Can only be called after draw()
    //
    //      Resize this column to the size it needs to be in order to accomodate it's contents.
    //<
    autoSizeColumn : function (columnNum) {
    	// determine the auto-size
        var columnWidth = this.body.getColumnAutoSize(columnNum);
    
    	// resize the field
        this.resizeField(columnNum, columnWidth);
    },
    So it looks like there is code there to do it, it just hasn't been exposed on the GWT side yet. I'll have a look to see if I can figure out how to do it...

    Comment


      #3
      Ok, have found one way to expose the autoSizeColumn() function:

      Code:
      import com.google.gwt.core.client.JavaScriptObject;
      import com.smartgwt.client.widgets.grid.ListGrid;
      
      public class EnhancedListGrid extends ListGrid
      {
          private JavaScriptObject jsGrid;
      
          @Override
          protected JavaScriptObject create()
          {
              jsGrid = super.create();
              return jsGrid;
          }
      
          public void autoSizeColumn(int col)
          {
              jsAutoSizeColumn(jsGrid, col);
          }
      
          private native void jsAutoSizeColumn(JavaScriptObject grid, int col)
              /*-{
                  grid.autoSizeColumn(col);
              }-*/;
      }
      However, this appears to be very slow, for my moderate size set of data I get a slow script warning. Looking into it it looks pretty CPU intensive to get the size of a column as it renders each column to be auto sized in an off screen canvas to get the width.

      Comment


        #4
        See ListGrid.setAutoFitData()

        Comment


          #5
          Hey, I've got the same problem. How does setAutoFitData() solve this? I want to set the initial column width based on it's content. setAutoFitData() works for the whole ListGrid choosing which columns are displayed, doesn't it?

          What I need (and, if I understand correctly, what zakalwe is trying to achieve) is a one time, on-data-load resizing of a single column, completely independant of the AutoFit mechanism (which I don't really want to touch, because I need it the way it is - there are several columns in the grid, not all of which are rendered.

          Any solution? Thanks in advance,

          Liosan

          Comment


            #6
            autoFitData() provides auto-fitting for all columns, so it provides what zakalwe was evidently looking for. But if you want a single column to autofit, the undocumented API that zakalwe found is a good approach for now (it won't be slow if it's only one column).

            Comment


              #7
              We're having a similar problem with a ListGrid bound to a WebService DataSource. Below is an excerpt from our code. No matter where we put the autoFitData() call, it does nothing. We've tried it immediately after the DataSource is set and where it is now (in the callback) - and neither work. The ListGrid always gets three columns exactly the same width as each other.
              Code:
                      XMLTools.loadWSDL("actual URL",
                                        new WSDLLoadCallback()
                      {
                          public void execute(WebService service)
                          {
                              if (service == null)
                              {
                                  // Error loading the WSDL.
                              }
                              else
                              {
                                  // WSDL loaded okay, so set its location.
                                  service.setLocation("actual URL");
              
                                  // Set up the namespaces.
                                  XmlNamespaces ns = new XmlNamespaces();
                                  ns.addNamespace("actual prefix",
                                                  "actual namespace");
              
                                  // Set up the data source.
                                  ds = new DataSource();
                                  ds.setServiceNamespace("actual URL");
                                  ds.setXmlNamespaces(ns);
                                  ds.setShowPrompt(false);
               
              					// Set up the data source's fields.
                                  DataSourceTextField nameField = new DataSourceTextField("name", "Name");
                                  DataSourceIntegerField systemField = new DataSourceIntegerField("system", "System");
                                  systemField.setPrimaryKey(true);
                                  systemField.setRequired(true);
                                  DataSourceTextField commentField = new DataSourceTextField("comment", "Comment", 120);
              
                                  // Add the fields to the data source.
                                  ds.setFields(nameField,
                                               systemField,
                                               commentField);
              
                                  // Get the data from the data source.
                                  ListGrid.this.setDataSource(ds);
                                  ListGrid.this.fetchData(new Criteria(), new DSCallback()
                                  {
                                      public void execute(DSResponse response, Object rawData, DSRequest request)
                                      {
                                          ListGrid.this.setAutoFitData(Autofit.HORIZONTAL);
                                      }
                                  });
                              }
                          }
                      }, new RPCRequest(), true);

              Comment


                #8
                Budfudder, this thread is ancient. See ListGridField.autoFitWidth.

                Comment


                  #9
                  I realise that this is an old thread, but I'm having a similar problem to what they were, and what was said in the thread doesn't fix my problem.

                  And as far as I can see, there is no ListGridField.autoFitWidth (or ListGridField.setAutoFitWidth).

                  Comment


                    #10
                    Which build are you using? Builds since August have had this method.

                    Code:
                    /**
                     * Enables autofitting to values or titles for this field.. This overrides the {@link
                     * com.smartgwt.client.widgets.grid.ListGrid#getAutoFitFieldWidths autoFitFieldWidths} attribute on a per-field basis.
                     *
                     * @param autoFitWidth autoFitWidth Default value is null
                     */
                    public void setAutoFitWidth(Boolean autoFitWidth)
                    Sanjiv

                    Comment


                      #11
                      Originally posted by sjivan
                      Which build are you using? Builds since August have had this method.

                      Code:
                      /**
                       * Enables autofitting to values or titles for this field.. This overrides the {@link
                       * com.smartgwt.client.widgets.grid.ListGrid#getAutoFitFieldWidths autoFitFieldWidths} attribute on a per-field basis.
                       *
                       * @param autoFitWidth autoFitWidth Default value is null
                       */
                      public void setAutoFitWidth(Boolean autoFitWidth)
                      Sanjiv
                      Isomorphic was talking about a method of ListGridField. Aren't you talking about a method of ListGrid? In any case, I can't find an autoFitWidth (or a setAutoFitWidth) for either ListGridField or ListGrid in any documentation anywhere online. There's ListGrid.setAutoFitData - is that what you mean? If so, I've tried it, and it doesn't work either.

                      Comment


                        #12
                        You still haven't mentioned which build you're using. ListGridField. setAutoFitWidth(..) does exist in builds since August. You will not find it on the online javadocs since it was added post SmartGWT 2.2. Download the latest nightly from http://www.smartclient.com/builds/ or wait for the next official release.

                        Comment


                          #13
                          Originally posted by sjivan
                          You still haven't mentioned which build you're using. ListGridField. setAutoFitWidth(..) does exist in builds since August. You will not find it on the online javadocs since it was added post SmartGWT 2.2. Download the latest nightly from http://www.smartclient.com/builds/ or wait for the next official release.
                          Okay, I'll do that. Thanks for your help.

                          Comment

                          Working...
                          X