jQuery UI Widgets Forums Plugins Data Adapter Select dataAdapter record based on outside variable

This topic contains 1 reply, has 2 voices, and was last updated by  Dimitar 8 years, 10 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author

  • jcf378
    Participant

    Is it possible for dataAdapter to return a record (and a specific field value from within that record) based on a variable outside dataAdapter?
    For instance:

    
    var outside = somevalue;
    var source = 
    	{ datatype: "json",
    	  datafields: [
    	   { name: 'shortname'},
    	   { name: 'longname'}
    	     ],
       	  url: 'databasearray.php'};
    
    var dataAdapter = new $.jqx.dataAdapter(source, {
                    loadComplete: function (records) {
                        var records = dataAdapter.records;
    

    // i would then like to select the specific record row where datafield ‘longname’ = outside.
    // from this identified row, i would like the function to return the value of ‘shortname’.
    // can I achieve that dynamic row selection based on the “var outside” from within dataAdapter?
    // many thanks, –Jason

    
    ...
                    },
                    loadError: function (jqXHR, status, error) {
                    },
                    beforeLoadComplete: function (records) {
                    }
                });
    
                dataAdapter.dataBind();
    

    Dimitar
    Participant

    Hello Jason,

    There are two possible ways to achieve this:

    1. Server-side – load only the necessary record(s) by sending the value of outside to the server and returning database records depending on it (with the WHERE clause). You can pass the value with the source object’s data property, i.e.:
      var outside = somevalue;
      var source = {
          datatype: "json",
          datafields: [{
              name: 'shortname'
          }, {
              name: 'longname'
          }],
          url: 'databasearray.php',
          data: {
              outside: outside
          }
      };
      
      var dataAdapter = new $.jqx.dataAdapter(source, {
          loadComplete: function(records) {
              var records = dataAdapter.records;
          },
          loadError: function(jqXHR, status, error) {},
          beforeLoadComplete: function(records) {}
      });
      
      dataAdapter.dataBind();
    2. Client-side – filter the loaded records in beforeLoadComplete:
      var outside = somevalue;
      var source = {
          datatype: "json",
          datafields: [{
              name: 'shortname'
          }, {
              name: 'longname'
          }],
          url: 'databasearray.php'
      };
      
      var dataAdapter = new $.jqx.dataAdapter(source, {
          loadComplete: function(records) {
              var records = dataAdapter.records;
          },
          loadError: function(jqXHR, status, error) {},
          beforeLoadComplete: function(records) {
              var filteredRecords = [];
              for (var i = 0; i < records.length; i++) {
                  if (records[i].longname === outside) {
                      filteredRecords.push(records[i]);
                  }
              }
              return filteredRecords;
          }
      });
      
      dataAdapter.dataBind();

    Best Regards,
    Dimitar

    jQWidgets team
    http://www.jqwidgets.com/

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.