jQWidgets Forums

jQuery UI Widgets Forums Plugins Data Adapter Accessing DataAdapter records

This topic contains 4 replies, has 2 voices, and was last updated by  ivailo 10 years, 1 month ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
  • Accessing DataAdapter records #70744

    phpmaven
    Participant

    I have the following code.
    I need to iterate through the data returned from the Ajax call and I trying to do that by accessing the records in the dataAdapter.
    But if I try and access those records outside of creating the new dataAdapter, it’s tells me it’s undefined.
    The alert box on the second to last line says that it’s undefined even though the combobox on the last line gets created just fine.

    What am I doing wrong?

    Thanks,

    Mark

    $(document).ready(function () {
    var data_url = ‘/get_builders.php’;
    var source =
    {
    datatype: “json”,
    datafields: [
    { name: ‘BuilderId’, type: ‘string’},
    { name: ‘BuilderName’, type: ‘string’},
    { name: ‘Checked’, type: ‘string’}
    ],
    id: ‘id’,
    url: data_url,
    sortcolumn: ‘Name’,
    sortdirection: ‘asc’,
    cache: false
    };
    var current_user = $(‘#userId’).val();
    var dataAdapter = new $.jqx.dataAdapter(source, {
    formatData: function (data) {
    $.extend(data, {
    userId: current_user
    });
    return data;
    },
    loadError: function(xhr, status, error)
    {
    alert(error);
    }
    }
    );

    // Create a jqxComboBox
    alert(dataAdapter.records[0]);
    $(“#jqxcombobox”).jqxComboBox({ checkboxes: true, source: dataAdapter, displayMember: “BuilderName”, valueMember: “BuilderId”, width: 400, height: 17});

    Accessing DataAdapter records #70761

    ivailo
    Participant

    Hi phpmaven,

    Cause of the asynchronous nature of the request you have some delay about loading data from your external source.
    You can try to use loadComplete callback function to be sure about your data is successfully loaded. In this function you can insert your alert.

    var dataAdapter = new $.jqx.dataAdapter(source, {
                  loadComplete: function () {
                      alert(dataAdapter.records[0]);
                  },
                  formatData: function (data) {
                  $.extend(data, {
                      userId: current_user
                  });
                  return data;
                  },
                  loadError: function(xhr, status, error)
                  {
                  alert(error);
                  }
    });

    Best Regards,
    Ivailo Ivanov

    jQWidgets Team
    http://www.jqwidgets.com

    Accessing DataAdapter records #70800

    phpmaven
    Participant

    Thank you, but that doesn’t give me what I need. If I try and create a variable from the returned records, it will tell me it’s undefined outside of the scope of the dataAdapter functions. For example if I try the following code and then try to use the “test” variable outside of the dataAdapter, it tells me it’s undefined. The “test” var should be global, but I can’t access it outside the function.

    Mark

    *******************************

    var test;
    var dataAdapter = new $.jqx.dataAdapter(source, {
    loadComplete: function () {
    test = dataAdapter.records[0];
    },
    formatData: function (data) {
    $.extend(data, {
    userId: current_user
    });
    return data;
    },
    loadError: function(xhr, status, error)
    {
    alert(error);
    }
    });

    // get undefined error here
    alert(test);

    Accessing DataAdapter records #70802

    phpmaven
    Participant

    As a follow up, I can’t access dataAdapter.records[] object outside of the dataAdapter definition:

    ***************************

    var dataAdapter = new $.jqx.dataAdapter(source, {
    loadComplete: function () {
    // this shows an object
    alert(dataAdapter.records[0]);
    },
    formatData: function (data) {
    $.extend(data, {
    userId: current_user
    });
    return data;
    },
    loadError: function(xhr, status, error)
    {
    alert(error);
    }
    }
    );
    //this shows undefined
    alert(dataAdapter.records[0]);

    Accessing DataAdapter records #70824

    ivailo
    Participant

    Hi phpmaven,

    The loading of the external data has some delay. In this time the rest of the code continues it’s execution. That means asynchronous. That make the execution faster and not depending of the time for data loading. The loadComplete function indicates that your data loading is finished, and in it’s body you can call all your function depended to this data.

    In your code maybe you’re expecting synchronous execution, but when you alert your dataAdapter it is not yet ready for use. So you receive “undefined”.

    If you realy want to use synchronous way of coding you can set the async property to false, but this will slow your app depended of data loading from get_builders.php.

    More about jqxDataAdapter you can read here.

    Best Regards,
    Ivailo Ivanov

    jQWidgets Team
    http://www.jqwidgets.com

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

You must be logged in to reply to this topic.