Show Data in grid from DataSource in YUI?

90 views Asked by At

I have attached my code in this question.

I am using YUI-3 for showing data in grid format from DataSource

I could not get data from datasource.But data passing from URL.

My grid showing No data to display always..

<script src="http://yui.yahooapis.com/3.18.1/build/yui/yui-min.js"></script>
<script>
YUI().use("datatable", "datasource-get", "datasource-jsonschema", "datatable-datasource", function (Y) {

    var url = "http://domainname.com/trans/sample_json.php?format=json",
        query = "&return=true",
        dataSource,
        table;

    dataSource = new Y.DataSource.Get({ source: url });

    dataSource.plug(Y.Plugin.DataSourceJSONSchema, {
        schema: {
            resultListLocator: "query.results.result",
            resultFields: [
                "Title",
                "Phone",
                {
                    key: "Rating",
                    locator: "Rating.AverageRating",
                    parser: function (val) {
                        // YQL is returning "NaN" for unrated restaurants
                        return isNaN(val) ? -1 : +val;
                    }
                }
            ]
        }
    });

    table = new Y.DataTable({
        columns: [
            "Title",
            "Phone",
            {
                key: "Rating",
                formatter: function (o) {
                    if (o.value === -1) {
                        o.value = '(none)';
                    }
                }
            }
        ],
        width:"100%",sortable: true,
        summary: "Pizza places near 98089",
        caption: "Table with JSON data from Datasource...."
    });

    table.plug(Y.Plugin.DataTableDataSource, { datasource: dataSource });

    table.render("#pizza");

    table.datasource.load({

        callback: {
            success: function (e) {
                table.datasource.onDataReturnInitializeTable(e);
            },
            failure: function() {
                Y.one('#pizza').setHTML(
                    'The data could not be retrieved. Please <a href="?mock=true">try this example with mocked data</a> instead.');
            }
        }
    });
});
</script>

This is sample_json.php file. Here I am sending data in JSON format.

<?php
echo '{"query": {"count": 2,"created": "2013-06-04T22:50:08Z","lang": "en-US","results": {"result":[{"Title" : "Giovannis Pizzeria","Phone" : "(408) 734-4221","Rating": {"AverageRating": "4"}},{"Title" : "Pizza","Phone" : "(800) 555-1212","Rating": {"AverageRating":"NaN"}}]}}}';
?>

I could not find where is error. Can anyone help me.?

1

There are 1 answers

1
MonteCristo On

your PHP doesn't look like it's returning JSON data. Try this?

<?php
    $data = {"query": {"count": 2,"created": "2013-06-04T22:50:08Z","lang": "en-US","results": {"result":[{"Title" : "Giovannis Pizzeria","Phone" : "(408) 734-4221","Rating": {"AverageRating": "4"}},{"Title" : "Pizza","Phone" : "(800) 555-1212","Rating": {"AverageRating":"NaN"}}]}}};

    header('Content-Type: application/json');

    echo json_encode($data);