Announcement

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

    ListGrid Date Sorting - RESTful Datasource

    Hello Support,

    I am trying to recognize a date field properly within a ListGrid so that my users can sort against it. The data is delivered in XML via a RESTful data source. My first step was to just get the date rendering within the ListGrid and that is working fine. Now I am trying to recognize the dsGSADateModifiedField (see below) as a Date object and get it to sort properly within the ListGrid.

    I have seen other forum questions about this topic, but I could not identify a fix to my specific use case. Could you share some advice?

    Details Below:

    SmartClient Version: SC_SNAPSHOT-2011-08-02/PowerEdition Deployment (built 2011-08-02)

    XML Data Source Sample:
    Code:
    <record>
     <id>2</id>
     <gsa_date_modified>20-Nov-10</gsa_date_modified>
     ...
    </record>
    Code Snippets
    Code:
        // Placed within onModuleLoad method.
    
        DateUtil.setDateParser(new DateParser()
        {
            public Date parse(String dateString)
            {
                final DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("dd-MMM-yy");
                return dateTimeFormat.parse(dateString);
            }
        });
    
        // Field definition for data source.
    
        DataSourceDateField dsGSADateModifiedField = new DataSourceDateField("gsa_date_modified", "GSA Date Modified");
    
        // Field definition for ListGrid
    
        ListGridField lgLastModifiedDateField = new ListGridField("gsa_date_modified", "Date", 75);
        lgLastModifiedDateField.setType(ListGridFieldType.DATE);
    The dates are displayed within the ListGrid, but they do not sort properly. They appear to be recognized as Text fields.

    Thanks for reviewing this.

    #2
    The problem is in your DataSource - the field is not declared as type "date" / "datetime" or the XML data does not have the date / datetime format (XML Schema format is expected, as explained in the docs).

    Comment


      #3
      In the code snippet provided above, I defined the field for the restful data source as follows:

      Code:
      // Field definition for data source.
      
      DataSourceDateField dsGSADateModifiedField = new DataSourceDateField("gsa_date_modified", "GSA Date Modified");
      What more does the RestDataSource need to identify the field properly? Also, can you tell which document you are recommending that I re-read?

      Thanks.

      Comment


        #4
        Sorry, missed that line. The DataSource definition seems correct then (although you're only showing one line of it) so likely the format is wrong. This is covered in several places:

        http://www.smartclient.com/smartgwtee/javadoc/com/smartgwt/client/types/FieldType.html#DATE
        http://www.smartclient.com/smartgwtee/javadoc/com/smartgwt/client/docs/DateFormatAndStorage.html
        http://www.smartclient.com/smartgwtee/javadoc/com/smartgwt/client/data/RestDataSource.html

        Comment


          #5
          It is working properly now - thank you. I had read the document below in a prior release and did not realize it had been update with more information.

          http://www.smartclient.com/smartgwtee/javadoc/com/smartgwt/client/data/RestDataSource.html

          For those interested, I fixed my issues by correcting my web service so that it formatted all dates as "yyyy-MM-dd". This got the cell to recognize the field as a date and correctly sort the column.

          Finally, I needed to just clean up the display format. To do that, I added the following logic to handle the formatting of the cell:

          Code:
                  lgLastModifiedDateField.setCellFormatter(new CellFormatter()
                  {
                      public String format(Object aValue, ListGridRecord aRecord, int aRowNum, int aColNum)
                      {
                          if (aValue instanceof Date)
                          {
                              Date dateValue = (Date) aValue;
                              return DateTimeFormat.getFormat("dd-MMM-yy").format(dateValue);
                          }
                          else
                              return GWTStrUtl.EMTPY;
                      }
                  });

          Comment


            #6
            A couple of further notes -

            1. if you declare a field as type "date" and get the format wrong a ton of warnings are logged

            2. you probably mean to apply that custom format system-wide, and if so, you can do so using APIs on DateUtil

            Comment


              #7
              OK - I want to get this right for myself and anyone reading the post later.

              Regarding:

              1 - Since I control what the server sends, I can ensure the dates are formatted properly. Also, I checked the log and there were no parsing exceptions during my tests.

              2 - If you look at my original post above you will see that I tried to use the DateUtil.setDateParser() method. Did I correctly implement it?

              Thanks for the follow-up, I do appreciate it.

              Comment


                #8
                1. they are not exceptions, they are logs

                2. yes, your original code DateUtil code is fine if you want to change the date that *end users* input dates, system-wide. This does not affect data transmission. And again, in lieu of CellFormatter, you probably want to set system-wide formatting logic via DateUtil.

                Comment

                Working...
                X