2009-02-04 56 views
1

我正在使用YUI數據表和數據源在我的一個項目中呈現數據。返回的數據恰好是NULL,YUI數據源無法解析它。如何解析使用YUI數據源返回的NULL值

下面是datasource和datatable的聲明代碼。爲了可讀性,我將每個聲明分開。

列說明聲明

  var columnDescription = 
      [ 
       {key:'Requirements'}, 
       {key:'abc'}, 
       {key:'xyz'} 
      ]; 

這columnDescription在下面的功能設置。

數據源宣言

var dataSrcSample = new YAHOO.util.FunctionDataSource(getDataGrid); 
       myDataSource.connMethodPost = true; 
       myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON; 
       myDataSource.responseSchema = { 
       fields:['Requirements', 
{key:'abc',parser:YAHOO.util.DataSource.parseString}, 
{key:'xyz',parser:YAHOO.util.DataSource.parseString}] 
        }; 

getDataGrid函數進行調用服務器端從服務器獲取數據。 下面是表格定義本身。

 YAHOO.example.sampleTable = function() 
     { 
          var columnDesc=columnDescription; 
      var myDataSource = dataSrcSample; 
      var oConfigs = 
      { 
       width:'100%' 
      }; 

      var myDataTable = new YAHOO.widget.DataTable("tableContainerDiv", 
columnDesc, 
myDataSource, 
oConfigs); 
      }(); 

tableContainerDiv在HTML頁面聲明。這是容器分區。 從服務器獲取JSON數據的函數。

function getDataGrid() 
{ 
     //calls backend and gets the data 
} 

該函數返回具有一些空值的json字符串。數據源構造函數抱怨以下問題。

  • ERROR_DATAINVALID
  • ERROR_DATANULL

我檢查了銳documentation,發現串分析器不分析空值。我想知道是否有任何方法來解析這些數據。我必須處理響應解析原始數據嗎?任何建議感激。

回答

0

首先確保您需要解析器:YAHOO.util.DataSource.parseString用於字段。我還沒有看到你的JSON結構。所以我不能對此發表評論。

其他選項是使用自定義格式器。像下面的代碼片段可以工作。

var customFormatter = function(elCell, oRecord, oColumn, sData) { 
    elCell.innerHTML = ''; 
    try { 
     var strData = YAHOO.lang.JSON.parse(sData); 
     // set the elCell.innerHTML based on the strData 
    } catch { 
     // don't to anything 
    } 
} 

myDataSource.responseSchema = {fields:['Requirements', 'abc', 'xyz']}; 

var columnDescription = 
        [ 
         {key:'Requirements'}, 
         {key:'abc', 
         formatter: customFormatter 
         }, 
         {key:'xyz', 
         formatter: customFormatter 
         } 
        ];