Announcement

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

    Submitting form updates vs ListGrid updates

    Hello,

    I've noticed that, when I update a record in a ListGrid, only the fields I explicity edit are added to the values submitted to the server. However, if I edit a ListGridRecord by calling editRecord() on a DynamicForm and passing in a ListGridRecord, then all values on the ListGridRecord are submitted to the server. Is there any way to avoid this discrepenacy so that all values are submitted when updating directly in the ListGrid as well?

    #2
    DSRequest.getOldValues()

    Hi Senordhuff,
    Both the ListGrid and the DynamicForm include a map of 'oldValues' along with 'newValues' when submitting data to the server for saving.
    This "oldValues" object is the set of values for the record before editing. You can retrieve it on the server via the method DSRequest.getOldValues(), which returns a Map of fields to values.
    Note: this is not present in the javadoc reference shipped with the SmartClient 5.5 release, but the method is present in the code and will be documented for future releases. From the JavaDoc:
    ======================================
    getOldValues
    public java.util.Map getOldValues()

    This is a convenience method - it is useful for when you know there can only be one set of oldValues. If your UI and your custom logic does not allow a user to add, update, or remove more than one record at a time, then this is a safe assumption.
    If you call this method on a DSRequest that contains multiple value sets, a warning is logged and the first value set in the list is returned by this method.

    See also getOldValueSets() for a description on the meaning of criteria by operationType.

    Returns:
    the oldValues for this DSRequest if there is only one, or the first one in the list if there is more than one.
    ======================================

    And the docs for getOldValueSets:

    ======================================
    getOldValueSets

    public java.util.List getOldValueSets()

    For an "update" or "remove" operation, returns the complete original record as it was delivered to the client, as a set of key-value pairs where the keys are field names and the values are field values.
    The server can compare the oldValues to the most recent stored values in order to detect that the user was looking at stale values when the user submitted changes (NOTE: this means of detecting concurrent edit is sometimes called "long transactions").
    Note that client logic must be written to pass oldValues. SmartClient DataBound Components do so automatically, but manually submitted DSRequests may not.
    Note: this signagure returns a List of value sets (Maps). This is the generic case and the recommended API because it captures interactions like updating multiple records at once. If there is only one set of values, then you will get a List with one Map.

    [b]Returns:[b]
    List of value sets for this request.
    See Also:
    getOldValues()
    ======================================


    Therefore to get a complete set of fields for the record (rather than a sparse set of editValues), you can do a simple combination of the old values with the new values - something like this:
    Code:
            DataSource ds = dsRequest.getDataSource();
            Map oldValues = dsRequest.getOldValues();
            Map newValues = dsRequest.getValues();
            for (Iterator ii = oldValues.keySet().iterator(); ii.hasNext();) {
                String key = (String) ii.next();
                if (ds.getField(key) != null && !newValues.containsKey(key)) newValues.put(key, oldValues.get(key));
            }
    Note: the reason for the discrepancy between values submitted from an edited listGrid record and from a DynamicForm is that grid editing supports asynchronous save, where user may continue editing the same record, so we need to be able to make the distinction on the client between fields that have been edited and were committed as part of a save, and edits that have been performed (and committed) while a save was occurring, on the same record, etc.

    Comment


      #3
      Thanks, your code snippet works great. I'd like to read more about asynchronous saves on ListGrids. Can you point me to any reference material on that subject?

      Comment


        #4
        Hello senordhuff,

        You are in asynchronous save mode if listGrid.waitForSave is false (the default). There's not much that a developer needs to know: the ListGrid automatically handles ongoing user editing while the asynchronous save takes place, including sticky cases like the server transforming edited values (eg, uppercasing or otherwise canonicalizing them). ListGrid editing was designed this way for the kind of rapid, "heads-down" editing you find with expert users in enterprise environments.

        Comment

        Working...
        X