2010-02-24 125 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; 
      } 

..但有可能更多的是比這一點。