过滤JSON请求

时间:2014-09-02 16:46:03

标签: json yui

我有一组简单的JSON数据,我从本地文件中提取并加载到数据表中

使用YUI,如何过滤此请求的响应以仅匹配与请求数据相关的数据?

编辑:第一篇文章格式不正确

YUI().use('aui-datatable', 'datatable-sort', 'aui-io-request', 'aui-tabview', 'datasource-io',
    function(Y) {

        var columns = [{
            key : 'id',
            sortable : true
        }, {
            key : 'name',
            sortable : true
        },{
            key : 'price',
            sortable : true
        }];
        var dataTable = new Y.DataTable({
            columns : columns
        }).render("#searchResultsTab");

        var node = Y.one('#searchButton');
        var criteria = document.getElementById("searchCriteria");
        node.on(
            'click', //on Search..
            function(){
                dataSource = new Y.DataSource.IO({source:'mydata.json'});
                request = document.getElementById("searchBox").value;

                dataSource.sendRequest({
                    on: {
                        success: function(e){
                            var response = e.data.responseText;
                            jdata = Y.JSON.parse(response);

                            dataTable.set('data', jdata.info); //setting table data to json response
                        },
                            failure: function(e){
                                alert(e.error.message);
                            }
                        }
                });
             }
         );
        new Y.TabView(
              {
                srcNode: '#searchResultsContainer'
              }
            ).render();

      });

mydata.json

{"info" : [
    {"id": 1,"name": "A green door","price": 12.50 },
    {"id": 2,"name": "A blue door","price": 10.50 },
    {"id": 3,"name": "A red door","price": 8.50 }
}

1 个答案:

答案 0 :(得分:0)

在on on success方法中,在设置数据表数据源之前过滤响应数据。以下是模型列表过滤的示例:http://yuilibrary.com/yui/docs/model-list/#filtering-models