Announcement

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

    DataSourceField for JSON attribute containing array of Strings

    I have JSON returning:

    Code:
    "data":[
       {
          "id":50904,
          ...
          "buckets":{
             "b1":[
                "b1Value1".
                "b1Value2".
             ],
             "b2":[
                "b2Value1",
                "b2Value2",
             ],
          }
       },
       ...
    ]
    Code:
    DataSourceBinaryField field = new DataSourceBinaryField( "buckets_b1, "b1" );
    field.setValueXPath( "buckets/b1" );
    field.setCanEdit( true );
    is this right? How do I make sure I can edit the sub array with a SelectItem.multiple=true in a listgrid cell?

    So far the behavior is that my SelectItem.changed[handler] gets fired, but my ListGrid.cellSaved[handler] does nor ListGrid.editComplete[handler].

    In short, I need to edit a multi-select attribute in a record attribute sub-structure and have it trigger to go back to the server for update. (I so hope this is doable and not too tricky)

    My Details:

    1. the *complete* SmartGWT or SmartClient version from the lower left-hand corner of the Developer Console (see FAQ for how to open Developer Console), for example, \"v8.2p_2012-04-18/PowerEdition Deployment\"

    SmartClient Version: v9.0p_2013-11-17/LGPL Development Only (built 2013-11-17)

    2. browser(s) and version(s) involved

    FF/Mac

    3. for a server-side problem, the *complete* logs generated during processing of the failing request (do *not* trim to just the error message)

    n/a

    4. for any problem processing a server response, the actual response as shown in the RPC tab in the Developer Console

    n/a

    5. if there is a JavaScript error, the stack trace logged in the Developer Console (see FAQ)

    n/a

    6. sample code if applicable

    see above
    Last edited by RobertHana; 5 Mar 2014, 08:54.

    #2
    Using a "binary" field is definitely wrong (that's for files) and you need to set multiple:true.

    At that point the field will be editable with a multi-select. There's no way to guess what's going on with your save (waaaay too little information provided).

    However note when saving there will be no automatic attempt to re-create your nested structure - if you save via eg a RestDataSource, an array of values will be saved for the field buckets_b1 with no "b1" nested array.

    Comment


      #3
      DataSourceTextField, but still not saving

      ok i am using DataSourceTextField, but it still won't save my selected strings to "buckets/b1".

      i set an editor customizer on my listgrid to provide a new SelectItem (so i have access to it at some point, mostly debugging). I set a value map on the selectItem (but not on the listgridfield, nor the datasourcefield (this is a abound listgrid).

      selectItem.setMultipleAppearance( MultipleAppearance.PICKLIST );
      selectItem.setValueMap( "Cat", "Dog", "Giraffe", "Goat", "Marmoset", "Mouse" );

      other perhaps non-related stuff:
      listgrid.canEdit = true
      listgrid.showAllRecords = true
      listgrid.editEvent = true

      i added listgrid.editCompleteHandler just to logWarn and just changing the SelectItem selections and clicking away from the record does not trigger an editComplete.

      so something in about my SelectItem on change doesn't doesn't let the listgrid kno it has pending changes i think.

      i did try SelectItem.onChangedHandler ...
      and have it do an event.getForm().saveData() and the outbound PUT *DID HAVE* my selected values.

      So i still think my SelectItem selections are not letting listgrid row (in row-edit mode) know it has pending changes or something like that.

      i dunno if i have time to build a full standalone test case


      sorry, i also made a mistake in my original pseudocode ... here's my DSField:

      Code:
      DataSourceTextField field = new DataSourceTextField( "foobar" );
      field.setValueXPath( "buckets/b1" );
      field.setCanEdit( true );
      field.setMultiple( true );
      field.setMultipleValueSeparator( ", " );
      so my dsfield "foobar" is NOT an actual attribute in the inbound JSON (foobar is a "fake"? attribute, dsfield to manage changes to buckets/b1, right? -- and not make a new one for sure -- the array is provided empty or not)
      Last edited by RobertHana; 5 Mar 2014, 10:34.

      Comment


        #4
        Still no information about what kind of DataSource you have, how you intend it to save, what request you are seeing go to the server, what the response is, etc. No one can help without basic information like this.

        Comment


          #5
          sorry, yes it is REST datasource,

          but i don't see that as the problem ... i'm in the middle of selecting values in the selectitem on the page

          and at various points i call (in the SmartClient Dev Console):

          myListGrid1.hasChanges()

          and i get FALSE

          if i change values in other fields (plain text fields, whatnot)

          hasChanges() gives me TRUE

          Comment


            #6
            Take a look at the Grid Editing overview; it doesn't seem like you have a good handle on the lifecycle of edits, when they are sent to the server, when events will fire, etc.

            Comment


              #7
              could there be a problem if the field name provided ("foobar") was really "foo.bar"?

              Comment


                #8
                Yes, that's not a valid fieldName.

                Comment


                  #9
                  ok i changed it to foo_bar and it works to trigger the update, but now the structure being sent in the REST datasource is:

                  Code:
                  {
                      "dataSource":"myDS", 
                      "operationType":"update", 
                      "componentId":"myListGrid", 
                      "data":{
                          "id":50904, 
                          "foo_bar":[
                              "Cat", 
                              "Dog", 
                              "Giraffe", 
                              "Goat"
                          ]
                      }, 
                      "oldValues":{
                          "id":50904, 
                          "buckets":{
                              "b1":[
                                  "b1Value1"
                              ]
                          }, 
                          "foo_bar":"b1Value1"
                      }
                  }
                  interesting tho that the original data from the fetch was:

                  Code:
                      "data":[{
                          "id":50904, 
                          "buckets":{
                              "b1":[
                                  "b1Value1"
                              ]
                          }
                      }]
                  Note: original data had NO "foo_bar".

                  I thought DataSourceField.setValueXPath was supposed to affect translation in-bound AND back OUT to the JSON. And that it should not cause the new attribute "foo_bar" to appear.

                  Comment


                    #10
                    The docs are explicit that valueXPath does not try to recreate the original JSON structure, and so were we, in post #2.

                    Comment


                      #11
                      Thanks, I misread post 2. I took it to say that if the substructure didn't exist, the save would not create it -- or something like that. Now I understand that XPath is not used on save whatsoever.

                      I solved it by original problem of persisting the data, by having code on the REST back-end to handle the newly added attributes at the top level.

                      Thanks for your help. Sorry for all the confusion. I just had a bad assumption that XPath would make things super simple by being used in both directions.

                      Comment

                      Working...
                      X