ajax table load not working

ajax table load not working

traveptravep Posts: 5Questions: 0Answers: 0
edited November 2013 in General
I am using struts2. Before I understood how to use the json plugin with struts I did the following and have the table loading:

[code]
/* WORKING */
//below produces :: [{"retryID":1,"retryCreateTime":1383836882000,"retryCount":14,"retrySuccessTime":null,"serverAlias":"Login1","active":true,"comment":"here is my comment","createDate":1383836882000,"domain":"dorkville.com","emailAddr":"bobbysue@dorkville.com","emailCrypt":"647e04222a98762fb5b1a8e67420c5b8","importID":8,"listID":2,"removeDate":1383836882000,"siteID":1,"unsubLinkID":1},{"retryID":2,"retryCreateTime":1383836882000,"retryCount":11,"retrySuccessTime":null,"serverAlias":"Login1","active":true,"comment":"another comment","createDate":1383836882000,"domain":"dorkville.com","emailAddr":"bettysue@dorkville.com","emailCrypt":"c49385e32b96d7ff99d15d165572ab2e","importID":8,"listID":2,"removeDate":1383836882000,"siteID":1,"unsubLinkID":1},{"retryID":3,"retryCreateTime":1383836882000,"retryCount":8,"retrySuccessTime":null,"serverAlias":"Login1","active":true,"comment":"still another comment","createDate":1383836882000,"domain":"windstream.net","emailAddr":"sallysue@windstream.net","emailCrypt":"f3079e5b02ce4b2216dec7bc12721bf1","importID":31,"listID":6,"removeDate":null,"siteID":1,"unsubLinkID":1},{"retryID":4,"retryCreateTime":1383836917000,"retryCount":11,"retrySuccessTime":null,"serverAlias":"Login1","active":true,"comment":"i have no comment","createDate":1383836917000,"domain":"sbcglobal.net","emailAddr":"mollysue@sbcglobal.net","emailCrypt":"a3e0b52881180ec852d5026735676b65","importID":31,"listID":6,"removeDate":null,"siteID":1,"unsubLinkID":1}];
var aaData = <%= request.getAttribute("retryData") %>;
$(document).ready( function () {
oTable = $('#example').dataTable( {
"bFilter": false,
"aaData": aaData,
"aaSorting":[[1,"asc"]],
"aoColumnDefs": [
{"aTargets":[0],"sTitle":"RetryId","mData":"retryID","bVisible":false,"sType":"numeric"},
{"aTargets":[1],"sTitle":"Server","mData":"serverAlias"},
{"aTargets":[2],"sTitle":"Site","mData":"siteID","sType":"numeric"},
{"aTargets":[3],"sTitle":"List","mData":"listID","sType":"numeric"},
{"aTargets":[4],"sTitle":"Email","mData":"emailAddr"},
{"aTargets":[5],"sTitle":"Crypt","mData":"emailCrypt"},
{"aTargets":[6],"sTitle":"Create Time","mData":"retryCreateTime","sType":"date", "mRender": formatDate},
{"aTargets":[7],"sTitle":"Count","mData":"retryCount","sType":"numeric"}
]
} );
}
[/code]

Notice that I hack it calling an attribute set in the struts action called retryData and manually create a js array upon entering the page. The table is then loaded from that array.

Now that it's working I want to do it properly. I have my action now returning an array but the table is not finding what it needs. Here is what I'm doing now:

[code]

/* NOT WORKING */
//data coming back from ajax call to /sultan/allRetryData:
//{ aaData: [{"retryID":1,"retryCreateTime":1383836882000,"retryCount":14,"retrySuccessTime":null,"serverAlias":"Login1","active":true,"comment":"here is my comment","createDate":1383836882000,"domain":"dorkville.com","emailAddr":"bobbysue@dorkville.com","emailCrypt":"647e04222a98762fb5b1a8e67420c5b8","importID":8,"listID":2,"removeDate":1383836882000,"siteID":1,"unsubLinkID":1},{"retryID":2,"retryCreateTime":1383836882000,"retryCount":11,"retrySuccessTime":null,"serverAlias":"Login1","active":true,"comment":"another comment","createDate":1383836882000,"domain":"dorkville.com","emailAddr":"bettysue@dorkville.com","emailCrypt":"c49385e32b96d7ff99d15d165572ab2e","importID":8,"listID":2,"removeDate":1383836882000,"siteID":1,"unsubLinkID":1},{"retryID":3,"retryCreateTime":1383836882000,"retryCount":8,"retrySuccessTime":null,"serverAlias":"Login1","active":true,"comment":"still another comment","createDate":1383836882000,"domain":"windstream.net","emailAddr":"sallysue@windstream.net","emailCrypt":"f3079e5b02ce4b2216dec7bc12721bf1","importID":31,"listID":6,"removeDate":null,"siteID":1,"unsubLinkID":1},{"retryID":4,"retryCreateTime":1383836917000,"retryCount":11,"retrySuccessTime":null,"serverAlias":"Login1","active":true,"comment":"i have no comment","createDate":1383836917000,"domain":"sbcglobal.net","emailAddr":"mollysue@sbcglobal.net","emailCrypt":"a3e0b52881180ec852d5026735676b65","importID":31,"listID":6,"removeDate":null,"siteID":1,"unsubLinkID":1}] };
$(document).ready( function () {
oTable = $('#example').dataTable( {
"bFilter": false,
"aaSorting":[[1,"asc"]],
"aoColumnDefs": [
{"aTargets":[0],"sTitle":"RetryId","mData":"retryID","bVisible":false,"sType":"numeric"},
{"aTargets":[1],"sTitle":"Server","mData":"serverAlias"},
{"aTargets":[2],"sTitle":"Site","mData":"siteID","sType":"numeric"},
{"aTargets":[3],"sTitle":"List","mData":"listID","sType":"numeric"},
{"aTargets":[4],"sTitle":"Email","mData":"emailAddr"},
{"aTargets":[5],"sTitle":"Crypt","mData":"emailCrypt"},
{"aTargets":[6],"sTitle":"Create Time","mData":"retryCreateTime","sType":"date", "mRender": formatDate},
{"aTargets":[7],"sTitle":"Count","mData":"retryCount","sType":"numeric"}
],
sAjaxSource: "/sultan/allRetryData"
} );
}
[/code]


I put the json string above the javascript so you can see the string it's using. The only difference is that the new string wraps the previous json result in a variable: "{ aaData: ........ }

What do I need to do to make the table recognize the data needed in the new resulting json string?

Travis

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Travis,

    That looks like it should work to me. Can you link to a test page showing the problem so we can debug it please?

    Allan
  • traveptravep Posts: 5Questions: 0Answers: 0
    Allan, I appreciate the response. Sorry it took me a day but we needed to spin out an environment for you to see...we are early on in dev. I created a test that has the same problem. It takes the canned example from the site and my struts layer returns a modified version of the test dataset. Here is the link to the page:

    http://ec2-107-22-110-8.compute-1.amazonaws.com:8080/sultan/test

    Here is a link to the ajax call...just to see that it's returning the data:

    http://ec2-107-22-110-8.compute-1.amazonaws.com:8080/sultan/testMe

    Thanks again for looking...I want to get this working properly.

    Travis
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Your sAjaxSource parameter is pointing at its own page: `"sAjaxSource": "/sultan/test",` , not your data source... :-)

    Allan
  • traveptravep Posts: 5Questions: 0Answers: 0
    Allan, you are right, of course. This is a mistake I made when jacking around with it yesterday. It is supposed to be calling "/sultan/testMe" so it shows the real error. I have made that change and promoted it to the same place:

    http://ec2-107-22-110-8.compute-1.amazonaws.com:8080/sultan/test

    Could I trouble you to look at it again? I am sorry to have messed that up earlier. Thanks so much...

    Travis
  • syndesissyndesis Posts: 12Questions: 0Answers: 0
    the json coming back from testMe is:
    [code]{"aaData":"[{'engine': 'Trident','browser': 'Internet Explorer 4.0','platform': 'Win 95+','version': '4','grade': 'X'},{'engine': 'Trident','browser': 'Internet Explorer 5.0','platform': 'Win 95+','version': '5','grade': 'C'}]"}[/code]

    i don't think the whole array is supposed to be in " "

    untested, YMMV, HTH, HAND
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Agreed - aaData should be an array, not a string.

    Allan
  • traveptravep Posts: 5Questions: 0Answers: 0
    Allan and syndesis,

    Thanks for your input...and it was right on. I was taking a json string and returning it via Struts. Struts then posts it in it's own json string and it didn't come out right. So I'm working with objects now and letting Struts do the translation...so it's all good.

    I really appreciate you helping me out!

    Travis
This discussion has been closed.