如何解析使用YUI数据源返回的NULL值 [英] How to parse NULL value returned using YUI datasource

查看:113
本文介绍了如何解析使用YUI数据源返回的NULL值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用YUI数据表和数据源在我的一个项目中呈现数据。返回的数据恰好为NULL,YUI数据源无法解析它。

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.

列描述声明

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

此列说明在下面的函数中设置。

This columnDescription is set in the function below.

数据源声明

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 函数调用服务器端以从

getDataGrid function makes the call to server side to get the data from the server.

下面是表定义本身。

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数据的函数。

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
}

该函数返回的json字符串是有一些空值。数据源构造函数抱怨以下问题。

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


  • ERROR_DATAINVALID

  • ERROR_DATANULL

我检查了yui 文档,发现字符串解析器无法解析空值。我想知道是否有任何方法可以解析此数据。我是否必须处理Response解析原始数据?

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.

推荐答案

首先请确保您需要parser:YAHOO.util.DataSource.parseString作为字段。我还没有看到您的JSON结构。因此,我无法对此发表评论。

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

这篇关于如何解析使用YUI数据源返回的NULL值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆