2010-02-24 126 views
0

我试图放在一起使用YUI的DataTable组件的应用程序,但我得到“数据错误”消息。数据源被配置为从ASP.NET Web方法获取记录。记录成功返回到客户端(我使用IE的调试器进行了检查)。我的代码如下所示:YUI数据表错误

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方法是这样的:

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 }); 

}

任何帮助是值得欢迎和赞赏。提前致谢。

+0

http://sscce.org/或repro链接如何? – 2010-02-24 19:39:16

+0

也可能值得发布在IE调试器中记录的JSON。 – 2010-02-25 03:54:09

回答

1

我发现了什么导致了错误。在数据源的responseSchema中,resultList被定义为'd.records',但我没有web方法返回的“records”字段。我用'd'替换了'd.records',并且样本工作。我的错误是我借用了来自http://mattberseth.com/blog/2008/09/dynamic_data_experimenting_wit.html的使用“记录”字段的示例应用程序中的代码。

快乐编码。

1

我相信重写doBeforeParseData方法应该返回oFullResponse对象...

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

..但有可能更多的是比这一点。