Announcement

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

    ListGrid date fields with records that have date strings rather than date objects

    v12.0p_2020-02-02/LGPL Deployment

    We are only using the JS side of the SmartClient product with a .NET backend. Several of us have been working with SmartClient for years in the past, but we are at a new company now and trying to do things better this time around.

    The issue that I am looking for clarification on has to do with ListGrid and fields with a type of DateTime. The records that our server returns has date strings in the format of "2020-02-18T08:46:34". When we have a ListGrid with simple fields, let's just use one field defined as

    Code:
    { name: "theDate", type: "datetime", title: "some date" }
    along with an example record of
    Code:
    { theDate: "2020-02-18T08:46:34" }
    the ListGrid does not format that date properly - or better put, it does not convert it and display it as a date, it just thinks it is some string and thus shows it that way.

    Reading the documentation I see that the datetime fields in a ListGrid are expecting a date object rather than a string representation of that date, so that is fair... but we are looking to find a way to do that easily to all of our records that are returned and have date values on them.

    One thing we found in the SmartClient JS code is the isc.DataSource.recordsFromObjects function. This works for us, but I have personally always hesitated because it is not a documented function, and thus fear it could change at any point in time and users should not expect to get notified about it.

    My question is around the idea I have outlined. Is this undocumented function a safe bet for us to use for making our records work with the ListGrid component, or is there another approach that you guys would suggest?

    Any help is much appreciated. Thanks!

    #2
    If you look at the docs for RestDataSource, you are using the correct date format, so if the field is correctly declared as datetime, and your return format is proper, the value will be converted to a true Date automatically in the DataSource layer, so that the ListGrid gets the Date object as expected.

    Check for mismatched field names in the DS vs returned data, returned data not quite matching the format (extra layer of nesting, missing required parts, etc). If you think you're doing everything right, put together a test case and we can look.

    If you're not using RestDataSource, obviously we need to know what you're using..

    Comment


      #3
      Thanks for the quick response - and my apologies for the lack of mine.

      For this specific instance that we are working on, we are actually doing an RPCManager.sendRequest to get meta data about the fields and the data for the grid that we are populating dynamically. This is not normal usage, we usually stick to using data sources in the manner they were intended to be used.

      Here is the scenario: we select a data summary from a main grid, which in turn sends that RPCManager.sendRequest as mentioned before to get the fields for this data summary and the data that corresponds to it. We then create a set of fields in the JS that corresponds to the format required for list grids.

      In the callback of that sendRequest, we create a regular (clientOnly: true) isc.DataSource and set the fields to that data source. We then call setCacheData on the data source with the data that is provided along with the fields. I will show a screenshot below showing those fields logged to the console along with the data.

      Click image for larger version

Name:	smartclient-question-recordsFromObjects.PNG
Views:	134
Size:	10.2 KB
ID:	261230

      Here is the code:

      Code:
      console.log(fields);
      console.log(data.reportData);
      var dataSource = isc.DataSource.create({
          fields,
          clientOnly: true
      });
      
      //dataSource.recordsFromObjects(gridData);
      
      dataSource.setCacheData(gridData);
      listGrid.setDataSource(dataSource, fields);
      listGrid.fetchData();
      When we have the recordsFromComponents line commented out, the date field is not parsed into a date and thus displayed like a string. We remove that comment and it calls the recordsFromObjects function, the grid shows the date as it should be as a date object.

      Comment


        #4
        You didn't say how you're using sendRequest() but we'll assume you've set evalResult -in which case strings will remain strings as that's what eval does. Same with standard JSON parsing.

        The most efficient thing you could do is have your server write out a JavaScript expression creating a date (new Date(...)). That's what our server product does.

        Barring that, you need to do client-side parsing. recordsFromObjects() is in fact the method we use in our DataSource layer to do this parsing when a DataSource is using strict JSON (so can't use a "new Date()" expression), however, it's technically an undocumented API, so the official route would be to do the conversion yourself. But, this API has been around unchanged for more than 12 years, and is likely to become a publicly documented and supported API in the future, so it's pretty low-risk to use.

        Last note: you could also download just the DataSource information and then create a DataSource that does a request to retrieve the data, which would also avoid this issue, and would enable paging - your current approach will only work for modest data volumes.

        Comment

        Working...
        X