YUI3 datatable not rendering data when using Plugin.DataTableDataSource with JSON
-
30-06-2021 - |
Question
I've been trying, unsuccessfully, to get a yui3 datatable to correctly populate via a json call. I am flummoxed and hope someone can show me the error of my ways. No matter what I have tried I just get "No data to display".
When I run wireshark during the page load I am seeing the json data returned from the ajax call. So I think I have the data source bound to the data table correctly. Note that the data records I am interested in are nested with metadata and it appears my DataSourceJSONSchema definition is correct.
Here is the http response from the ajax call.
YUI.Env.DataSource.callbacks.yui_3_6_0_1_1346868215546_114({
"recordsReturned":4,"startIndex":0,"dir":"asc","pageSize":10,
"records":[
{"id":"48ee0540-ebd7-11e1-33e-c33ce39c9e", "email":"jim@example.com","name":"James"},
{"id":"1447ea60-eca2-11e1-33e-f6c33ce39c9e", "email":"john@example.com","name":"John"},
{"id":"48ff6a60-ebd7-11e1-a33e-f6c33ce39c9e", "email":"ryan@example.com","name":"Ryan"},
{"id":"1a298060-f774-11e1-ad38-f6c33ce39c9e", "email":"vincent@example.com","name":"Vince"}]})
Here is the html page.
<script type="text/javascript">
YUI({
lang : "en-US"
})
.use("datatable","datatype","datasource",function(Y) {
var ajaxData;
function ajaxSuccess(e) {
ajaxData = e.data;
}
}
function ajaxFailure(e) {
Y.log("failure");
}
var myDataSource = new Y.DataSource.Get({
source : "/actions/User.action?getAjaxUserList=&"
});
myDataSource.plug(Y.Plugin.DataSourceJSONSchema,{
schema : {
metaFields : {
recordsReturned : "recordsReturned",
startIndex : "startIndex",
dir : "dir",
pageSize : "pageSize"
},
resultsListLocator : "records",
resultFields : [ {
key : 'id'}, {
key : 'email'},{
key : 'name' }}});
var table = new Y.DataTable({
columns : [ {
key : "id",
label : "ID"
}, {
key : "name",
label : "Name"}, {
key : "email",
label : "Email"} ],
caption : "My first DataTable!",
summary : "Example DataTable showing basic instantiation configuration"
});
table.plug(Y.Plugin.DataTableDataSource, {datasource : myDataSource});
table.render('#dynamicdata');
table.datasource.load({request : encodeURIComponent('fred=steve')});
});
</script>
<div id="dynamicdata"></div>
Solution
I noticed several syntax error on the code you give here : The function(Y) callback is closed too soon for example.
I think your error is due to the fact that you set in the schema of your Datasource the property "resultsListLocator" instead of "resultListLocator".