我正在使用 YUI 数据表和数据源在我的一个项目中呈现数据。返回的数据恰好为 NULL,YUI 数据源无法解析它。
下面是数据源和数据表的声明代码。为了便于阅读,我将每个声明分开。
列描述声明
var columnDescription =
[
{key:'Requirements'},
{key:'abc'},
{key:'xyz'}
];
此列描述在下面的函数中设置。
数据源声明
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 页面中声明。这是容器 div。 从服务器获取 JSON 数据的函数。
function getDataGrid()
{
//calls backend and gets the data
}
该函数返回具有一些空值的 json 字符串。数据源构造函数提示以下问题。
- ERROR_DATAINVALID
- ERROR_DATANULL
我检查了 yui documentation发现字符串解析器不解析空值。我想知道是否有任何方法可以解析这些数据。我必须 handleResponse 解析原始数据吗?任何建议表示赞赏。
最佳答案
首先确保您需要parser: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
}
];
关于javascript - 如何解析使用 YUI 数据源返回的 NULL 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/509620/