Announcement

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

    Get record from a datasource

    Hello,
    I a beginner in smartgwt. My problem is that I can't find out how to get data from a datasource, unless it is inside a listgrid by setDataSource(DataSource);
    setAutoFetchData(true);

    I have a php file that return data at json format, and it is working fine
    Here is my code
    Code:
     DataSource sData = new DataSource();
            sData.setDataFormat(DSDataFormat.JSON);
            sData.setDataURL("http://127.0.0.1:8888/smartgwt/get?service=03");
            DataSourceIntegerField id_field = new DataSourceIntegerField("id");
            DataSourceFloatField min_field = new DataSourceFloatField("min_lim");
            DataSourceFloatField max_field = new DataSourceFloatField("max_lim");
            DataSourceTextField type_name_field = new DataSourceTextField("type_name");
            DataSourceTextField unit_field = new DataSourceTextField("unit");
            sData.setFields(id_field,min_field,max_field,type_name_field,unit_field);
            sData.fetchData();
    I don't want to display this data in any widget. It is the application flow control. How do I can take the data from the DataSource and put the field values into java object or primitive variables (int, string etc)
    I tried with ResultSet using
    ResultSet res = new ResultSet(sData);
    Record rec=res.first();

    Can someone help me to figure out how to do it?
    thanks

    #2
    fetchData() has a callback providing you with direct access to the data when it arrives. Do with it what you want at that point. Remember fetchData() is asynchronous.

    Comment


      #3
      thanks for the quick reply david.
      Is there an example of how to use this callback? I read the docs but I cant put DSCallback to work. do I need to implement it on server-side?

      Comment


        #4
        Something like this:
        Code:
        ds.fetchData(criteria, new DSCallback() {
            public void execute(DSResponse response, Object rawData, DSRequest request) {
                // Grab an array of records or a RecordList. The latter handy for searches.
                Record[] records = response.getData();
                RecordList recordList = response.getDataAsRecordList();
                // Use one of the above here...
            }
        });

        Comment


          #5
          Hi,

          I have trouble extracting the data from the response also. Concider this code:
          Code:
          searchButton.addClickHandler(new ClickHandler() {
               public void onClick(ClickEvent event) {
                   DynamicForm f = searchForm;
                   Criteria c = f.getValuesAsCriteria();
                   searchResults.fetchData(c , new DSCallback() {
          		public void execute(DSResponse response, Object rawData, DSRequest request) {
                              Record[] data = response.getData();
          		}
          	     });
                }
           });
          When I don't add the callback to the fetchData method, my form retrieves the data. When I do add the callback, I cannot figure out how to extract the data from it.

          The data object (the Record[]) has a length of 1. If I execute data[0].getAttributes() I get an empty String[]. I need to extract the Status field (see below) which is an error state in this case.

          The XML I retrieve back from the wsdl call is:
          Code:
          <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body>
          <opvragenBedrijfAntw>
            <BedrijfQuery xsi:nil="true"/>
            <Status>02</Status>
            <Bedrijf xsi:nil="true"/>
          </opvragenBedrijfAntw>
          </soapenv:Body>
          </soapenv:Envelope>
          Last edited by phoebe; 15 Sep 2010, 04:38.

          Comment

          Working...
          X