Use GetData API in file-based module

LabKey Support Forum (Inactive)
Use GetData API in file-based module Christopher Garay  2014-05-12 13:01
Status: Closed
 
I'm currently implementing a custom UI for a project inside of a file-based module. I'm using the GetData API to get data to plot. Currently, this works if I add my JavaScript plotting code through the Manage Views interface and check "Use GetData API". I can't get this to work if I include the report as a file in the module. Is there any way to accomplish this?

Thanks,

Chris
 
 
alanv responded:  2014-05-12 16:56
Hi Christopher,

Can you give me any more detail as to what's going on in your file based view? Are you seeing any errors on the page? Are you able to show me the snippet of code that you're using to make the getData API call? If you haven't given it a read, the documentation for the JavaScript GetData wrapper can be found here:

https://www.labkey.org/download/clientapi_docs/javascript-api/symbols/LABKEY.Query.GetData.html#.getRawData
 
Christopher Garay responded:  2014-05-13 04:39
The default JavaScript report code works well as an example:

var jsDiv;

// When a JavaScript report is viewed, LabKey calls the render() function, passing a query config
// and a div element. This sample code simply stashes the div, initializes callback functions, and
// calls selectRows() to retrieve the data from the server. See the "Help" tab for more details.
function render(queryConfig, div)
{
    jsDiv = div;
    queryConfig.success = onSuccess;
    queryConfig.error = onError;
    LABKEY.Query.GetData.getRawData(queryConfig);

    // If not using the GetData API you can use SelectRows instead:
    // LABKEY.Query.selectRows(queryConfig);
}

function onSuccess(results)
{
    //jsDiv.innerHTML = results.rows.length + ' rows returned';
}

function onError(errorInfo)
{
    jsDiv.innerHTML = errorInfo.exception;
}


If I open a new report (which has "Use GetData API" checked) and keep the example code I see the correct number of rows returned from the getRawData call. However, if I copy and paste this as a report file inside my module, keeping the call to getRawData yields the following error:

"Error: A source is required for a GetData request."

selectRows does return the correct number of rows when I use that in the file. It looks like LabKey is passing a different object for queryConfig that depends on whether or not the "Use GetData API" box being checked. It's unclear from the documentation how to enable this for a file-based module report in the module configuration.

Thanks for your help!

Chris
 
alanv responded:  2014-05-13 12:57
Hey Chris,

When you create a JavaScript report LabKey creates the appropriate configuration for you. If you select "Use GetData API" it creates a queryConfig object that conforms to the JavaScript GetData.rawData requirements. In order to manually use the GetData API you'll need to write some code that looks something like this:

<script type="text/javascript">
    var config = {
        source : {
            type: 'query',
            schemaName: '<The Schema Name You Need Goes Here>',
            queryName: '<The Query Name YOu Need Goes Here>'
        },
        success: function(response) {
           // Your Success function goes here.
           console.log(response);
        },
        failure: function(response) {
            // Your failure function goes here.
            console.error('Error occurred during GetData request');
            console.log(response);
        }
    };

    LABKEY.Query.GetData.getRawData(config);
</script>

You can also specify the list of columns you want, the sorts you need, filters, grouping, and more. All of the GetData config options are listed in the documentation that I linked above.