YUI 数据表 - “数据错误”。

发布于 2024-10-09 21:37:52 字数 1528 浏览 2 评论 0原文

我正在尝试使用 YUI 和 JSON 返回的数据创建一个数据表。

包含的是json返回的数据,以及显示的页面数据。

JSON 数据:

[{"supplier_id":"127","name":"Adams Farms","description":"","ofarm":"1","active":"1"},{"supplier_id":"141","name":"Barriger Farms","description":"","ofarm":"1","active":"1"}]

YUI Javascript:

<script type="text/javascript">
YAHOO.util.Event.addListener(window, "load", function() {
    YAHOO.example.JSON = function() {
        var myColumnDefs = [
            {key:"supplier_id", label:"ID"},
            {key:"name", label:"Name"},
            {key:"description", label:"Notes"},
            {key:"ofarm", label:"Ofarm"},
            {key:"active", label:"Active"}
        ];

        var myDataSource = new YAHOO.util.DataSource("ajax/select/supplier");
        myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
        myDataSource.responseSchema = {
            fields: ["supplier_id","name","description","ofarm","active"]
        };

        var myDataTable = new YAHOO.widget.DataTable("json", myColumnDefs,
                myDataSource);

        return {
            oDS: myDataSource,
            oDT: myDataTable
        };
    }();
});
</script>

页面浏览量:

YUI 测试(标题)

此示例使用数据填充 DataTable。 (介绍文字)

ID - 名称 - 注释 - Ofarm - 活动(列标题)

数据错误。 (返回数据)

I'm trying to make a datatable using YUI with JSON returned data.

Included is the json returned data, and the page data displayed.

JSON Data:

[{"supplier_id":"127","name":"Adams Farms","description":"","ofarm":"1","active":"1"},{"supplier_id":"141","name":"Barriger Farms","description":"","ofarm":"1","active":"1"}]

Javascript for YUI:

<script type="text/javascript">
YAHOO.util.Event.addListener(window, "load", function() {
    YAHOO.example.JSON = function() {
        var myColumnDefs = [
            {key:"supplier_id", label:"ID"},
            {key:"name", label:"Name"},
            {key:"description", label:"Notes"},
            {key:"ofarm", label:"Ofarm"},
            {key:"active", label:"Active"}
        ];

        var myDataSource = new YAHOO.util.DataSource("ajax/select/supplier");
        myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
        myDataSource.responseSchema = {
            fields: ["supplier_id","name","description","ofarm","active"]
        };

        var myDataTable = new YAHOO.widget.DataTable("json", myColumnDefs,
                myDataSource);

        return {
            oDS: myDataSource,
            oDT: myDataTable
        };
    }();
});
</script>

Page View:

YUI Test (header)

This example populates a DataTable with data. (intro text)

ID - Name - Notes - Ofarm - Active (column titles)

Data error. (returned data)

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

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

发布评论

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

评论(2

太阳公公是暖光 2024-10-16 21:37:52

根据 YUI dataSource 页面,YUI dataSource 需要一个 JavaScript 对象,而不是一个对象数组。当使用 JSON 时,必须在 responseSchema 属性上设置 resultsList。类似于(注意 dataSourceSettings.responseSchema.fields 属性)

(function() {
    var YdataTable  = YAHOO.widget.DataTable,
        YdataSource = YAHOO.util.DataSource;

    var settings = {
        container:"<DATATABLE_CONTAINER_GOES_HERE>",
        source:"<URL_TO_RETRIEVE_YOUR_DATA>",
        columnSettings:[
            {key:"supplier_id", label:"ID"},
            {key:"name", label:"Name"},
            {key:"description", label:"Notes"},
            {key:"ofarm", label:"Ofarm"},
            {key:"active", label:"Active"}
        ],
        dataSourceSettings:{
            responseType:YdataSource.TYPE_JSON,
            responseSchema:{
                resultsList:"<DOT_NOTATION_LOCATION_TO_RESULTS>",
                fields:[
                    {key:"supplier_id"},
                    {key:"name"},
                    {key:"description"},
                    {key:"ofarm"},
                    {key:"active"}
                ]
            }
        },
        dataTableSettings:{
            initialLoad:false
        }
    }

    var dataTable = new YdataTable(
                    settings.container, 
                    settings.columnSettings, 
                    new YdataSource(
                    settings.source, 
                    settings.dataSourceSettings), 
                    settings.dataTableSettings);
})();

According to YUI dataSource page, YUI dataSource expectes an JavaScript object, not an array of objects. And when using JSON, use must set a resultsList on the responseSchema property. Something as (Notice dataSourceSettings.responseSchema.fields property)

(function() {
    var YdataTable  = YAHOO.widget.DataTable,
        YdataSource = YAHOO.util.DataSource;

    var settings = {
        container:"<DATATABLE_CONTAINER_GOES_HERE>",
        source:"<URL_TO_RETRIEVE_YOUR_DATA>",
        columnSettings:[
            {key:"supplier_id", label:"ID"},
            {key:"name", label:"Name"},
            {key:"description", label:"Notes"},
            {key:"ofarm", label:"Ofarm"},
            {key:"active", label:"Active"}
        ],
        dataSourceSettings:{
            responseType:YdataSource.TYPE_JSON,
            responseSchema:{
                resultsList:"<DOT_NOTATION_LOCATION_TO_RESULTS>",
                fields:[
                    {key:"supplier_id"},
                    {key:"name"},
                    {key:"description"},
                    {key:"ofarm"},
                    {key:"active"}
                ]
            }
        },
        dataTableSettings:{
            initialLoad:false
        }
    }

    var dataTable = new YdataTable(
                    settings.container, 
                    settings.columnSettings, 
                    new YdataSource(
                    settings.source, 
                    settings.dataSourceSettings), 
                    settings.dataTableSettings);
})();
于我来说 2024-10-16 21:37:52

附带说明一下,我在 YUI 数据表中查找“数据错误”的原因时发现了此页面,最终发现我的网页上缺少 /build/connection/connection-min.js 脚本引用。

As a side note, I found this page when looking for the cause of "Data error" in a YUI datatable, and I eventually found out that I was missing the /build/connection/connection-min.js script reference on my web page.

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