如何解析使用YUI数据源返回的NULL值

发布于 2024-07-13 02:13:54 字数 1643 浏览 6 评论 0原文

我正在使用 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 文档 并发现字符串解析器不解析空值。 我想知道是否有任何方法可以解析这些数据。 我必须用handleResponse解析原始数据吗? 任何建议表示赞赏。

I am using YUI datatable and datasource to render data in one of my projects. The data returned happens to be NULL and YUI datasource is unable to parse it.

Below is the declaration code of datasource and datatable. For readability sake, I am seperating each of the declarations.

Column Descriptions declaration

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

This columnDescription is set in the function below.

DataSource Declaration

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 function makes the call to server side to get the data from the server.

Below is the table definition itself.

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 is declared in the html page. This is the container div.
The function that gets the JSON data from server.

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

The function is returning json string that has some null values. Datasource constructor is complaining following problems.

  • ERROR_DATAINVALID
  • ERROR_DATANULL

I checked the yui documentation and found that the string parser does not parse null values. I am wondering if there is any way to parse this data. Do I have to handleResponse parse the raw data? Any suggestions appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮萍、无处依 2024-07-20 02:13:54

首先确保字段需要 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
                        }
                     ];

First make sure that you need parser:YAHOO.util.DataSource.parseString for the fields. I haven't seen your JSON structure. So I cannot comment on this.

Other option is to use a custom formatter. Something like the following snippet will work.

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
                        }
                     ];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文