views:

172

answers:

2

Hi,

I'm trying to put together an application which uses YUI's DataTable component but I get the "Data error" message. The datasource is configured to get the records from an ASP.NET web method. The records are returned to the client side successfully (I checked it with IE's debugger). My code looks like the following:

YAHOO.example.Basic = function() {
            var dsWS_Restaurants = new YAHOO.util.DataSource("/DemoWebSite/RestaurantsWebService.asmx/GetList", { connMethodPost: true });

            dsWS_Restaurants.connMgr = YAHOO.util.Connect;
            dsWS_Restaurants.connMgr.initHeader('Content-Type', 'application/json; charset=utf-8', true);
            dsWS_Restaurants.responseType = YAHOO.util.DataSource.TYPE_JSON;

            dsWS_Restaurants.doBeforeParseData =
                function(oRequest, oFullResponse, oCallback) {
                    // checked here if oFullResponse contains the desired results and it does.
                }

            dsWS_Restaurants.responseSchema =
            {
                resultsList: 'd.records',
                fields: ["id", "name"]
            };

            var dsWS_Restaurants_ColumnDefs = [
                { key: "id", sortable: true, resizeable: true },
                { key: "name", sortable: true, resizeable: true }
                ];

            var dsWS_Restaurants_DataTable =
                new YAHOO.widget.DataTable("basic4", dsWS_Restaurants_ColumnDefs, dsWS_Restaurants, { caption: "dsWS_Restaurants" });

            return {
                oDS: dsWS_Restaurants,
                oDT: dsWS_Restaurants_DataTable
            };
        } ();

...

Web method look like this:

public Object GetList() {
    var restaurants =
        new []{
            new
            {
                id="1",
                name="Popeyes spinach"
            },
            new
            {
                id="2",
                name="Big pappas cottage"
            }
        };

    return restaurants.Select (x => new { id = x.id, name = x.name });

}

Any help is welcome and appreciated. Thanks in advance.

A: 

I believe that the overridable doBeforeParseData method should return the oFullResponse object...

        dsWS_Restaurants.doBeforeParseData =
            function(oRequest, oFullResponse, oCallback) {
                // checked here if oFullResponse contains the desired results and it does.
                 return oFullResponse;
            }

.. but there may be more to it than just that.

Gavin Brock
+1  A: 

I found out what caused the error. In the responseSchema of the datasource the resultList was defined as 'd.records' but I had no "records" field returned by the web method. I replaced 'd.records' with 'd' and the sample worked. My mistake was that I borrowed the code from a sample application from http://mattberseth.com/blog/2008/09/dynamic_data_experimenting_wit.html which used the "records" field.

Happy coding.

Zoliq