Announcement

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

    How to obtain a ListGridRecord from a ListGrid by its primary ID...

    Hello SmartGWT gurus...

    I need to call a ListGrid function that requires a ListGridRecord and I only have the primary key of the record I need to update.

    How do I obtain a ListGridRecord from a primary ID?

    Many thanks!

    Jean-Pierre

    #2
    Hi all, I can get an updated record with the following code:

    Code:
    Criteria oCrit = new Criteria("participantId", sRecID);
      fetchData(oCrit, new DSCallback() {
        public void execute(DSResponse response, Object rawData, DSRequest request) {
          Record[] aRec = response.getData();
       }
    });
    but unfortunately the grid now only display the new (updated) record and I'd be forced to do another full fetch to display the up-to-date full data.

    In other words, for this particular case, I don't really need to obtain a ListGridRecord from a primary key... I just need the ListGrid cache to update itself and what I have is a primary key.

    Am I on the right track with fetchData? I would like to call updateData but it requires a Record object and I don't know how to obtain one from a primary key.

    Help! :)

    Jean-Pierre

    Comment


      #3
      It depends what triggered the update. If you saved a record from a form, update is designed to be automatic and you should not have to do anything - see the FAQ on grids not updating for details.

      If the data was saved by some other means or you are just trying to refresh an individual record, you can fetch the new data if needed via DataSource.fetchData as you've shown, then you supply the update to all DataBoundComponents using DataSource.updateCaches().

      Comment


        #4
        Hi Admin, thanks for your response.

        In this case, the server updates its own JPA bean, and via Comet I push a map containing IDs of all records that have changed so that on the client the ListGrid knows how to request just those records.

        A few records from the client data & cache are therefore invalid in this situation, and I just need a way to invalid the relevant cache records and request the up-to-date records hopefully without requesting the entire table.

        DataSource.updateCaches() seems to flush the entire cache... is there a way to get the cache to update just a few record?

        Many thanks!

        Jean-Pierre

        Comment


          #5
          No, DataSource.updateCaches updates individual records without dropping the rest of the cache and is the right solution in this use case.

          Comment


            #6
            Hi Admin,

            If I understand your response correctly, all I need to do is call DataSource.updateCaches() when the server changes at least one record and the client/server is smart enough to serialize just the changed records?

            Thanks again,

            Jean-Pierre

            Comment


              #7
              Hi, for those following these threads, the best solution I found thus far is similar to the one found at http://forums.smartclient.com/showthread.php?t=14370&highlight=updateCaches

              Code:
                  public void UpdateDatasourceRecords(final DataSource oDS, Criteria oCrit) {
                      // Forces the datasource to update records based on oCrit, update the cache and the visual display of relevant GUI elements (such as ListGrid)
                      oDS.fetchData(oCrit, new DSCallback() {
                          public void execute(DSResponse response, Object rawData, DSRequest request) {
                              request.setOperationType(DSOperationType.UPDATE);       //***LEARN: Update a single record via technique at http://forums.smartclient.com/showthread.php?t=14370&highlight=updateCaches
                              oDS.updateCaches(response, request);
                              //Record[] aRec = response.getData();       //***LEARN: How to access update records returned
                          }
                      });
                  }
              Now the only remaining problem I have is how to obtain a ListGridRecord from a primary ID. Has anyone succeeded in doing this?

              Many thanks!

              Jean-Pierre

              Comment


                #8
                If you mean from already-loaded data, use grid.getResultSet.find(pkFieldName, pkValue).

                If you mean from the server, call DataSource.fetchData() with criteria that contains only the pkField and value.

                Comment


                  #9
                  Hi Admin, many thanks... I've been scratching my head for months on how to do this. grid.getResultSet.find(pkFieldName, pkValue) looks like it will do it.

                  Recommendation for SmartGWT 2.5: As grids require only one primary key, why not simplify the above and write a ListGrid.FindRecordByPrimaryKey(pkValue) that wraps up the above?

                  Obtaining a ListGridRecord by primary key is totally needed in tons of situations, and most new developers would have a hard time finding the "grid.getResultSet.find(pkFieldName, pkValue)" solution.

                  Thanks again for the great work on SmartGWT and your professional time answering our questions!!

                  Jean-Pierre

                  Comment


                    #10
                    Almost that exact API is in fact planned in order to make this easier to find.

                    Comment


                      #11
                      Hi Admin, most awesome!

                      However, your solution returns a Record object. I've tried casting to ListGridRecord and I get an invalid cast exception.

                      Any way to get a ListGridRecord from a Record?

                      Thanks!

                      Jean-Pierre

                      Comment


                        #12
                        All the Record types are inter-convertible like so: new ListGridRecord(record.getJsObj())

                        Comment


                          #13
                          Coolness... it works! I think we'd have a hard time progressing with SmartGWT if this forum didn't exist!

                          My solution is:
                          Code:
                              public ListGridRecord FindListGridRecord(ListGrid oGrid, String sFieldName, String sFieldValue) {
                                  Record oRecord = oGrid.getResultSet().find(sFieldName, sFieldValue);        //***LEARN: How to find a ListGridRecord by primary id as per technique at http://forums.smartclient.com/showthread.php?t=16988&page=2
                                  ListGridRecord oRec = new ListGridRecord(oRecord.getJsObj());
                                  return oRec;
                              }
                          Thanks again!

                          Jean-Pierre

                          Comment

                          Working...
                          X