具有 JSON 和客户端过滤数据错误的 YUI DataTable

发布于 2024-08-29 05:25:28 字数 2823 浏览 8 评论 0原文

我不明白我在这里做错了什么!我不断收到数据错误。但我验证了 JSON 并且没问题...

这是来自 YUI Datatble 示例的 javascript(稍作修改)。



<pre class="prettyprint"><code>

<script type="text/javascript">
YAHOO.util.Event.addListener(window, "load", function() {
    //var Ex = YAHOO.namespace('example');

    var dataSource = new YAHOO.util.DataSource("jsondb/json_meta_proxy.html",{
        responseType : YAHOO.util.DataSource.TYPE_JSON,
        responseSchema : {
            resultsList: "records",
            fields: [
                            {key:"idprojet"},
                            {key:"nomprojet"}
                    ],
                    metaFields: { 
                    totalRecords: "totalRecords"
                    }
        },
        doBeforeCallback : function (req,raw,res,cb) {
            // This is the filter function
            var data     = res.results || [],
                filtered = [],
                i,l;

            if (req) {
                req = req.toLowerCase();
                for (i = 0, l = data.length; i < l; ++i) {
                    if (!data[i].state.toLowerCase().indexOf(req)) {
                        filtered.push(data[i]);
                    }
                }
                res.results = filtered;
            }

            return res;
        }
    });

    var cols = [
        {key:"idprojet"},
            {key:"nomprojet"}
    ];

    var paginateur = new YAHOO.widget.Paginator({
        rowsPerPage   : 25,
        pageLinks     : 10
    });

    var conf = {
        paginator : paginateur,
        sortedBy: {key:'idprojet', dir:YAHOO.widget.DataTable.CLASS_ASC}
    };

    var dataTable = new YAHOO.widget.DataTable('tbl',cols,dataSource,conf);

    var filterTimeout = null;
    var updateFilter  = function () {
        // Reset timeout
        filterTimeout = null;

        // Reset sort
        var state = dataTable.getState();
            state.sortedBy = {key:'idprojet', dir:YAHOO.widget.DataTable.CLASS_ASC};

        // Get filtered data
        dataSource.sendRequest(YAHOO.util.Dom.get('filter').value,{
            success : dataTable.onDataReturnInitializeTable,
            failure : dataTable.onDataReturnInitializeTable,
            scope   : dataTable,
            argument: idprojet
        });
    };

    YAHOO.util.Event.on('filter','keyup',function (e) {
        clearTimeout(filterTimeout);
        setTimeout(updateFilter,600);
    });
});
</script>

这是文件“jsondb/json_meta_proxy.html”中的 JSON 数据



    {
    "recordsReturned": 1,
    "totalRecords": 1,
    "startIndex": 0,
    "sort": "idprojet",
    "dir": "asc",
    "records": [
        {
            "idprojet": "11256",
            "nomprojet": ""
        }
    ]
}

非常感谢您的帮助!

I don't understand what I'm doing wrong here ! I keep getting a Data Error. But I validated the JSON and it's ok...

Here is the javascript from the YUI Datatble example (slightly modified).



<pre class="prettyprint"><code>

<script type="text/javascript">
YAHOO.util.Event.addListener(window, "load", function() {
    //var Ex = YAHOO.namespace('example');

    var dataSource = new YAHOO.util.DataSource("jsondb/json_meta_proxy.html",{
        responseType : YAHOO.util.DataSource.TYPE_JSON,
        responseSchema : {
            resultsList: "records",
            fields: [
                            {key:"idprojet"},
                            {key:"nomprojet"}
                    ],
                    metaFields: { 
                    totalRecords: "totalRecords"
                    }
        },
        doBeforeCallback : function (req,raw,res,cb) {
            // This is the filter function
            var data     = res.results || [],
                filtered = [],
                i,l;

            if (req) {
                req = req.toLowerCase();
                for (i = 0, l = data.length; i < l; ++i) {
                    if (!data[i].state.toLowerCase().indexOf(req)) {
                        filtered.push(data[i]);
                    }
                }
                res.results = filtered;
            }

            return res;
        }
    });

    var cols = [
        {key:"idprojet"},
            {key:"nomprojet"}
    ];

    var paginateur = new YAHOO.widget.Paginator({
        rowsPerPage   : 25,
        pageLinks     : 10
    });

    var conf = {
        paginator : paginateur,
        sortedBy: {key:'idprojet', dir:YAHOO.widget.DataTable.CLASS_ASC}
    };

    var dataTable = new YAHOO.widget.DataTable('tbl',cols,dataSource,conf);

    var filterTimeout = null;
    var updateFilter  = function () {
        // Reset timeout
        filterTimeout = null;

        // Reset sort
        var state = dataTable.getState();
            state.sortedBy = {key:'idprojet', dir:YAHOO.widget.DataTable.CLASS_ASC};

        // Get filtered data
        dataSource.sendRequest(YAHOO.util.Dom.get('filter').value,{
            success : dataTable.onDataReturnInitializeTable,
            failure : dataTable.onDataReturnInitializeTable,
            scope   : dataTable,
            argument: idprojet
        });
    };

    YAHOO.util.Event.on('filter','keyup',function (e) {
        clearTimeout(filterTimeout);
        setTimeout(updateFilter,600);
    });
});
</script>

and here is the JSON data in the file "jsondb/json_meta_proxy.html"



    {
    "recordsReturned": 1,
    "totalRecords": 1,
    "startIndex": 0,
    "sort": "idprojet",
    "dir": "asc",
    "records": [
        {
            "idprojet": "11256",
            "nomprojet": ""
        }
    ]
}

Many thanks for your help !!!

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

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

发布评论

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

评论(2

旧竹 2024-09-05 05:25:28

更改为 TYPE_JSARRAY 解决了问题...

感谢您的评论埃里克!
我去了这个论坛,它确实是数据表问题的地方。

changing to TYPE_JSARRAY solved the problem...

Thanks for your comments Eric !
I went to this forum and it is indeed the place for datatable questions.

时光礼记 2024-09-05 05:25:28

如果您在 StackOverflow 上没有得到答案,您可能需要尝试将问题发布到 YUI 开发者论坛:http:// /yuilibrary.com/forum。许多 YUI 专家,包括最有经验的 DataTable 开发人员,都在那里闲逛。

-埃里克

If you don't get an answer on StackOverflow, you may want to try posting your question to the YUI developer forums: http://yuilibrary.com/forum. Many YUI experts, including the most experienced DataTable developers, hang out there.

-Eric

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