Trying to run datatables in grails
Trying to run datatables in grails
Hello
I just found out this awesome jquery plugin but couldn't get it to work with my grails program (maybe the lack of javascript experience)
Basically what i am trying to do is read a JSON list back from my controller but the problem is i just can't seem to execute the controller? Here is what i have done
html header
[code]
Testing tables
$(document).ready(function(){
$('#people-table').dataTable({
"sAjaxSource":
});
});
[/code]
body
[code]
id
amount
name
[/code]
controller
[code]
def returnJSON = {
println "lalalala"
def superSalaryList = TestMap.list()
println 'a' + superSalaryList
render superSalaryList as JSON
}
[/code]
I think the issues is at the sAjaxSource requires a certain formatted link? https?
Thanks
I just found out this awesome jquery plugin but couldn't get it to work with my grails program (maybe the lack of javascript experience)
Basically what i am trying to do is read a JSON list back from my controller but the problem is i just can't seem to execute the controller? Here is what i have done
html header
[code]
Testing tables
$(document).ready(function(){
$('#people-table').dataTable({
"sAjaxSource":
});
});
[/code]
body
[code]
id
amount
name
[/code]
controller
[code]
def returnJSON = {
println "lalalala"
def superSalaryList = TestMap.list()
println 'a' + superSalaryList
render superSalaryList as JSON
}
[/code]
I think the issues is at the sAjaxSource requires a certain formatted link? https?
Thanks
This discussion has been closed.
Replies
I'm afraid I know little to nothing about grails, but the following two examples might be of use to you:
http://datatables.net/examples/data_sources/js_array.html
http://datatables.net/examples/data_sources/ajax.html
The first has a Javascript array as the data source, and the second gives DataTables a URL to go and get the data from a different address.
Regards,
Allan
I've tried both of the example before i post it here
but i think the problem is that the sAjaxSource = is not firing the link that i pass in
[code]
Testing tables
$(document).ready(function(){
$('#people-table').dataTable({
"sAjaxSource":
});
});
[/code]
did i miss something in the sytax for the datatables?
Regards,
Allan
this is my gsp code
[code]
$(document).ready(function(){
$('#people-table').dataTable({
"sAjaxSource":'${createLink (action:'returnJSON')}'
});
});
Sample title
Sample line
[/code]
here's my html output
[code]
$(document).ready(function(){
$('#people-table').dataTable({
"sAjaxSource":'/GrailsTestJQuery/car/returnJSON'
});
});
Sample title
Sample line
[/code]
i tried
a few options
1. createLink
2.
3. remoteLink
4. remoteFunction
Allan
the issues is at the gsp page where i must have the table code available
[code]
id
name
[/code]
now the problem is the data is not showing
i formated my data return like this
[code]
def returnJSON = {
def data = [:]
def carList = Car.list()
def carListInJSON = carList as JSON
def carHeader = [["sTitle":"id"],["sTitle":"name"]] as JSON
data.put("aaData",carListInJSON)
data.put("aoColumns",carHeader)
data.put("iTotalRecords",carList.count())
data.put("iTotalDisplayRecords",2)
data.put("sEcho",1)
println 'data' + data
return data
}
[/code]
what is the correct format should i put for the return data ?
Thanks
http://datatables.net/examples/examples_support/json_source.txt
Allan
here my code and output
[code]
def returnJSON = {
def aaData = TestMap.list()
println "aaData = " + aaData
def data = [:]
println "data in JSON = " + data
data.put("aaData",aaData)
// println "after put aaData into data = " + data
return data as JSON
}
[/code]
does this look correct? is there any specific way in building a JSON ?
what i did is not putting the data into a map but into a list. This will resolve the issues where the map key cannot match with the id i define in the gsp page.
[code]
def returnJSON = {
println "lalalala"
def list = []
def listData = TestMap.list()
listData.each{
list << [
it.id,
it.name
]
}
def data = ["aaData":list]
render data as JSON
}
[/code]
my gsp code
[code]
Testing tables
@import "../css/demo_table.css";
$(document).ready(function(){
$('#people-table').dataTable({
"sAjaxSource":'${createLink (action:'returnJSON')}'
});
});
Testing tables
lala
id
name
[/code]
why if i put my action into the following method and it won't work?
[code]
def returnJSON = {
println "lalalala"
def list = []
def listData = TestMap.list()
listData.each{
list << [
"id":it.id,
"name":it.name
]
}
def data = ["aaData":list]
render data as JSON
}
[/code]
take note on the id and name at the list <<
shouldn't this be map with the id and name at the gsp page?
like this
[code]
id
name
[/code]
Another option might be to use fnServerData to "post-process" your data and take it from the format reuired by your server page, and change it into that needed by DataTables. But that's another step in the process...
Regards,
Allan